Jump to content

Array-style Referencing


Recommended Posts

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

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 by sahsanu
Link to comment
Share on other sites

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

  • 1 year later...

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

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

JimPBarber,

Excellent. Glad I could help. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...