RobotStudio event

use string for referencing to variable


Hey to all,

Sorry for this stupid question. I am really new to that stuff. First of all: YES, I tried to search the help, much more longer than a few minutes :-)

minimal example:

-------------------------------

!three position variables
CONST pos position_1 := [10, 10, 10];
CONST pos position_2 := [1, 1, 1];
VAR pos position_3;

!string containing variable names
CONST string positions := ["position_1","position_2"]

!want to assign position from string
position_3 := positions{2};

------------------------------

---> doesn't work, because "position{2}" is a string and not a position

I hope there is an easy solution :-)

thanks
RockOn





RockOn_232012-11-03 12:54:15

Comments

  • You will need to create a function that returns the data that the string identifies. Be sure to handle the error handler correctly for your application to avoid using "not found" data, etc. You can decide where to handle the error for error handling, either in the function itself or the calling part of the program... In its simplest form, you could replace your assignment with GetDataVal positions{2},position_3; - but this does not lend itself to handling any error(s) if you need too - or how to handle multiple "GetDataVal" Instrucitons in one routine for example...
     

    !three position variables

    CONST pos position_1 := [10, 10, 10];

    CONST pos position_2 := [1, 1, 1];

    VAR pos position_3;

    !string containing variable names

    CONST string positions{2} := ["position_1","position_2"];

     

    !want to assign position from string

    !position_3 := positions{2}; Error 101: Type error: Type mismatch of expected type pos and found type string instead, use: 

    position_3:=fPOSbyName(positions{2});


    !Check for valid return value here before using

    TPWrite "pos "+positions{2}+":="pos:=position_3;

     

    FUNC pos fPOSbyName(string posName)

      VAR pos value:=[0,0,0];

      GetDataVal posName,value;

      return value;

    ERROR

      TPWrite "ERROR! " + posName + " NOT FOUND!";

      TryNext;

      ENDFUNC

     

     
    Thomas J2012-11-05 15:47:03
    Thomas H. Johnston
    PACs Application Engineer