Sign in to follow this  
Followers 0
DarkBoost

"Variable used without being declared" (N00b)

18 posts in this topic

I have a script running which is throwing me a message: Variable used without being declared

I am still new to programming and its methodolical rules, but slowly getting there.

It simply goes:

If $A = $B Then

$C = 1

EndIf

MsgBox(0,"","The Number is "&$C)

Share this post


Link to post
Share on other sites



I have a script running which is throwing me a message: Variable used without being declared

I am still new to programming and its methodolical rules, but slowly getting there.

It simply goes:

If $A = $B Then

$C = 1

EndIf

MsgBox(0,"","The Number is "&$C)

That doesn't even make sense. What's the value of B?

and you have to declare your variables, look up Local, Dim, Global


children may smile; the wise ponder- Dr. Holmes of Hardvard Medical School on an Ether BingeLove Makes The World Go Round?So does five shots of tequila. What's your point?[quote name='Valik' date='Jun 5 2008, 05:13 PM']wraithdu, 24 hours. Said I have a bad attitude, just driving the point home with a ban.[/quote]This is classic. :)

Share this post


Link to post
Share on other sites

That doesn't even make sense. What's the value of B?

and you have to declare your variables, look up Local, Dim, Global

It doesn't matter what $A and $B are its irrelavant to the example... doesn't matter, will check the help file again.

Thanks anyways.

Share this post


Link to post
Share on other sites

Yes, variables are very relevant. If you don't post code that shows the problem, how do you expect us to fix it?

Share this post


Link to post
Share on other sites

I have a script running which is throwing me a message: Variable used without being declared

I am still new to programming and its methodolical rules, but slowly getting there.

It simply goes:

If $A = $B Then

$C = 1

EndIf

MsgBox(0,"","The Number is "&$C)

It means that your variables have to be equal to something before you start using them.

$A = 1
$B = 2

If $A = $B Then
$C = 1
EndIf

MsgBox(0,"","The Number is "&$C)

If they aren't set to anything how does the program know if $A = $B?


Other People's Stuff:Andy Flesner's AutoIt v3: Your Quick Guide[topic="34302"]Locodarwin's ExcelCom_UDF[/topic][topic="61090"]MrCreatorR's Opera Library[/topic]

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

My apologises.

I have given the appearance that I do not understand variables due to not showing what $A and $B is declared as in my original example, my bad!

Let me rephrase:

Can you declare a variable during an IF statement? example:

If FileExists("c:\example1.txt") Then
$var = "red"
EndIf

If FileExists("c:\example2.txt") Then
$var = "blue"
EndIf

If FileExists("c:\example3.txt") Then
$var = "green"
EndIf

MsgBox(0,"","The colour is now "&$var); only 1 of the above files will ever exist

From muncherw's post, obviously No it needs to be declared outside the If Statement.

However the above works on Windows XP but fails on Vista 64bit with my original post: Variable used without being declared

Is there better way to write this, is this something that array or loop might be useful for?

Edited by DarkBoost

Share this post


Link to post
Share on other sites

Let me rephrase:

Can you declare a variable during an IF statement? example:

Yes you absolutely can do that.


Other People's Stuff:Andy Flesner's AutoIt v3: Your Quick Guide[topic="34302"]Locodarwin's ExcelCom_UDF[/topic][topic="61090"]MrCreatorR's Opera Library[/topic]

Share this post


Link to post
Share on other sites

However the above works on Windows XP but fails on Vista 64bit with my original post: Variable used without being declared

Maybe it has something to do with Vista. *shrug*


Other People's Stuff:Andy Flesner's AutoIt v3: Your Quick Guide[topic="34302"]Locodarwin's ExcelCom_UDF[/topic][topic="61090"]MrCreatorR's Opera Library[/topic]

Share this post


Link to post
Share on other sites

Yes you absolutely can do that.

Thats what I thought and this works with Windows XP 32bit and Windows Vista Ultimate 32bit

but now failing with Windows Vista Ultimate 64bit it this an incompatible issue with AutoIT and Vista Ultimate 64bit?

Share this post


Link to post
Share on other sites

No, you are probably doing something wrong. AutoIt works on Vista. Vista just doesn't work.

Share this post


Link to post
Share on other sites

Thats what I thought and this works with Windows XP 32bit and Windows Vista Ultimate 32bit

but now failing with Windows Vista Ultimate 64bit it this an incompatible issue with AutoIT and Vista Ultimate 64bit?

There shouldn't be a "incompatible issue". Have you tried the things noted in the helpfile? Things like=

Did you run the script as administrator?

Used the #requireadmin?

Disabled UAC?

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

There shouldn't be a "incompatible issue". Have you tried the things noted in the helpfile? Things like=

Did you run the script as administrator?

Used the #requireadmin?

Disabled UAC?

Or there's the obvious question. Do the files exist?! It's obvious that with his code, the variables are only declared if the files exist I.E, no "Else" statement.

EDIT:

AutoIt works on Vista. Vista just doesn't work.

Amen.

Edited by KentonBomb

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

I have run the script as an Administrator and tested with the #RequireAdmin included and UAC has always been disabled.

To my understanding, the IF statement should always default as TRUE unless specified, so if the FileExists = True then

it should run whatever is declared in the Then statement otherwise it will EndIf. Please let me know if this is not correct.

I have attached a screenshot of the code including the exact error (maybe this will help?)

Update: This is failing for both Windows Vista Ultimate 32bit and 64bit... this is only working for Windows XP 32bit

Any ideas?

Edited by DarkBoost

Share this post


Link to post
Share on other sites

I have run the script as an Administrator and tested with the #RequireAdmin included and UAC has always been disabled.

To my understanding, the IF statement should always default as TRUE unless specified, so if the FileExists = True then

it should run whatever is declared in the Then statement otherwise it will EndIf. Please let me know if this is not correct.

I have attached a screenshot of the code including the exact error (maybe this will help?)

Update: This is failing for both Windows Vista Ultimate 32bit and 64bit... this is only working for Windows XP 32bit

Any ideas?

I'm also a total AI beginner and I don't know anything about Vista but that said... If you are doing these If statements and say number 2 is true, then if it gets to number 3, then it's false again. Does AI have a Case or Select statement? From other languages I've used, you need to drop out of the eval mode when a statement is true.

I'm running into the undeclared variable thing using old code with latest AI, so if you are using any includes, maybe that's where the problem is?

Terry

Share this post


Link to post
Share on other sites

Let me rephrase:

Can you declare a variable during an IF statement? example:

If FileExists("c:\example1.txt") Then

$var = "red"

EndIf

If FileExists("c:\example2.txt") Then

$var = "blue"

EndIf

If FileExists("c:\example3.txt") Then

$var = "green"

EndIf

MsgBox(0,"","The colour is now "&$var); only 1 of the above files will ever exist

Someone said ABSOLUTELY YES. So what happens when every condition fails? The state of $var is...

Share this post


Link to post
Share on other sites

I thought that if an If statement which only includes Then would skip the Then if it renders False?

Example:

If FileExists("c:\Example1.txt") Then; This file does not exist so statement = False
$var = "red"; This line is never read because the above statement  = False
EndIf

If FileExists("c:\Example2.txt") Then; This file does exists so statement = True
$var = "blue"; This line is read because the above statement = True
EndIf

MsgBox(0,"","The Colour is now "&$var); The answer would be 'blue' because $var was declared in the second IF statement as it rendered True

Sorry guys, I am just trying to understand how AutoIt works.

Share this post


Link to post
Share on other sites

Correct. If the statement is false, the block is not executed. You must always handle the chance that it isn't true though. Just say $var = "" before the blocks, that way you always know it's declared, in the case that none of your files exist. Applied to this example anyways.

Share this post


Link to post
Share on other sites
Richard Robertson This makes sense, thanks!

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