Jump to content

create a new variable inside an If statement?


frew
 Share

Recommended Posts

Hello,

Why does $y3 = $y2 - 100 not create the variable $y3 in the If statement below?

Variable not declared error comes about apparently because I try to create a new variable

inside an If statement, like this, for example:

If $y2 < $y1 Then
    $y3 = $y2 - 100   ;this does not create the variable $y3 as I thought it would
EndIf

If $y3 > 716 Then   ;error says that this variable is trying to be used without being declared
    $y3 = 716
EndIf

Solution: I put

Dim $y3

at the top of the script. (but I didn't think that was supposed to be necessary, because generally I have been able to

just create variables as I go along anywhere in the script.)

Thanks for any ideas about this.

frew

Edited by frew
Link to comment
Share on other sites

Maybe because it is not ABSOLUTE that $y2 < $y1. If not then the next If/test is invalid because $y3 was never created

Ah, yes...thank you Valuater, that makes sense.

I guess then it's a good rule of thumb to not try to create variables inside If statements.

Okay, if I do If statements like this, I guess I just have to be sure to declare any variables that may be created inside the Ifs

outside the Ifs. Like with a Dim above the If statement.

Thank you very much,

frew

Link to comment
Share on other sites

As I understand it is better to use Global rather than Dim per Dev's here.... and remember to use Local rather than Global for example inside a function.

That's good to know, thanks.

I see so much Dim around in much code examples, I thought maybe that was the way to go.

Okay, Global and Local...Global for when a variable's value is the same throughout the script, and Local for when the variable's value may change throughout the script...I think that's the idea.

I'll study that in the help I guess...because I may be mixing that up with Constants, sorry.

Thanks, I'll look into that.

frew

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