Sign in to follow this  
Followers 0
BOUNCER

How to use declared variables with IF ...

5 posts in this topic

im tangled up with this declaring variable stuff...

i want to make a function that has all the variables declared...and im having a hard time with it

Func disconnectcheck()
If $disconnectcheck <> "" Then
GUICtrlDelete($Pic1)
$connect2onlinelogo = GUICtrlCreatePic("connect2online.gif", 24, 16, 243, 258)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFFFF00)
GUICtrlSetBkColor(-1, 0xFF0000)
GUICtrlSetOnEvent(-1, "disconnectClick")
Else
$disconnectcheck = IniRead("config.ini", "blah", "blah2", "")
$Pic1 = GUICtrlCreatePic("connect2offline.gif", 24, 16, 243, 258)   
EndIf
EndFunc

simple if statements...except it has variables on both sides that cant see each other

On both sides of the IF and ELSE statements the variables are declared...i cant seem to organize it in a way so that the variables are declared on both sides...

my other problem is that

$Pic1 = GUICtrlCreatePic("connect2offline.gif", 24, 16, 243, 258)
for example actually CREATES the image, so what the hell? if i do
Global $Pic1 = GUICtrlCreatePic("connect2offline.gif", 24, 16, 243, 258)
or
Global $disconnectcheck = IniRead("config.ini", "blah", "blah2", "")
itll just EXECUTE those functions...

whats the point of declaring a variable like $pic1 if it actually creates the picture, how am i suppose to use it after its declared

Share this post


Link to post
Share on other sites



Simply, don't declare variables with IF. Declare Global vars at the top of the script and local vars at the start of functions then use them. By doing that you won't have to worry about scope, its good practice.

Add Opt("MustDeclareVars", 1) to the very top of your script. If you have more issues post all your code.


Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

so if i do

Global $Pic1 = GUICtrlCreatePic("connect2offline.gif", 24, 16, 243, 258)

how would i use $Pic1 again...how do you use variables after their declared

Edited by BOUNCER

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

The way I understand it is, If you globally declare a variable, it is available throughout your entire script

Global $var = "blue"

Func myfunc()
   If $var <> "blue" Then
      do whatever
   Else
      do something different
   EndIf
EndFunc

However if you want to manipulate the global variable from within a function, start reading the help file, first stop "ByRef"

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

how would i use $Pic1 again...how do you use variables after their declared

Exactly how JohnOne demonstrates.

The way I understand it is, If you globally declare a variable, it is available throughout your entire script

Global $var = "blue"

Func myfunc()
   If $var <> "blue" Then
      do whatever
   Else
      do something different
   EndIf
EndFunc

However if you want to manipulate the global variable, start reading the help file, first stop "ByRef"

That is correct, not going to explain scope, the help file does it to some degree though - see Dim. Edited by bo8ster

Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

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  
Followers 0