Community
PowerMill Forum
Welcome to Autodesk’s PowerMill Forums. Share your knowledge, ask questions, and explore popular PowerMill topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

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

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
Anonymous
544 Views, 4 Replies

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

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

Labels (1)
4 REPLIES 4
Message 2 of 5
Sean570
in reply to: Anonymous

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 
Message 3 of 5
Anonymous
in reply to: Sean570

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
in reply to: Anonymous

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

@Anonymous & @lokesh.kalia 

 

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

 

Cheers,

Christopher Marion
Technical Specialist - CAM
SolidCAD - Canada





Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report