Jump to content

variable accessible in other functions - Global?


Recommended Posts

i am using this line to prompt user to choose a file

Global $afile = FileOpenDialog($message, @DesktopDir & "\", "Text Files (*.txt)", 1 + 4)

but then in a later function, the script doesnt know what $afile is.

any help would be apprecaited.

thanks.

Link to comment
Share on other sites

i am using this line to prompt user to choose a file

Global $afile = FileOpenDialog($message, @DesktopDir & "\", "Text Files (*.txt)", 1 + 4)

but then in a later function, the script doesnt know what $afile is.

any help would be apprecaited.

thanks.

That doesn't make any sense. Post a short demo script that will reproduce the symptoms. This works fine, how can you make it fail as you describe?
Global $message = "Test"
Global $afile = FileOpenDialog($message, @DesktopDir & "\", "Text Files (*.txt)", 1 + 4)

_MyFunc()

Func _MyFunc()
    MsgBox(64, "Results", "$afile = " & $afile)
EndFunc

muttley

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Global $afile = FileOpenDialog($message, @DesktopDir & "\", "Text Files (*.txt)", 1 + 4)

is residing within a function... then i try using $afile in a subsequent function and it doesnt recognize the variable.

Edited by gcue
Link to comment
Share on other sites

you must declare a GLOBAL on top (before all functions) - not within a FUNC statement to make it global

WRONG

Func _MyFunc()
Global $afile
EndFunc

Right

Global $afile

Func _MyFunc()
EndFunc
Edited by nobbe
Link to comment
Share on other sites

i have delcared Global $afile (wout a value of course) outside of every function BUT the $afile doesnt get set until a user initializes the fileopendialog (which happens within a function)

Link to comment
Share on other sites

i have delcared Global $afile (wout a value of course) outside of every function BUT the $afile doesnt get set until a user initializes the fileopendialog (which happens within a function)

Only declare it once at top of file, declared more then will make a null/empty value in the variable

Correct me if I'm wrong

Link to comment
Share on other sites

i have delcared Global $afile (wout a value of course) outside of every function BUT the $afile doesnt get set until a user initializes the fileopendialog (which happens within a function)

then i dont understand the problem? maybe in func2 you call the variable before it is set to a value in func1?

example:

when pressing "show" it will not show any file result until user selected "refresh file" button first and selected a file

#include <GUIConstants.au3>
Global $afile 


$gui = GUICreate("GUI", 853, 725, 193, 115)
$btn_Refresh_file = GUICtrlCreateButton("refresh file", 576, 24, 75, 25, 0)
$btn_show_result = GUICtrlCreateButton("show", 576, 64, 75, 25, 0)


GUISetState(@SW_SHOW)



While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg

        Case $btn_Refresh_file          
$afile = FileOpenDialog("open", @DesktopDir & "\", "Text Files (*.txt)", 1 + 4)
        Case $btn_show_result
                        
  MsgBox(64, "Results", "$afile = " & $afile)
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd
Link to comment
Share on other sites

i have delcared Global $afile (wout a value of course) outside of every function BUT the $afile doesnt get set until a user initializes the fileopendialog (which happens within a function)

Personally, I think that having a local variable declared as global has no purpose whatsoever. I don't even think AutoIt should have implemented the local and global keywords.

Anyways,

Global $x = 100

Func Test()
    Global $y = 250
EndFunc

MsgBox(0, "", $x)
Test()
MsgBox(0, "", $y)

Whenever the function gets called, the variable $y will be added to AutoIt's list of variables (or however that is implemented) and it will be global throughout the source. So, until you initially call the function in which your variable is created, it technically won't be declared (and isn't accessible throughout your source).

So like everybody said, I'd recommend creating a global variable that is technically global - not a local variable.

Edited by cppman
Link to comment
Share on other sites

its an "interpreter" language limitation

a variable is used within a function but hasnt been declared.. . but what if the function is never called?? then the script runs without an error. with a compiled language it would catch the error on compile time..

declaring local variables makes sense if you use e.g. udfs (all use e.g. $i as variables)

Link to comment
Share on other sites

its an "interpreter" language limitation

a variable is used within a function but hasnt been declared.. . but what if the function is never called?? then the script runs without an error. with a compiled language it would catch the error on compile time..

declaring local variables makes sense if you use e.g. udfs (all use e.g. $i as variables)

I was referring to it just simply not making sense. A variable in a function is all ready known to be in the "local" scope. A variable that isn't in any function, or code block, would be considered "global" throughout the entire source. Declaring a local variable to be "global" just makes no sense. I don't see why anyone would need, or want, to do that in the first place anyways.

you must declare a GLOBAL on top (before all functions) - not within a FUNC statement to make it global

Actually both functions are right. However, only after calling _MyFunc will $afile be declared globally and become usable throughout the entire source.

Func Add($x, $y)
    Global $answer = $x + $y
EndFunc

Add(100, 200)
MsgBox(0, "", $answer)
Edited by cppman
Link to comment
Share on other sites

@gcue: You STILL haven't posted any code that reproduces your problem... muttley

A Global can be declared inside a function, but it is considered bad style and will generate warnings from the syntax checker. Yet it will work fine if you ignore the warnings:

_GetFile()
_MyFunc()

Func _GetFile()
    Local $message = "Test"
    Global $afile = FileOpenDialog($message, @DesktopDir & "\", "Text Files (*.txt)", 1 + 4)    
EndFunc

Func _MyFunc()
    MsgBox(64, "Results", "$afile = " & $afile)
EndFunc

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

i would start by removing the "function global" - it is not useful to declare them again

Global $pushmenu, $afile

....

Func Enable_RegistrySetting()

$message = "Please choose your assets text file."

Global $afile = FileOpenDialog($message, @DesktopDir & "\", "Text Files (*.txt)", 1 + 4)

Edited by nobbe
Link to comment
Share on other sites

gcue,

It looks like you are destroying the values for $afile by having the lines

$afile = 1

If you have Global $afile declared at the top of your script then there is no need to have Global $afile = ..., just say $afile =.

If a variable is declared as Global then it is Global whether it is inside a function or not . Ignore the nonsense that has been mentioned about a local variable being declared as Global and a global declared inside a function is local.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

heres whats going on...

the user can run the CHECK function to check the current settings on its own which prompts the user for an asset file ($afile).

but if the user runs any of the other functions first, (push a setting, delete a setting, etc) then it will also prompt them for an asset file ($afile). after that function has been run, i want the CHECK function to run after so that way it shows the user the new settings. but by this time the user has already been prompted for an asset file.. theres no need to be reprompted, this is why i set $afile=1.

Link to comment
Share on other sites

i figured a way out to do this easier.

i added Global $afile=1 at the top then at every function i put

if $afile=1 then

chooseTXT()

Endif

thanks for all your help fellas.

=)

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...