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
Solved! Go to Solution.
Solved by lokesh.kalia. Go to Solution.
Solved by Sean570. Go to 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
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
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.
@Anonymous & @lokesh.kalia
Great information to know. I'll make sure to remember that rule.
Cheers,
Can't find what you're looking for? Ask the community or share your knowledge.