Jump to content

Storing GUI Objects in an Array - (Error: Subscript used on non-accessible variable)


Recommended Posts

 

I am trying to store an object in an array. I am posting here instead of the GUI section because I can't consoleWrite the output and I imagine its something to do with my general understanding of vars/arrays in autoit.   Tried a ton of things, really struggling with this one. I come from Python/Javascript coding  but love autoit and would like to use it more.

Working Text Array:

Global $arrayButtons[3] = ["Button0", "Button1", "Button2"]
_ArrayAdd($arrayButtons, "Button3")

 

A - Creating the array and console write

This is the array I try to add the var to (you can see the console write output as well):

Global $arrayButtonVars[3] = ["1", "2", "3"]
ConsoleWrite(@CRLF & "|" & "$arrayButtonVars[2]: " & $arrayButtonVars[2] & ", IsArray: " & IsArray($arrayButtonVars & "|" & @CRLF))
Quote

Output: |$arrayButtonVars[2]: 3, IsArray: 1

B - Adding an object to the array (Normally I wouldnt mix strings and objects in one array, this is just my most recent attempt at trying to understand the problem.)

 

#Region BUTTON1
Global $arrayButtonVars[3] = GUICtrlCreateButton($arrayButtons[3], 25, 50, 100, 30)
GUICtrlSetTip(-1, '#Region BUTTON')
#EndRegion BUTTON1

C - The problem comes when I try to use  $arrayButtonVars now. Exact same console write as before is now causing an error:

 

ERROR OUTPUT (Same console write: )

Spoiler

 

>"GUI Custom\Custom Panel 1.3.au3"    

|$arrayButtonVars[2]: 3, IsArray: 1"Custom Panel 1.3.au3" (123) : ==> Subscript used on non-accessible variable.:
ConsoleWrite(@CRLF & "|" & "$arrayButtonVars[2]: " & $arrayButtonVars[2] & ", IsArray: " & IsArray($arrayButtonVars & "|" & @CRLF))
ConsoleWrite(@CRLF & "|" & "$arrayButtonVars[2]: " & $arrayButtonVars^ ERROR
>Exit code: 1    Time: 0.2457

 

 

D - Main goal is to use the object in my GUI

Trying to access the object when the button is pushed (same error as before about non-accessible variable):

While 1
    Switch GUIGetMsg()
        Case $arrayButtonVars[3]
            ConsoleWrite("  Something  ")
    EndSwitch
WEnd

 

 

 


 

Link to post
Share on other sites

Remember arrays are zero based so you need to use $arrayButtonVars[2], basic example:

#include <GUIConstantsEx.au3>
Global $g_aButtons[3][2] = [["","Button 1"],["","Button 2"],["","Button 3"]]

Example()

Func Example()
    ; Create a GUI with various controls.
    Local $hGUI = GUICreate("Example", 105, 95)
    $g_aButtons[0][0] = GUICtrlCreateButton($g_aButtons[0][1], 10, 10, 85, 25)
    $g_aButtons[1][0] = GUICtrlCreateButton($g_aButtons[1][1], 10, 35, 85, 25)
    $g_aButtons[2][0] = GUICtrlCreateButton($g_aButtons[2][1], 10, 60, 85, 25)

    GUISetState(@SW_SHOW, $hGUI)

    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $g_aButtons[0][0] To $g_aButtons[2][0]
                MsgBox(4096, "Button", GUICtrlRead($nMsg))
        EndSwitch
    WEnd
    GUIDelete($hGUI)
EndFunc

 

Edited by Subz
Link to post
Share on other sites

Its so rare to post a question and the first response be straight to the point. I really appreciate it. That example is perfect. ❤️ 

Coming from python/js I'm used to arrays being 0 based so I feel like its more likely that I was confused on the rules around array size and declaring them. I definitely didnt understand that when you declare the array you dont use 0 base (seems really confusing to go between two systems like that).  The way you loop through the array when reading the gui state is really great. Will read up on Switch/Case more. 

Any advice on main difference or things to read up on for someone coming from Python/Javascript? 

I wasted so much time on this its a bit embarrassing, learned a ton from your example. Ty

 

Link to post
Share on other sites
  • Moderators

gorillamath,

Quote

 I definitely didnt understand that when you declare the array you dont use 0 base

But you do. Declaring an array with 5 elements ($aArray[5]) gives you an array with elements $aArray[0] to $aArray[4].

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 post
Share on other sites
Posted (edited)

Ya I get it. I was also wrong on it being a "weird" way to declare the array length.

Realized that in js you can declare an array as:

new Array(3)

Its just not something I do since modifying the array as we code seems so natural:

someArray = ["a","b"]

someArray.push("c")
someString = "defg"


for (let i=0; i<someString.length; i++){
    someArray.push(someString[i])
}

console.log(someArray)
someArray >> ["a", "b", "c", "d", "e", "f", "g"]

 

Why do we need to declare the length of the array before using it in autoit? Something with memory management / the way the compiler works? Is there some simple concept I am missing when it comes to using arrays? 

Not a complaint btw, I assume that ppl have a good reason for doing things when they are on the level of creating coding languages. Curios and might help with my intuition.

Edited by gorillamath
Link to post
Share on other sites
12 minutes ago, gorillamath said:

Why do we need to declare the length of the array before using it in ahk?

Why not ask on the ahk forum?

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to post
Share on other sites

Yeah, rest in peace😈

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to post
Share on other sites
  • Moderators

gorillamath,

Quote

Why do we need to declare the length of the array before using it in autoit?

You do not have to declare a length in all cases - but there are 2 types of data collection variables in AutoIt - arrays and maps - which both use very similar notation. It is the manner in which the variable is declared which determines its type:

Local $aVar[3] ; An Array 
Local $vVar[3] = [1, 2, 3] ; An Array  )
Local $vVar[] = [1, 2, 3] ; An Array   ) These 3 declarations are functionally identical
Local $vVar = [1, 2, 3] ; An Array     )

Local $mVar[] ; A Map

So as you  can see, there is no need to declare the size of an array if you declare the content at the same time. Declaring neither size nor content creates a map.

Does that 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 post
Share on other sites

I recommend against the Local $vVar[] = [1, 2, 3] syntax: should you at some time decide that the static initialization isn't the best way for your code and leave Local $vVar[] only, then your variable instanly and silently transmutates into a map, which will cause an error in your code when used, possibly months after release if the offending part of code is used rarely.

Local $vVar[]
; ... much code
; now you know that $vVar will store 3 entries
Dim $vVar[3]
; Beware: $vVar is STILL a Map!
ConsoleWrite("$vVar is a " & VarGetType($vVar) & @LF)
; ... much code
$vVar[0] = "aaa"
$vVar[1] = "bbb"
; ... much code
_daily()
ConsoleWrite("$vVar[1] contains '" & $vVar[1] & "'" & @LF)
; ... much code
ConsoleWrite("'zzz' stored in vVar at index: " & _yearly() & @LF)

Func _daily()
    $vVar[1] = "zzz"
EndFunc

Func _yearly()
    Local $index = _ArraySearch($vVar, "zzz")
    Return ($index >= 0 ? $index : "Not found")
EndFunc

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to post
Share on other sites

 

Spoiler

 

Thank you.

 

 

I believe the correct names are:

Local $aVar[3] ; "COY ARRAY"   ;..I know exact element count but you need to wait to see the elements
Local $vVar[3] = [1, 2, 3] ; "YOU OK TO DRIVE ARRAY"
Local $vVar[] = [1, 2, 3] ; "TRIVIA ARRAY"   ;great for trivia night and passive aggressive coders
Local $vVar = [1, 2, 3] ; "ARRAY"

Prize goes to anyone that can give a use case for #1 or #2 that cant be coded using the regular array + one length check in the code

 

Useful Map Guide:

 

 

Link to post
Share on other sites

I made a bug report in Trac about Dim unable to change a Map into an Array https://www.autoitscript.com/trac/autoit/ticket/3879

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...