evaluation of strings in external inputs doesnt work correctly

 

Hello, can anyone give me an advice for a very special SW-problem with external input parameters:


We are runnung an EA with external input parameters at the normal input-parameter menue..
This parameters can be changed at any time . The parameter changes are read by the EA and the EA responds to these changes.

extern int iNumeric = 1;
extern string strText = "X";



int iNumericCopy=0;
string strTextCopy ="";

bool boNumericChanged=False;
bool boTextChanged=False;

int init()
{

  ..some code to select InitReason "Parameter changed"
  //at this point, the actual content of the external input params can be read

  //Show the old content of the parameter
  Print(iNumericCopy);//With numeric values, this works well,here we see the old value
  Print(strTextcopy); // this variable has already the content from the input-section, this cnnot be possible!

  if( iNumericCopy!= iNumeric )
  {
     //the value has changed,do something
     //This is ok, changes are recognized
     boNumericChanged=True;
  }
  iNumericCopy=iNumeric;

  if(strTextCopy != strText)
  {
    boTextChanged=True; //This is never reached
  }
  strTextCopy=strText;

  
  return(0);
}


int deinit()
{
  //Show the latest content of the parameter just bevoer reinit
  Print(iNumericCopy); // leave program with actual value,this is ok
  Print(strTextCopy); // leave program with actual value, the value "seems" to be OK
  return(0);
}

Start()
{
  //do something by reading iNumericCopy and strTextcopy and Changed Flag
  Print( boTextChanged);
  Print( boNumericChanged);

  boNumericChanged=False;
  boTextChanged=False;

  ..some remaining code

  return(0);
}


This codefragment shows a strange problem with strings.
Normally, changes at the parameter-Input results in a deinit() and a Re-Init() with the Reason "Parameter changed"
This is done by the Init() Function.
Changes are recognised by comparing the old copy with the he new value taken from the external inputs.
This does not work with strings.
At the beginning of the init() function, the Copy of a string has the new value of the external input, but the assignment was never done in the program.
So maybe, there is a stackproblem or any hidden recursion or a threading problem.
Please let me know, if this programming strategy is not legal.
For me, it seems, that under some conditions, a stringcopy doesnt move the content, but the address of the stringvaraiable,in a manner that is used in C++ for references to objects.

Under normal program conditions, strings can be copied from one variable to another variable by assignment, the destination variable is not an alias name. In case of an extern input, the destination variable turns to an alias of the copied variable ???
The only workaround at the moment is, to avoid Terms like: strA =extInputstringB;

Using the construct strA =StringConcatenate(extInputstringB,"") works well.

So I have moved the codefragment to the codesection for better reading.

Thank you, your Rudi Ratlos

 

Please . . .

 

I figured it out!!!

I was having the same problem at you- I was like, how about that?! I've found a glitch in the matrix! LOL

So case in point:

Say you have the variables:

extern string Badger="I get what I want.";

string CopyOfBadger;

Then somewhere in the code:

CopyOfBadger=Badger;

Whenever you change the extern string Badger, it then assigns: string CopyOfBadger - the same thing. As if it were, like you said, an alias instead of just a copy of the text itself.

I am no programmer, and it is possible that this is by design. I wouldn't assume so, but I was glad I ran into this post so I knew I wasn't alone. But none-the-less I NEEDED for this not to happen and a solution around it.

The reason for this is that I needed to be able to compare strings if the user changed a the variable in the EA properties:

If(Badger!=CopyOfBadger)

        {

        Print("You changed the string cool guy.");

        CopyOfBadger=Badger; 

        } 

SO, I came up with a very simple solution:

CopyOfBadger=StringSubstr(Badger,0,StringLen(Badger));

This in turn copies the text instead of assigning an alias. Hope this helps you and someone in the future. Sorry if this is common sense, but obviously people run into this. :)

 
  1. I'd count this as a bug in the terminal code.
  2. CopyOfBadger=StringConcatenate(Badger);