Jump to content

Array help


Recommended Posts

I have 20 checkboxes that I want to read/print in an efficient manner. Right now, I have a very inefficient 20 section function similar to what I have here, but I want to use an array to replace checkCN1 with checkCN[$var], where $var is 1, 2, 3... 20.

I'd appreciate any help with using arrays so I can cut down length.

$complete = "Complete"
$incomplete = "Incomplete"
$var = 1
While $var <= 20
    If GUICtrlRead($checkCN[$var]) == 1 Then ; If checkbox true, then complete
        $status_CN[$var] = $complete
    Else ; If checkbox false, incomplete
        $status_CN[$var] = $incomplete
    EndIf
WEnd

This gives me the error

C:\Documents and Settings\Administrator\Desktop\gui3a.au3 (1069) : ==> Variable used without being declared.:

If GUICtrlRead($checkCN[$var]) == 1 Then

If GUICtrlRead(^ ERROR

Link to comment
Share on other sites

  • Moderators

ranhalt,

Assuming you haave the ControlIDs of the 20 checkbowes in the $checkCN array, you need a For...Next loop:

$complete = "Complete"
$incomplete = "Incomplete"
For $var = 1 To 20
    If GUICtrlRead($checkCN[$var]) = 1 Then ; If checkbox true, then complete
        $status_CN[$var] = $complete
    Else ; If checkbox false, incomplete
        $status_CN[$var] = $incomplete
    EndIf
Next

Note that the == operator is only used for making case-sensitive comparisons between strings - you do not need it here.

And you can set strings directly into an array, you do not need to set them as variables first:

$status_CN[$var] = "Complete"

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

OK, this could involve a lot more change than anticipated. I have no existing array, it really is just 20x of this right now:

If GUICtrlRead($checkCN1) == 1 Then ; If checkbox true, then complete
        $status_CN1 = $complete
    Else ; If checkbox false, incomplete
        $status_CN1 = $incomplete
    EndIf

I'm working on creating an array, which is what I think I need, but not to store the status of the checkboxes, just as an index. I want it to increment + 1 while I read the values of the checkboxes.

and replace this:

If GUICtrlRead($checkCN1) == 1 Then ; If checkbox true, then complete
        $status_CN1 = $complete
    Else ; If checkbox false, incomplete
        $status_CN1 = $incomplete
    EndIf
    
    If GUICtrlRead($checkCN2) == 1 Then ; If checkbox true, then complete
        $status_copy = $complete
    Else ; If checkbox false, incomplete
        $status_copy = $incomplete
    EndIf
    
    If GUICtrlRead($checkCN3) == 1 Then ; If checkbox true, then complete
        $status_CN3 = $complete
    Else ; If checkbox false, incomplete
        $status_CN3 = $incomplete
    EndIf
    
    If GUICtrlRead($checkCN4) == 1 Then ; If checkbox true, then complete
        $status_CN4 = $complete
    Else ; If checkbox false, incomplete
        $status_CN4 = $incomplete
    EndIf

with this (not entirely accurate syntax):

X = 1
while 
    If GUICtrlRead($checkCNX) == 1 Then ; If checkbox true, then complete
        $status_CNX = $complete
    Else ; If checkbox false, incomplete
        $status_CNX = $incomplete
    EndIf
        X += 1
wend
Link to comment
Share on other sites

  • Moderators

ranhalt,

This is extremely ugly and, in my opinion, very bad coding practise, but you can get round your wish to avoid arrays like this: :evil:

#include <GUIConstantsEx.au3>

Global $statusCN1, $statusCN2

$hGUI = GUICreate("Test", 500, 500)

$checkCN1 = GUICtrlCreateCheckbox("", 10, 10, 20, 20)
GUICtrlSetState(-1, $GUI_CHECKED)
$checkCN2 = GUICtrlCreateCheckbox("", 10, 50, 20, 20)

GUISetState()

For $i = 1 To 2
    If GUICtrlRead(Eval("checkCN" & $i)) = 1 Then ; If checkbox true, then complete
        Assign("statusCN" & $i, "complete")
    Else ; If checkbox false, incomplete
        Assign("statusCN" & $i, "incomplete")
    EndIf
Next

ConsoleWrite("StatusCN1: " & $statusCN1 & @CRLF)
ConsoleWrite("StatusCN2: " & $statusCN2 & @CRLF)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

But I still firmly belive that you should recast the code to use arrays - having to use Eval and Execute like this is usually a sign that you are on the wrong track! ;)

M23

Edit: Left some errorchecking code in and did not want to confuse!

Edited by Melba23

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

It shouldn't really take a whole lot of changes to put everything in an array, which would really serve you well.

Go to the part of your script where you're defining all the checkboxes. Just above it, add this:

Dim $checkCNX[20]
(this tells AutoIt that $checkCNX is an array, and will have 20 elements in it).

Now, go through your script...wherever you have a line like, $checkCNX1=GUICtrlCreateCheckbox(...), just replace it with

$checkCNX[0]=GUICtrlCreateCheckbox(...)
(note arrays start at 0, not 1, so the first checkbox should be 0, and the 20th should be 19.

Basically the same thing as you've had, but it's a single array now, instead of 20 variables.

Now you need an array to store the results, so add:

Dim $status_CNX[20]

Then use your For...Next loop like:

For $i=0 To 19
    If GUICtrlRead($checkCNX) == 1 Then ; If checkbox true, then complete
        $status_CNX[$i] = $complete
    Else ; If checkbox false, incomplete
        $status_CNX[$i] = $incomplete
    EndIf
Next

That way, it increments itself (saves a line of code, no biggie, just cleaner), and stores all your checkboxes are in one array, and all your results are in another.

Does that make sense?

Edited by james3mg
"There are 10 types of people in this world - those who can read binary, and those who can't.""We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true." ~Robert Wilensky0101101 1001010 1100001 1101101 1100101 1110011 0110011 1001101 10001110000101 0000111 0001000 0001110 0001101 0010010 1010110 0100001 1101110
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...