Jump to content

Var value lost in Func


Nova
 Share

Recommended Posts

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 !

Link to comment
Share on other sites

  • Developers

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 etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

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 etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

  • Developers

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 etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

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 !

Link to comment
Share on other sites

  • Developers

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 etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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...