Jump to content

Why can't arrays contain variables?


Loc
 Share

Go to solution Solved by Luke94,

Recommended Posts

For example, I set the variables to different names to make it easier to remember, instead of setting a variable $Key[nn], it's easy to call. Is there a way to solve the case of arrays containing variables to make the code less verbose?

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>

;~ Global $arrayall[3] = [$Label1, $Label2, $Label3, $Label4]

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 529, 437, 192, 124)
$Label1 = GUICtrlCreateLabel("", 56, 24, 36, 17)
$Label2 = GUICtrlCreateLabel("", 56, 64, 36, 17)
$Label3 = GUICtrlCreateLabel("", 56, 104, 36, 17)
$Label4 = GUICtrlCreateLabel("", 56, 144, 36, 17)
$Button1 = GUICtrlCreateLabel("SET", 56, 144, 36, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Global $arrayall[3] = [$Label1, $Label2, $Label3, $Label4]

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            _SETDATA()

    EndSwitch
WEnd

Func _SETDATA()
    For $i = 0 To UBound $arrayall[3]
    GUICtrlSetData($arrayall[$i], 1)
    Next
EndFunc

 

Link to comment
Share on other sites

Your code works (with a couple of fixes):

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>

;~ Global $arrayall[3] = [$Label1, $Label2, $Label3, $Label4]

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 529, 437, 192, 124)
$Label1 = GUICtrlCreateLabel("", 56, 24, 50, 17)
$Label2 = GUICtrlCreateLabel("", 56, 64, 50, 17)
$Label3 = GUICtrlCreateLabel("", 56, 104, 50, 17)
$Label4 = GUICtrlCreateLabel("", 56, 144, 50, 17)
$Button1 = GUICtrlCreateButton("SET", 56, 184, 36, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Global $arrayall[4] = [$Label1, $Label2, $Label3, $Label4]

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            _SETDATA()

    EndSwitch
WEnd

Func _SETDATA()
    For $i = 0 To (UBound($arrayall) - 1)
    GUICtrlSetData($arrayall[$i], 'Set data')
    Next
EndFunc

You could also declare an array of labels at the top, then assign them as you create the labels. Example:

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>

;~ Global $arrayall[3] = [$Label1, $Label2, $Label3, $Label4]

Global $g_iLabelID[4]

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 529, 437, 192, 124)
$g_iLabelID[0] = GUICtrlCreateLabel("", 56, 24, 50, 17)
$g_iLabelID[1] = GUICtrlCreateLabel("", 56, 64, 50, 17)
$g_iLabelID[2] = GUICtrlCreateLabel("", 56, 104, 50, 17)
$g_iLabelID[3] = GUICtrlCreateLabel("", 56, 144, 50, 17)
$Button1 = GUICtrlCreateButton("SET", 56, 184, 36, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
;~ Global $arrayall[4] = [$Label1, $Label2, $Label3, $Label4]

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            _SETDATA()

    EndSwitch
WEnd

Func _SETDATA()
    For $i = 0 To (UBound($g_iLabelID) - 1)
    GUICtrlSetData($g_iLabelID[$i], 'Set data')
    Next
EndFunc

Apologies if I'm misunderstanding your question..

Link to comment
Share on other sites

4 minutes ago, Danp2 said:

It isn't clear to me what you are trying to accomplish, so you may want to restate the problem.

Example: I set 5 variables.
$A, $B, $C, $D, $E, ... Then how can I combine these variables into an array so that I can change the state or data quickly without having to be verbose. Because the variable name I set to make myself easy to remember and recognize.

Edited by Loc
Link to comment
Share on other sites

  • Solution

I may still be misunderstanding so apologies if so.

Maybe a Map?

Maybe an Enum?

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>

;~ Global $arrayall[3] = [$Label1, $Label2, $Label3, $Label4]

Global Enum $LABEL_1 = 0, $LABEL_2, $LABEL_3, $LABEL_4

Global $g_iLabelID[4]

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 529, 437, 192, 124)
$Label1 = GUICtrlCreateLabel("", 56, 24, 100, 17)
$Label2 = GUICtrlCreateLabel("", 56, 64, 100, 17)
$Label3 = GUICtrlCreateLabel("", 56, 104, 100, 17)
$Label4 = GUICtrlCreateLabel("", 56, 144, 100, 17)
$Button1 = GUICtrlCreateButton("SET", 56, 184, 36, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Global $arrayall[4] = [$Label1, $Label2, $Label3, $Label4]

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            _SETDATA()

    EndSwitch
WEnd

Func _SETDATA()
    For $i = 0 To (UBound($arrayall) - 1)
    GUICtrlSetData($arrayall[$i], 'Set data')
    Next
    GUICtrlSetData($arrayall[$LABEL_3], 'This is Label 3')
EndFunc

 

Link to comment
Share on other sites

The question/details are not completely clear to me

  • You seem to want some logic name for each control
  • You seem to have a need to iterate over an array of named items 

Then you probably should look at the assign, eval, execute, assign functions

$a=10
$b=20
$c=30
$d=40
$e=50

$arrItemsOfInterest=stringsplit("a,c,e",",")

For $i = 0 To (UBound($arrItemsOfInterest) - 1)
    assign($arrItemsOfInterest[$i], eval($arrItemsOfInterest[$i])+5)
Next

consolewrite($a & @CRLF)
consolewrite($b & @CRLF)
consolewrite($c & @CRLF)
consolewrite($d & @CRLF)
consolewrite($e & @CRLF)

So what you want you can accomplish with

$arrItemsOfInterest=stringsplit("label1,label2,label3,label4",",")

 

Link to comment
Share on other sites

$A, $B, $C, $D, $E, ... Then how can I combine these variables into an array so that I can change the state,  data

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>

;~ Global $arrayall[3] = [$Label1, $Label2, $Label3, $Label4]

Global Enum $LABEL_1 = 0, $LABEL_2, $LABEL_3, $LABEL_4

Global $g_iLabelID[4]

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 529, 437, 192, 124)
$Label1 = GUICtrlCreateLabel("", 56, 24, 100, 17)
$Label2 = GUICtrlCreateLabel("", 56, 64, 100, 17)
$Label3 = GUICtrlCreateLabel("", 56, 104, 100, 17)
$Label4 = GUICtrlCreateLabel("", 56, 144, 100, 17)
$Button1 = GUICtrlCreateButton("SET", 56, 184, 36, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Global $arrayall[4] = ["1", "2", "3", "4"]
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            _SETDATA('off',"ee")

    EndSwitch
WEnd

Func _SETDATA($state,$data)
    For $i = 0 To (UBound($arrayall) - 1)
       Local $var
    $var &= $arrayall[$i]
 Next
 Local $prev = $var
 Local $new_array[1] = $var
 if $state = "on" Then
      $new_array = $data
      MsgBox(0,'',$new_array)
   Else
      $new_array = $prev
      MsgBox(0,'',$new_array)
      EndIf
EndFunc

 

Edited by ad777

iam ِAutoit programmer.

best thing in life is to use your Brain to

Achieve

everything you want.

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...