Sign in to follow this  
Followers 0
JDK

How do I get a reference to a variable by name dynamically?

4 posts in this topic

I would like to get a reference to a global variable by name dynamically because I can't use an array in my particular case. What I want to do is something like this:

Global $var1
Global $var2
Global $var3

Func test()
For $i = 1 To 3
$targetVar = $("var" & $i)   ; THIS LINE WILL NOT WORK!!! WHAT IS THE CORRECT SYNTAX???
GUICtrlSetData($targetVar, "data for label " & $i & " goes here") 
Next
EndFunc

Can anyone give me an example of the proper syntax?

Thanks!!

Share this post


Link to post
Share on other sites



There is no case where you can't use an array.

If you choose to do it this way, you use Eval("var" & $i).

Share this post


Link to post
Share on other sites

Opt('ExpandVarStrings', 1)

Global $var1 = 1
Global $var2 = 2
Global $var3 = 3

test()

Func test()
    For $i = 1 To 3
        ConsoleWrite(Eval("var" & $i) & @CRLF)
        GUICtrlSetData(Eval("var$i$"), "data for label " & $i & " goes here")
    Next
EndFunc   ;==>test

This is the correct syntax, but you should consider using arrays. There is only array equivalence to such an iteration construct.

Share this post


Link to post
Share on other sites

Opt('ExpandVarStrings', 1)

Global $var1 = 1
Global $var2 = 2
Global $var3 = 3

test()

Func test()
    For $i = 1 To 3
        ConsoleWrite(Eval("var" & $i) & @CRLF)
        GUICtrlSetData(Eval("var$i$"), "data for label " & $i & " goes here")
    Next
EndFunc   ;==>test

This is the correct syntax, but you should consider using arrays. There is only array equivalence to such an iteration construct.

Thanks, I'll give this a shot. My problem is, Koda Form Designer doesn't appear to allow GUI elements with name fields that are arrays. It wants simple variables. I'll see if I can use this syntax rather than resort to a whole lot of simple variables and a big Select. Thanks!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0