Jump to content
Sign in to follow this  
danielmohr91

Array variable subscript badly formatted - but only when Global...

Recommended Posts

danielmohr91

Ok, I've seen this error before, but I don't have a clue why I'm getting it here. I'm making a second GUI. Since this is only small part of a larger project, it needs to be within a function. I'm using a for... to... loop to make a bunch of input boxes, but if I add "global" to line 82 (the line where I make the boxes), I get the error, "Array variable subscript badly formatted." I looked it up on the forums, and it looks like that's caused by having a subscript less than zero, but I know here that $i = 0 - and it works fine without the global in front. It also seems to work if it's not inside a function. I'm really confused. Help would be greatly appreciated! I don't need you to rewrite the code or anything, I just would like to know what I'm doing wrong with the array's subscript and how to fix it. Thanks!

This is the problem part:

For $i = 0 To UBound($GUI_Gift_Input) - 1

        ; Why does this give me so many problems when I make it global?
        ;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
        Global $GUI_Gift_Input[$i] = GUICtrlCreateInput($array[0], 24, $bottom + 135 + (32 * $i), $width, 31) ;; If I take off global here it doesn't work for the other functions. @@@@
        ;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
        ;; Error - Array variable subscript badly formatted.


        GUICtrlSetFont(-1, $__Input_Font_size, $bold, 0, $font)
        $temp = IniRead($file, $section, "$GUI_Gift_Input[" & $i & "]", $name & $i + 1)
        GUICtrlSetData($GUI_Gift_Input[$i], $temp)
    Next

Here's the whole bottom part of my script that's giving the error. It's line 87 on here, and I commented around it - you'll see it.

HotKeySet("{f3}", "_GIFT_GUI")
HotKeySet("{f4}", "Gifting_Start_Pos")

; Include
#include <WinAPI.au3>
#include <GUIScroll.au3> ; UDF for scrolling
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <SliderConstants.au3>
#include <StaticConstants.au3>
#include <TabConstants.au3>
#include <WindowsConstants.au3>

Global $file = @ScriptDir & "\Settings.ini"
_GIFT_GUI()

;**********************************************************************************************************************************************
;******************************************                  Form Entry                  *****************************************************
;**********************************************************************************************************************************************
;**********************************************************************************************************************************************

Func _GIFT_GUI()
    MsgBox(0, "", "hey")
    ; GUI variables
    $GUI_Gift_Title = "Form Entry"
    $bold = 800
    $width = 250
    $__Group_Font_size = 11
    $__Tab_Font_size = 10
    $__Input_Font_size = 12
    Global $id_Start_Gifting = 28
    Global $id_end = 51

    ; font
    $temp = @OSVersion
    If $temp = "WIN_XP" Then
        Local $font = "Times New Roman"
    Else
        ;$font = "Times New Roman"
        Local $font = "Segoe Print"
    EndIf

    ; GUI
    $GUI = GUICreate($GUI_Gift_Title, 300, 600, @DesktopWidth - 350, -1, BitOR($WS_MINIMIZEBOX, $WS_CAPTION, $WS_SYSMENU)) ;$WS_THICKFRAME; and vertical resize here
    WinSetOnTop($GUI_Gift_Title, "", 1)
    GUISetFont(10, 400, 0, $font)
    $GUI_Gift_Label10 = GUICtrlCreateLabel("Form Entry", 72, 8, 280, 32)
    GUICtrlSetFont(-1, 14, $bold, 0, $font)

    GUICtrlCreateGroup("Start", $__Group_Font_size, 50, 280, 150)
    GUICtrlSetFont(-1, $__Group_Font_size, $bold, 0, $font)
    Global $GUI_Gift_Slider1 = GUICtrlCreateSlider(24, 80, $width, 33)
    Global $GUI_Gift_Button1 = GUICtrlCreateButton("Start", 24, 140, $width - 10, 50, $WS_GROUP)
    GUICtrlSetFont(-1, 12, 400, 0, $font)
    GUICtrlCreateLabel("Fast", 40, 112, 27, 23)
    GUICtrlSetFont(-1, 8, 400, 0, $font)
    GUICtrlCreateLabel("Slow", 224, 112, 30, 23)
    GUICtrlSetFont(-1, 8, 400, 0, $font)

    $bottom = 240 ;;  Bottom of ^ start group
    GUICtrlCreateGroup("Friends", $__Group_Font_size, $bottom - 25, 280, 815)
    GUICtrlSetFont(-1, $__Group_Font_size, $bold, 0, $font)
    GUICtrlCreateLabel("Fill in these boxes with the names", 24, $bottom, $width, 27)
    GUICtrlCreateLabel("of the friends you want to gift to.", 24, $bottom + 25, $width, 27)
    GUICtrlCreateLabel("First name and last name initial", 24, $bottom + 50, $width, 27)
    GUICtrlCreateLabel("should do, just enough to tell", 24, $bottom + 75, $width, 27)
    GUICtrlCreateLabel("them apart.", 24, $bottom + 100, $width, 27)
    GUICtrlCreateGroup("", -99, -99, 1, 1)
    GUISetFont(10, 400, 0, "Segoe Print")

    Dim $GUI_Gift_Input[20]
    Dim $array[UBound($GUI_Gift_Input)]
    Global $name = "Name "
    For $i = 0 To UBound($GUI_Gift_Input) - 1

        ; Why does this give me so many problems when i make it global?
        ;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
        Global $GUI_Gift_Input[$i] = GUICtrlCreateInput($array[0], 24, $bottom + 135 + (32 * $i), $width, 31) ;; If I take off global here it doesn't work for the other functions. @@@@
        ;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
        ;; Error - Array variable subscript badly formatted.


        GUICtrlSetFont(-1, $__Input_Font_size, $bold, 0, $font)
        $temp = IniRead($file, $section, "$GUI_Gift_Input[" & $i & "]", $name & $i + 1)
        GUICtrlSetData($GUI_Gift_Input[$i], $temp)
    Next

    ; Activate memory bank and fill in the input boxes
    Memory_Bank_1()

    ; Scrolling feature
    Scrollbar_Create($GUI, $SB_VERT, $bottom + 800); Last number is the length to scroll for (in pixels)
    Scrollbar_Step(20, $GUI, $SB_VERT); Scrolls per 20 pixels. If not set the default is 1 (smooth scrolling)

    GUICtrlSetLimit($GUI_Gift_Slider1, 50, 0)
    $speed = IniRead($file, "Speed", "$speed", 10)
    GUICtrlSetData($GUI_Gift_Slider1, $speed)
    GUISetState(@SW_SHOW)
    MsgBox(0, "", @ScriptLineNumber)

    Global $EditFocus = 0 ; no edit has focus
    Global $LastEdFocus = 0
    GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND") ; or some function name
    GUISetState()

    While 1
        $iScrollPos = Scrollbar_GetPos($GUI, $SB_VERT)
        ConsoleWrite($iScrollPos & @CRLF)

        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
            Case $GUI_Gift_Input[0] To $GUI_Gift_Input[UBound($GUI_Gift_Input) - 1] ;one way of dealing With it, because we know that the control has the ID $nMsg
                $temp = GUICtrlRead($GUI_Gift_Input[$nMsg - $id_Start_Gifting])
                $key = "$GUI_Gift_Input[" & $nMsg - $id_Start_Gifting & "]"
                IniWrite($file, $section, $key, $temp)
            Case $GUI_Gift_Button1
                Start_Gifting()
            Case $GUI_Gift_Slider1
                $temp = GUICtrlRead($GUI_Gift_Slider1)
                IniWrite($file, "Speed", "$speed", $temp)
        EndSwitch
    WEnd
EndFunc   ;==>_GIFT_GUI

Func _WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)
    Local $old_ID = "anything"
    Local $new_ID = "anything unequal to $old_ID"
    Local $iIDFrom, $iCode

    $iIDFrom = _WinAPI_LoWord($iwParam)
    ;The wParam has 8 bytes. Depending on the message or notification windows will use those bytes in different ways. For $EN_SETFOCUS it uses it for 2
    ;completely different things. The high order bytes are used for the id number of the control, and the low order bytes are used for the notification
    ;reference number. So if wParam is 0x00090100 it means that the control with ID 9 wants to tell you that it has just received focus because
    ;$EV_SETFOCUS is 0x0100.

    $iCode = _WinAPI_HiWord($iwParam)
    ;the high word of the $iwParam is the notification code, ie the reason for the message - The text has changed, focus gained/lost etc

    Switch $iCode
        Case $EN_KILLFOCUS ; Sent when an edit control loses the keyboard focus
            $EditFocus = 0
        Case $EN_SETFOCUS ; Sent when an edit control receives the keyboard focus
            $EditFocus = $iIDFrom
    EndSwitch
;~ If control get's focus we get the ID, if it loses focus, zero

    If $EditFocus <> $LastEdFocus And Not WinExists("Address") Then
;~      $msg1 = GUICtrlRead($Edit1)
        $msg2 = "$iCode = " & $iCode & @CRLF
        $msg4 = "$iwParam = " & $iwParam & @CRLF
        $msg3 = "$EditFocus = " & $EditFocus & @CRLF

        ;TrayTip("Focus", $EditFocus, 2, 1)
        Switch $EditFocus
            Case $id_Start_Gifting To $id_end
                TrayTip("", $EditFocus, 10)
                $temp = $EditFocus - $id_Start_Gifting
                $temp2 = GUICtrlRead($GUI_Gift_Input[$temp])
                ;TrayTip("", $temp2, 10)
;~              If StringLeft($temp2, 4) = $name Then
                If StringLeft($temp2, 4) = "Name" Then
                    GUICtrlSetData($GUI_Gift_Input[$temp], "")
                EndIf
            Case 0
                ;TrayTip("Focus lost", $EditFocus, 2, 1)
                $temp = $LastEdFocus - $id_Start_Gifting
                If $temp >= 0 And IsNumber($temp) Then
                    $temp2 = GUICtrlRead($GUI_Gift_Input[$temp])
                    If $temp <= UBound($GUI_Gift_Input) And $temp2 = "" Then ; ubound = 20 in this case
                        GUICtrlSetData($GUI_Gift_Input[$temp], "Name " & $temp + 1)
                        $data = GUICtrlRead($GUI_Gift_Input[$temp])
                        $key = "$GUI_Gift_Input[" & $temp & "]"
                        IniWrite($file, $section, $key, $data)
                    EndIf
                EndIf
        EndSwitch
    EndIf

    $LastEdFocus = $EditFocus

    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_COMMAND

Func Start_Gifting()
    $speed = GUICtrlRead($GUI_Gift_Slider1)
    $msg = "Place your cursor over the top checkbox, then press F4 to begin"
    $temp = SplashTextOn("", $msg, "725", "55", "-1", "-100", 1, "Segoe Print", "16", "400")
    $temp = 100
    WinSetTrans("", $msg, $temp)
    WinMove("", $msg, @DesktopWidth / 2 - 350, 35, 725, 55, 5)
    For $i = $temp To 230 Step 5
        WinSetTrans("", $msg, $i)
        Sleep(50)
    Next

    Do
        Sleep(10)
        $temp = @HotKeyPressed
    Until $temp = "{f4}"
EndFunc   ;==>Start_Gifting

Func Gifting_Start_Pos()
    $pos = MouseGetPos()
    $delay = 150
    SplashOff()
    Send("^{f10}") ; to reset the @HotKeyPressed macro...

    For $i = 0 To UBound($GUI_Gift_Input) - 1 ; GUI_Gift_Input[24]
        $key = "$GUI_Gift_Input[" & $i & "]"
        $name = IniRead($file, $section, $key, "none")
        $data = StringLeft($name, 4)
        If $name <> "none" And $data <> "Name" Then
            MouseClick("primary", $pos[0], $pos[1] - 20, 1, $speed)
            Sleep($delay / 2 + 25 * $speed)
            Send("^{a}")
            Sleep($delay + 25 * $speed)
            TrayTip($i, $name, 5)
            Send($name)
            Sleep($delay + 25 * $speed)
            MouseClick("primary", $pos[0], $pos[1], 1, $speed)
            Sleep($delay + 25 * $speed)
        EndIf
    Next
EndFunc   ;==>Gifting_Start_Pos

Func _reset()
    ; this function is called when I need to reset the @hotkeypressed macro
EndFunc   ;==>_reset

Func Memory_Bank_1()
    $i = 1
    Memory_Bank_Switch($i)
EndFunc   ;==>Memory_Bank_1

Func Memory_Bank_2()
    $i = 2
    Memory_Bank_Switch($i)
EndFunc   ;==>Memory_Bank_2

Func Memory_Bank_3()
    $i = 3
    Memory_Bank_Switch($i)
EndFunc   ;==>Memory_Bank_3

Func Memory_Bank_4()
    $i = 4
    Memory_Bank_Switch($i)
EndFunc   ;==>Memory_Bank_4

Func Memory_Bank_5()
    $i = 5
    Memory_Bank_Switch($i)
EndFunc   ;==>Memory_Bank_5

Func Memory_Bank_6()
    $i = 6
    Memory_Bank_Switch($i)
EndFunc   ;==>Memory_Bank_6

Func Memory_Bank_7()
    $i = 7
    Memory_Bank_Switch($i)
EndFunc   ;==>Memory_Bank_7

Func Memory_Bank_8()
    $i = 8
    Memory_Bank_Switch($i)
EndFunc   ;==>Memory_Bank_8

Func Memory_Bank_9()
    $i = 9
    Memory_Bank_Switch($i)
EndFunc   ;==>Memory_Bank_9

Func Memory_Bank_Switch($i) ; This runs on startup with a  variable of 1
    Global $section = "Friends" & $i
    For $i = 0 To UBound($GUI_Gift_Input) - 1
        $temp = IniRead($file, $section, "$GUI_Gift_Input[" & $i & "]", $name & $i + 1)
        GUICtrlSetData($GUI_Gift_Input[$i], $temp)
    NextEndFunc   ;==>Memory_Bank_Switch

Share this post


Link to post
Share on other sites
Melba23

danielmohr91,

Two parts to the answer:

1. If you add Global here:

For $i = 0 To UBound($GUI_Gift_Input) - 1
    Global $GUI_Gift_Input[$i] = GUICtrlCreateInput($array[0].........

you are resetting the array to have $i elements - and what is the first value of $i? :D You cannot dimension an array to 0 elements, so you get an error. Anyway, you do not need Global here - you do not want to keep resetting the array, you are trying to fill it!

2. You should be declaring this array as Global when it first appears a couple of lines above. You should not really use Dim - AutoIt will do its best to guess what scope you want for variables declared with Dim, but it is better coding practice to declare them explicitly with Global/Local. Personally I only use Dim to clear an array and reset its elements - and even than I try to use Global/Local if I remember. : I know Valik would like to remove Dim from the language - imagine the howls if he did! :huggles:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
danielmohr91

danielmohr91,

Two parts to the answer:

1. If you add Global here:

For $i = 0 To UBound($GUI_Gift_Input) - 1
    Global $GUI_Gift_Input[$i] = GUICtrlCreateInput($array[0].........

you are resetting the array to have $i elements - and what is the first value of $i? :D You cannot dimension an array to 0 elements, so you get an error. Anyway, you do not need Global here - you do not want to keep resetting the array, you are trying to fill it!

2. You should be declaring this array as Global when it first appears a couple of lines above. You should not really use Dim - AutoIt will do its best to guess what scope you want for variables declared with Dim, but it is better coding practice to declare them explicitly with Global/Local. Personally I only use Dim to clear an array and reset its elements - and even than I try to use Global/Local if I remember. : I know Valik would like to remove Dim from the language - imagine the howls if he did! :huggles:

M23

Ha he he! So simple! Thanks Melba! I feel like an idiot. And thanks for the advice on dim/Global/Local -- point taken. I switched them, and will avoid using dim in the future. For some reason I thought you had to use dim when making an array? Thanks so much!

~Daniel

Share this post


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
Sign in to follow this  

×