Assigning a variable to another variable


#1

Is there a way to assign a variable to another variable? I 'm not talking about the value of the variable but the variable itself.

Here is what I’m trying to do.
variables = iLeft_Virtual_Limit_Switch
iRight_Virtual_Limit_Switch
Home_Virtual_Switch
X_Axis_Home_Position

If (X_Axis Home_Position == Left) then
Home_Virtual_Switch = iLeft_Virtual_Limit_Switch;
EndIf

If (X_Axis Home_Position == Right) then
Home_Virtual_Switch = iRight_Virtual_Limit_Switch
EndIf

Is this something that can be done with pointers?


#2

Yes! In fact, that’s what’s wonderful about pointers!

Assuming I’m understanding your situation correctly, I think you’d want Home_Virtual_Switch to be the pointer here. In other words, sometimes it will “point” to one variable, and sometimes another, like this (it’s also common to put a ‘p’ in front of your pointer variable names):


 
If (X_Axis_Home_Position == Left) then

  pHome_Virtual_Switch = &iLeft_Virtual_Limit_Switch;

EndIf

If (X_Axis_Home_Position == Right) then

  pHome_Virtual_Switch = &iRight_Virtual_Limit_Switch;

EndIf

Check out the section in Form 1700, the PAC Control Users Guide, about “Understanding Pointers” which starts on p. 298. (It’s also the first option in PAC Control under Help > Manuals.)

Also handy, this OptoScript sample strategy which includes a “Pointers” block (top-center of chart). I’ll include a picture below of that block.

Notice when you use pointers, you’ll see some [B]&[/B] and [B]*[/B] characters in there. The way I remember which one to use is to think of ‘a’ for ‘ampersand’ and ‘[B]address[/B]’. As shown in my example above, you want the pointer to point to the ADDRESS (in memory) of your variable (vs. the value stored at that address).

The [B]*[/B] means, ‘[B]what this points to[/B].’ I remember this by thinking of an arrow (which points) from the back/end, where it has the feathers (fletchings?) sticking out in the shape of a * if you get what I’m saying here.

So in your code, when you later want to do something with this variable that pHome_Virtual_Switch points to, you do what we call “de-referencing” (get the value that this references) by adding * at the front to say “what this thing points to.”

Hope that all makes sense!

-OptoMary



#3

Thanks OptoMary,

I’ll give this a try. I must say, I am having so much fun with your software. It’s truly liberating when you’re coming from ladder and basic. Many thanks to all of you at Opto 22.

Andre