StevenOfPomroyIL Posted April 28, 2011 Share Posted April 28, 2011 I would like to dynamically reference variables within a loop. I have seen dynamic array-style referencing in other languages and it is a lifesaver when you're making dynamic variables (which you don't always know the name of the variable--but you can iterate to it. $var1_string = "great" $var2_string = "good" $var3_string = "tasty" For $i = 1 to 3 Step 1 MsgBox("Pizza is " & $var[$i]_string) Next Any help is so so so much appreciated Link to comment Share on other sites More sharing options...
sahsanu Posted April 28, 2011 Share Posted April 28, 2011 (edited) I would like to dynamically reference variables within a loop. Take a look to Eval function. $var1_string = "great" $var2_string = "good" $var3_string = "tasty" For $i = 1 to 3 MsgBox(0,"","Pizza is " & Eval("var" & $i & "_string")) Next Edit: By the way, did you think about using arrays? Global $var_string[3] = ["great","good","tasty"] For $i = 0 to UBound($var_string)-1 MsgBox(0,"","Pizza is " & $var_string[$i]) Next Edited April 28, 2011 by sahsanu Link to comment Share on other sites More sharing options...
StevenOfPomroyIL Posted April 28, 2011 Author Share Posted April 28, 2011 Take a look to Eval function. $var1_string = "great" $var2_string = "good" $var3_string = "tasty" For $i = 1 to 3 MsgBox(0,"","Pizza is " & Eval("var" & $i & "_string")) Next Edit: By the way, did you think about using arrays? Global $var_string[3] = ["great","good","tasty"] For $i = 0 to UBound($var_string)-1 MsgBox(0,"","Pizza is " & $var_string[$i]) Next Brilliant. Thank you. Yes I'm using this with a dynamically-created object that contains within itself a 2-dimensional array. Link to comment Share on other sites More sharing options...
sahsanu Posted April 28, 2011 Share Posted April 28, 2011 You are welcome Link to comment Share on other sites More sharing options...
JimPBarber Posted February 22, 2013 Share Posted February 22, 2013 I am actually trying to do something similar, and I am running into the same things. I have code : $Combo1 = GUICtrlCreateCombo("",16, 56, 145, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL)) GUICtrlSetData($Combo1,"None|Type1|Type2|Type3","None") $Combo2 = GUICtrlCreateCombo("", 16, 88, 145, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL)) GUICtrlSetData($Combo2,"None|Type1|Type2|Type3","None") $Combo3 = GUICtrlCreateCombo("", 16, 120, 145, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL)) GUICtrlSetData($Combo3,"None|Type1|Type2|Type3","None") I want to access it something like this: $i = 1 While $i <= 3 $CombBoxHandle = "$Combo" & i GetComboVFunc GetComboValue($ComboBoxHandle) dim $ComboBoxValue _GUICtrlComboBox_GetCurSel($ComboBoxHandle) _GUICtrlComboBoxEx_GetItemText($ComboBoxHandle,_GUICtrlComboBox_GetCurSel($ComboBoxHandle),$ComboBoxValue) $ComboBoxValue = $ComboBoxValue & " Index: " & _GUICtrlComboBox_GetCurSel($ComboBoxHandle) Return $ComboBoxValue EndFunc $String=$String & GetComboVFunc GetComboValue($ComboBoxHandle) Wend Link to comment Share on other sites More sharing options...
Clark Posted February 22, 2013 Share Posted February 22, 2013 You just make Combo an array and reference them that way, i.e. $Combo[1], $Combo[2], $Combo[3]. for $cntr = 1 to 3 _GUICtrlDoStuff($Combo[$cntr],0,0,0) next Link to comment Share on other sites More sharing options...
JimPBarber Posted February 22, 2013 Share Posted February 22, 2013 I can retry this. However, when I originally tried it, it always returned 0 instead of the value of the indexed Item. You highlighted reference, are you saying I should use byRef? Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 22, 2013 Moderators Share Posted February 22, 2013 JimPBarber, No, he means use the array index as a way of referencing the specific control. This should give you the idea: #include <GUIConstantsEx.au3> Global $aCombo[3] $hGUI = GUICreate("Test", 500, 500) $aCombo[0] = GUICtrlCreateCombo("", 16, 56, 145, 25) GUICtrlSetData(-1, "None|Type1|Type2|Type3", "None") $aCombo[1] = GUICtrlCreateCombo("", 16, 88, 145, 25) GUICtrlSetData(-1, "None|Type1|Type2|Type3", "None") $aCombo[2] = GUICtrlCreateCombo("", 16, 120, 145, 25) GUICtrlSetData(-1, "None|Type1|Type2|Type3", "None") $cButton = GUICtrlCreateButton("Read", 10, 200, 80, 30) GUISetState() While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit Case $cButton $sString = "" For $i = 0 To 2 $sString &= GUICtrlRead($aCombo[$i]) & @CRLF Next MsgBox(0, "Read", $sString) EndSwitch WEnd Notice that with controls created with the native functions you do not normally need the UDF functions to interact with them. All clear? Please ask if not. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
JimPBarber Posted February 22, 2013 Share Posted February 22, 2013 Thanks Crystal! I see where I went wrong. Thank you for the excellent example. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 22, 2013 Moderators Share Posted February 22, 2013 JimPBarber, Excellent. Glad I could help. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now