Jump to content

Passing a parameter (Variable used without being declared)


Recommended Posts

Posted (edited)

Hi all,

Can anyone help me and possibly explain why I'm getting the error message:

Variable used without being declared.:

on the following code

AutoItSetOption ( "MustDeclareVars", 1)
myFunction()


Func myFunction($getCustomName=True)
    If $getCustomName Then
        MsgBox(0,"Custom Name",$getCustomName)
    Else
        MsgBox(0,"Custom Name","None Given")
    EndIf
EndFunc

 

Edited by NassauSky
Typo
Link to post
Share on other sites
Posted (edited)

When I corrected MsbBox and reran the script, I didn't get any errors.  The error I saw was on the MsbBox line.

Edited by TheXman
Link to post
Share on other sites
  • Moderators

NassauSky,

Works fine for me too.

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

Sorry OK I realized it must be my implementation:

This example shows the error

 

AutoItSetOption ( "MustDeclareVars", 1)
Opt("GUIOnEventMode", 1)

Global  $hGUI = GUICreate("myHelper", 600, @DesktopHeight-80, @DesktopWidth-600, 0) ;w,h,x,y
Global $hExportList = GUICtrlCreateButton("Export List",200,25,80,25)   ;x,y,w,h
   GUICtrlSetOnEvent(-1, "myFunction")
GUISetState()
While 1
   Sleep(10)
WEnd

Func myFunction($getCustomName=True); Handle both a button press in EventMode and a call to the function
    If $getCustomName Then
        MsgBox(0,"Custom Name","Called through button press")
    Else
        MsgBox(0,"Custom Name","Called through function call myFunction(False)")
    EndIf
EndFunc

 

Link to post
Share on other sites
  • Moderators

NassauSky,

Here is how I might go about coding a solution:

AutoItSetOption ( "MustDeclareVars", 1)
Opt("GUIOnEventMode", 1)

HotKeySet("{ESC}", "_Exit")

Global $getCustomName = "Button"

Global  $hGUI = GUICreate("myHelper")
Global $hExportList = GUICtrlCreateButton("Export List",200,25,80,25)   ;x,y,w,h
   GUICtrlSetOnEvent(-1, "myFunction")
GUISetState()

 myFunction_Direct("Direct")

While 1
   Sleep(10)
WEnd

Func myFunction_Direct($Param = True)
    $getCustomName = $Param
    myFunction()
    $getCustomName = "Button"
EndFunc

Func myFunction(); Handle both a button press in EventMode and a call to the function
    If $getCustomName = "Button" Then
        MsgBox(0,"Custom Name","Called through button press")
    Else
        MsgBox(0,"Custom Name","Called through function call with param: " & $getCustomName)
    EndIf
EndFunc

Func _Exit()
    Exit
EndFunc

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

Thanks @Melba23.  I try to use my Global Variables sparingly. That's what I'll wind up doing.  It's just funny that the function does get called from the button event trigger and functions correctly if I don't make any references to the missing passed parameter.

 

Link to post
Share on other sites
Posted (edited)

Only occurs when using GuiOnEventMode and MustDeclareVars, you could use

AutoItSetOption ( "MustDeclareVars", 1)

Global  $hGUI = GUICreate("myHelper", 600, @DesktopHeight-80, @DesktopWidth-600, 0) ;w,h,x,y
Global $hExportList = GUICtrlCreateButton("Export List",200,25,80,25)   ;x,y,w,h
GUISetState()
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hExportList
            myFunction()
    EndSwitch
WEnd

Func myFunction($getCustomName = True); Handle both a button press in EventMode and a call to the function
    If $getCustomName Then
        MsgBox(0,"Custom Name","Called through button press")
    Else
        MsgBox(0,"Custom Name","Called through function call myFunction(False)")
    EndIf
EndFunc

 

Edited by Subz
Link to post
Share on other sites
Posted (edited)

Thanks @Subz

Yeah in this case I created the app a year ago and was tweaking it and must have used EventMode for a reason. Was faster to implement the global variable option.

 

Thanks again though I also like to hear other options like yours.

Edited by NassauSky
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...