Monday, October 1, 2007

Simplifying Evaluate

If you're only interested in getting a single value from Evaluate, you don't need to Dim a Variant to containt the Array Evaluate (almost) always returns.

Simply implode the evaluate, and you can assign the Evaluate directly.

Dim commonUsername As String
commonUsername = Implode( Evaluate( |@Name( [CN] ; @UserName )| ) )

9 comments:

Jake Howlett said...

Brilliant! I love tips like this and I hate dimming variants just to get one value.

J E Carter II said...

Very nice little trick, Tommy! Thanks for sharing!

Chris said...

I can't believe I never thought of that before. This has bugged me for years. I always wanted to do this instead:
Evaluate(|@Name([CN]; @UserName)|)(0)

Now I don't have to declare that useless temporary Variant variable anymore.

Thanks!

Nate said...

Nice one, Tommy!

I'm curious... why Implode instead of it's shorter alias Join?

Tommy Valand said...

I guess because of reading co-workers code, and being influenced by the formula-language, which @Implodes.

I think I'll try to change my habit, since, as one commenter on Codestore said, Join is three letters shorter than Implode, and split/join is also what those "operations" are called in JavaScript.

I also think split/join are better descriptions of what is goin on.

William Beh said...

Very very cool. Tried it out and started using it for application which is roll out to production.

Anonymous said...

You have to express more your opinion to attract more readers, because just a video or plain text without any personal approach is not that valuable. But it is just form my point of view

Anonymous said...

Does anyone know why this returns a empty string..?
Note it works if I replace s with "abcdefgh" in the actual evaluate...

Dim v as variant
Dim s as string

s = "abcdefgh"
v = Evaluate({@Middle(s; "B"; "H")})

msgbox v(0)

Should return "cdefg" but returns blank string?

Tommy Valand said...

Dim v as String
Dim s as String

s = "abcdefgh"
v = Join( Evaluate({@Middle("} + s + {"; "B"; "H")}) )

MsgBox v