Jump to content
Sign in to follow this  
JDK

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

Recommended Posts

JDK

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
Richard Robertson

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
Authenticity

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
JDK

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.