Jump to content
Sign in to follow this  
Nova

Var value lost in Func

Recommended Posts

Nova

Ok I have a bit of a problem here !

In the below code assume the function Input has been called by earlier code (Im not going to post it allunless its totally nessary because its huge)

Ok the password entered by the user is stored into the varible $password like so

$password=GUIRead($Input)

Then a hotkey is set to enter which when pressed checks if the password is correct.

Heres where the problem arises becasue $password=GUIRead($Input) is inside 1 function its value gets deleted when that function ends and it ends in order for the nxt function the password check to take place.

It appears that I need a Dim Global $password=GUIRead($Input)

but this dosent seem possible to the best of my knowledge !

Func Input()
   GUICtrlSetState ( $Logon, $GUI_SHOW )
   $Input = GUICtrlCreateInput ( "", @DesktopWidth/2-25,  @DesktopHeight/2 + 10, 300, 25,$ES_PASSWORD)
   GUICtrlSetState ( $Input, $GUI_FOCUS )
   HotKeySet("{ENTER}", "Enter2") 
   $password=GUIRead($Input)
EndFunc 
      
Func Enter2()
   
   if $password = ("correct") then
         SoundPlay("C:\Nova-Menu\Sounds\Start.wav")
         ToolTip ( "Password correct, Welcome friend !" , 550, 450)
         sleep (2000)
         ToolTip ( "" , 550, 450)
   else
         SoundPlay("C:\Nova-Menu\Sounds\End.wav")
         ToolTip ( "Password incorrect, plz try again !" , 550, 450)
         sleep (2000)
         ToolTip ( "" , 550, 450)
   endif
EndFunc

Can anyone advise ???

If my probelm isnt apparent from this section of code plz say so and ill post the full script !

Share this post


Link to post
Share on other sites
Nova

I understand the problem :) I just cant figure out a solution !

Share this post


Link to post
Share on other sites
Jos

I understand the problem  :) I just cant figure out a solution !

<{POST_SNAPBACK}>

This is a "contradiction in termino" ... :)

Just do a Global $password at the top of your program to make the variable available in the whole program without doing a Dim/Local?Global statement for it in the Func.


SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
Jos

SCOPE... Global and Local

@DEVS: Is this true for loops too?

<{POST_SNAPBACK}>

The scope for a Local is the Func .. not loops...

heres the definitions from the helpfile:

Dim = Local scope if the variable name doesn't already exist globally (in which case it reuses the global variable!)

Global = Forces creation of the variable in the Global scope

Local = Forces creation of the variable in the Local/Function scope


SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
Nova

Just do a Global $password=GUIRead($Input)

at the top of your program

I cant because $Input isnt defined untill the function input is called !

Share this post


Link to post
Share on other sites
db26

Also, you can have the Input() function return $password, and the Enter2() function take $password as a parameter.

However, this may not work in the context of your program. I can't really tell from what you've posted. I think it should though...

Share this post


Link to post
Share on other sites
Jos

I cant because $Input isnt defined untill the function input is called !

<{POST_SNAPBACK}>

lost me here, don't see any reason not being able to do:

Global $password

at the beginning of your script and in a func do :

$password=GUIRead($Input)

EDIT: By the way ... its better to quote what is really there, not what you read into it.... :)

Edited by JdeB

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
Nova

lost me here, don't see any reason not being able to do:

Global $password

at the beginning of your script and in a func do :

$password=GUIRead($Input)

A thousand appologies JdeB,I tought that in order to make a varible Global you had to set a value to it !

like so

Global $password = 'whatever'

Ive got my script working now !

Tnx everyone for the help !

Share this post


Link to post
Share on other sites
Jos

A thousand appologies JdeB,I tought that in order to make a varible Global you had to set a value to it !

like so

Global $password = 'whatever'

Ive got my script working now !

Tnx everyone for the help !

<{POST_SNAPBACK}>

no problem... :)

It is a good practice the have a section at the top of your program which defines all variables with a global scope and also set the initial value just in case.

So you could do that for your $password variable like:

Global $password = ''


SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
ezzetabi

and it is a good habit also using for 'more than 4 lines scripts'

Opt('MustDeclareVars',1)

It avoids silly but difficult to find mispelling errors and force you give a scope for every variant.

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  

×