2

I've got code to build variables based on a string using Eval and Execute. But for whatever reason, Execute is leaving the variable empty when I try to set it to a variable. But works fine if I set it to a string manually

This DOES NOT work, Gives an empty box:

For j = 0 To 2
    name = "alias_" & j
    val = "test"
    Execute(Eval("name") + "=" + val)
    msgbox(Execute(Eval("name"))
Next

This DOES work, shows "test" in the msgbox:

For j = 0 To 2
    name = "alias_" & j
    Execute(Eval("name") + "=" + "test")
    msgbox(Execute(Eval("name"))
Next

So Execute doesn't seem to like the variable here. What's even stranger, in my VBSEdit debugger, I can see the locals, and it creates a local variable called "test" instead of setting the value to the alias_j variable. Totally confusing me now.

3
  • Don't construct variables like that. Ever. If you need indexed variables: use arrays. That's what they were invented for. Commented Oct 6, 2014 at 16:20
  • Actually I would use dictionaries.. and will use dictionaries.. was having another vb-ism with dictionaries so I was trying something more creative with arrays, but didn't want iterative arrays. But I got dictionaries working properly now so this is defunct. Still good info... people are too afraid of 2 of the most powerful functions. Commented Oct 6, 2014 at 20:32
  • You're confusing "afraid" and "wary". Commented Oct 7, 2014 at 6:48

1 Answer 1

2

Are you trying to create variables named alias_0 thru alias_2 and assign a value to each? If so, this should work. It assigns the value test0 to alias_0, test1 to alias_1, etc.:

For j = 0 To 2
    name = "alias_" & j
    val = "test" & j
    Execute name & " = val"
    MsgBox Eval("alias_" & j)
Next
Sign up to request clarification or add additional context in comments.

10 Comments

Yes. that did it. Thanks! I think the issue was the "+" was trying to do math.. as I was wrong in my original post.. it only worked with a variable if it was a number. But your way works fine with strings. Thanks!
I just updated my answer a few seconds ago. Take another look. And, yes, always use & in lieu of + for string concatenation.
@Bond Personal opinion here but I try to avoid & as a concatenation operator whenever I can. It hides the conversions. Debug enough of other peoples scripts and it gets really annoying. Obviously I still use & when i'm just working out an idea. Or when on the RARE times I won't know the type. Otherwise it's + with CStr() of the Cxxx family of conversion functions. Maybe this is just my mindset coming from a typed/strongly typed C/C++/C# background But i prefer TELLING VBScript what I want my variable to be at any particular time rather than relying on VBScript to decide what is best.
@TofuBug You should really use &. & already casts both types to a string so there's no need for unnecessary CStr() casting. The beauty of using typeless languages is that you don't have to constantly coerce values between types. You're taking one of the best features away and treating it like a strongly-typed language, which is really just more work. str12 = CStr("1") + CStr("2") is overkill. You could argue that your code is more readable because you're being explicit about your types. I would argue the opposite. It's messier because you don't have a clear understanding of them.
@TofuBug (Ran out of characters in my last post!). You're right, though, it's a personal preference. And I don't mean to insult your preference. Just know that & only does string concatenation. That's it. + is overloaded to handle integer and real arithmetic as well as string concatenation so, unless you're diligent about using CStr() everywhere, there's a chance you may not get what you want.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.