Jump to content

Error Subscript used on non-accessible variable putting data into an array


Recommended Posts

I am trying to add data to an array and I keep getting the error "Subscript used on non-accessible variable ".

#include <array.au3>
; Includes, etc
~
~
~
Local $aOptionsArray[3]

$aOptionsArray = CheckboxesAndRadioButtons()
_ArrayDisplay($aOptionsArray)
Func CheckboxesAndRadioButtons()
; Create Checkboxes and Radio Buttons and read the results
~
~
~
    ; Radio Buttons to Array
    ; $aOptions[0] = $bTestSelectForever
    ; $aOptions[1] = $bTestSelect3Times
    ; $aOptions[2] = $bTestSelectOnce
    
    If $bSelect1 = 1 Then
        $aOptions[0] = "True"
    Else
        $aOptions[0] = "False"  <<< This is where the error occurs.
    EndIf

    If $bSelect2 = 1 Then
        $aOptions[1] = "True"
    Else
        $aOptions[1] = "False"
    EndIf

    If $bSelect3 = 1 Then
        $aOptions[2] = "True"
    Else
        $aOptions[2] = "False"
    EndIf
    Return $aOptions
EndFunc

Is putting data into an array while in a If - Then - Else illegal?

Link to comment
Share on other sites

Apologies - it is defined in the Function.  Updated script below. From what I understand if I create an array in a function and then return the array I need two different variable names - one in Main and one in the function. Yes?


 
#include <array.au3>
; Includes, etc
~
~
~
Local $aOptionsArray[3]

$aOptionsArray = CheckboxesAndRadioButtons()
_ArrayDisplay($aOptionsArray)

; lots of other code
~
~
~

Func CheckboxesAndRadioButtons()

Local $aOptions

; Create Checkboxes and Radio Buttons and read the results
; all variables in this Function were assigned either at the top of the script or in the function.
~
~
~
    ; Radio Buttons to Array
    ; $aOptions[0] = $bTestSelectForever
    ; $aOptions[1] = $bTestSelect3Times
    ; $aOptions[2] = $bTestSelectOnce
    
    If $bSelect1 = 1 Then
        $aOptions[0] = "True"
    Else
        $aOptions[0] = "False"  <<< This is where the error occurs.
    EndIf

    If $bSelect2 = 1 Then
        $aOptions[1] = "True"
    Else
        $aOptions[1] = "False"
    EndIf

    If $bSelect3 = 1 Then
        $aOptions[2] = "True"
    Else
        $aOptions[2] = "False"
    EndIf
    Return $aOptions
EndFunc

 

Link to comment
Share on other sites

OK I figured it out. I neglected to put a [3] after my declaration.

Local @aOptions[3] solved the issue.

This is weird because usually when I declare an array in other code I don't have to add [x] as in :

Local $aArray  << has worked before

vs

Local $aArray[3]  << required for this particular script... I don't understand why!

Link to comment
Share on other sites

Jibberish,

An alternative approach.  Nota Bene, for this to work the controls must be defined consecutively.  This is handy for a large number of controls that can change...

; *** Start added by AutoIt3Wrapper ***
#include <GUIConstantsEx.au3>
; *** End added by AutoIt3Wrapper ***

#include <array.au3>
; Includes, etc

#AutoIt3Wrapper_Add_Constants=n

Local $aOptionsArray[3]

; lots of other code

Local $gui010 = GUICreate('Test')
Local $bSelect1 = GUICtrlCreateCheckbox('#1', 10, 10, 100, 20)
Local $bSelect2 = GUICtrlCreateCheckbox('#2', 10, 30, 100, 20)
Local $bSelect3 = GUICtrlCreateCheckbox('#3', 10, 50, 100, 20)

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $gui_event_close
            Exit
        Case $bSelect1 To $bSelect3
            _CheckboxesAndRadioButtons()
            _ArrayDisplay($aOptionsArray)
    EndSwitch
WEnd

Func _CheckboxesAndRadioButtons()

    ; Create Checkboxes and Radio Buttons and read the results
    ; all variables in this Function were assigned either at the top of the script or in the function.
    ; Radio Buttons to Array
    ; $aOptions[0] = $bTestSelectForever
    ; $aOptions[1] = $bTestSelect3Times
    ; $aOptions[2] = $bTestSelectOnce

    For $i = 0 To UBound($aOptionsArray) - 1
        $aOptionsArray[$i] = (GUICtrlRead(Eval('bSelect' & $i + 1)) = $gui_checked) ? 'True' : 'False'
    Next

EndFunc   ;==>_CheckboxesAndRadioButtons

Also, it might be helpful for you to read (re-read) the helpfile sections concerning "scope" and function calls (byref vs. value).

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

OK I figured it out. I neglected to put a [3] after my declaration.

Local @aOptions[3] solved the issue.

This is weird because usually when I declare an array in other code I don't have to add [x] as in :

Local $aArray  << has worked before

vs

Local $aArray[3]  << required for this particular script... I don't understand why!

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

×
×
  • Create New...