Losing a variable defined in an IF statement when the statement is finished

Losing a variable defined in an IF statement when the statement is finished

Anonymous
Not applicable
736 Views
4 Replies
Message 1 of 5

Losing a variable defined in an IF statement when the statement is finished

Anonymous
Not applicable

Hi,

 

Here is the problem:

I am losing the value of a variable I assign in an if statement. I don't know why this would happen.

See example:

 

INT test = INPUT "enter a number"

 

if test==5 {

string number = "the number is 5"

}

 

 

As soon as i get out from that if I lose the value number. What am I doing wrong here?

 

Thanks,

Marc

0 Likes
Accepted solutions (3)
737 Views
4 Replies
Replies (4)
Message 2 of 5

Sean570
Advocate
Advocate
Accepted solution

I don't think you are losing the number value, just the string value.

try something like:

INT test = INPUT "enter a number"
string number = ""
if test==5 {
    $number = "the number is 5"
}

// send message to make sure value sticks
message info $number 
0 Likes
Message 3 of 5

Anonymous
Not applicable
Accepted solution

I found the problem:

 

I was defining the variable in the if statement:

 

Resolution:

define the variable outside

update the variable in the if statement

 

Thank you all

Message 4 of 5

lokesh.kalia
Autodesk
Autodesk
Accepted solution

Hi @Anonymous Yes, the curly brackets {} define the scope of any variable declared within them. That is, the variable is destroyed after the closing } bracket. As you've said, for a variable to be used both within the scope defined by the pair of curly brackets and afterwards, it must be defined before that scope begins.



Lokesh Kalia
Senior Manager, Software Development
Message 5 of 5

ChristopherMarion
Advisor
Advisor

@Anonymous & @lokesh.kalia 

 

Great information to know.  I'll make sure to remember that rule.

 

Cheers,

Christopher Marion
Technical Specialist - CAM
SolidCAD - Canada





0 Likes