Jump to content
Sign in to follow this  
byoda

UDF as a variable in a function

Recommended Posts

byoda

All,

I was wondering if I could have a UDF as a variable in a function. Essentially, when the GO button is pushed, Func 3 is called, which in turn calls Func 2. I get an error message saying "Variable used without being declared on the "$test=GUICreate" line. When I take out the $test, it works, but I need to use the $test variable. This is why I am thinking I am not able to have a UDF as a variable, within a function. Thanks.

Func 1()

Case statement

Case $msg=$Go

Func 3()

EndFunc

Func 2()

$test=GUICreate("Test", 300, 75)

EndFunc

Func 3()

Func 2()

EndFunc

Share this post


Link to post
Share on other sites
Melba23

byoda,

If you tidy up your code so it actually runs, you get no errors at all. :blink:

Global $msg = 1, $go = 1

_1()

Func _1()
    Switch $msg
        Case $msg = $go
            _3()
    EndSwitch
EndFunc   ;==>_1

Func _2()
    $test = GUICreate("Test", 300, 75)
EndFunc   ;==>_2

Func _3()
    _2()
EndFunc   ;==>_3

Now what was the question? ;)

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

 

Share this post


Link to post
Share on other sites
byoda

No, it doesnt. The $test = GUICreate("Test", 300, 75) runs great if:

a. It is outside of a function.

b. It is inside of the function, BUT the $test is removed, leaving GUICreate("Test", 300, 75)

So what I am asking is, can you have a UDF as a variable, within a function? Is this legal? Thanks.

Share this post


Link to post
Share on other sites
Melba23

byoda,

A light dawns perhaps! :blink:

Do you have Opt("MustDeclareVars", 1) somewhere in your script? ;)

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

 

Share this post


Link to post
Share on other sites
DicatoroftheUSA

No, it doesnt. The $test = GUICreate("Test", 300, 75) runs great if:

a. It is outside of a function.

b. It is inside of the function, BUT the $test is removed, leaving GUICreate("Test", 300, 75)

So what I am asking is, can you have a UDF as a variable, within a function? Is this legal? Thanks.

inside the func, before guicreate.

local $test

Edited by DicatoroftheUSA

Share this post


Link to post
Share on other sites
byoda

Melba,

Yes it was set.

DIC, lol.... You got it good sir.... That worked. Could you elaborate why? Thanks.

Share this post


Link to post
Share on other sites
Melba23

byoda,

Variables in AutoIt are of (essentially) 2 kinds - Global and Local (we will forget Static because it is still not fully resolved).

Global variables are visible to the main section and any function in the code - Local variables are only visible within the function in which they are defined. AutoIt will automatically scope variables in the main script as Global and in functions as Local unless you tell it otherwise. Using Opt("MustDeclareVariables", 1) means that you MUST explicitly declare your variables as either Global or Local or you get an error. :blink:

Look at this script:

; This is the main script

$vVar1 = 10 ; This is Global by default

; See what is visible in the function
Test()

; See what is visible in the main script
ConsoleWrite("In Main Script " & $vVar1 & @CRLF) ; This will work
ConsoleWrite("In Main Script " & $vVar2 & @CRLF) ; This will give you an error

Func Test()

    $vVar2 = 20 ; This is Local by default

    ; Both of these will work
    ConsoleWrite("In Function " & $vVar1 & @CRLF)
    ConsoleWrite("In Function " & $vVar2 & @CRLF)

EndFunc

Now let us declare $vVar2 as Global and see what happens:

; This is the main script

$vVar1 = 10 ; This is Global by default

; See what is visible in the function
Test()

; See what is visible in the main script
ConsoleWrite("In Main Script " & $vVar1 & @CRLF) ; This will work
ConsoleWrite("In Main Script " & $vVar2 & @CRLF) ; This will give you an error

Func Test()

    Global $vVar2 = 20 ; This is Local by default, but declared Global <<<<<<<<<<<<<<<<<<<<<<<<<<<<

    ; Both of these will work
    ConsoleWrite("In Function " & $vVar1 & @CRLF)
    ConsoleWrite("In Function " & $vVar2 & @CRLF)

EndFunc

And we can now see the variable outside the function. ;)

All clear?

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

 

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  

×