How to address a variable name by combining strings

Find and share HowTos to various installations / configurations!
6 posts • Page 1 of 1
Kirvesmies
Posts:42
Joined: Mon Dec 04, 2017 2:28 pm

How to address a variable name by combining strings

Post by Kirvesmies »

This is just a stupid example, but how can I get the code to understand I want to set fValue1 value to 123.4?

Code: Select all

float fValue1;
int i=1;
("fValue"+i) = 123.4;  //One cannot point to a variable by combining texts it seems

EER
Posts:17
Joined: Fri Jun 12, 2015 1:31 pm

Re: How to address a variable name by combining strings

Post by EER »

I think its not possible.

Something that looks like your example:

Code: Select all

  dyn_float dfValues;
  
  int i =1;

  dfValues[i] = 123.4;
But this does not compose a variable name using strings. In most cases you don't need to do this anyway.

I don't know what problem you try to solve but try to use lists or maps to structure the data.
Avoid defining variable references during runtime. In general this will make the code hard to read and predict.

dvribeira
Posts:24
Joined: Mon Mar 18, 2019 11:53 am

Re: How to address a variable name by combining strings

Post by dvribeira »

I will not get to rewriting what you want, but to give you a hint, you can generate a script, i.e. write it to a string, and then execute it with evalScript() or execScript().

leoknipp
Posts:2928
Joined: Tue Aug 24, 2010 7:28 pm

Re: How to address a variable name by combining strings

Post by leoknipp »

The example given by "EER" is to read an entry in a dyn_array. It does not compose a variable name.

With the function setVariable()/getVariable() you can probably do what you want.
But as mentioned using dynamic variable names must not be used.

Best Regards
Leopold Knipp
Senior Support Specialist

Kirvesmies
Posts:42
Joined: Mon Dec 04, 2017 2:28 pm

Re: How to address a variable name by combining strings

Post by Kirvesmies »

Thanks guys. Problem solved. I did not know you can use dyn_variables in the callback function parameters so here is the old and new dpConnect:

OLD:

Code: Select all

dpConnect("WS_CB", TRUE,  $dpe+".fValue1:_online.._value",
	$dpe+".fValue2:_online.._value",
	$dpe+".fValue3:_online.._value"....");
}
WS_CB(string dpe1, float fValue1,
	string dpe2, float fValue2,
	string dpe3, float fValue3...,)
{      		
dyn_float fValue;
 for(int i=1; i<10; i++)
 	{
 	fValue[i] = ("fValue"+i); //No good
 	}
}
 

NEW:

Code: Select all

dpConnect("WS_CB", TRUE,  $dpe+".fValue1:_online.._value",
	$dpe+".fValue2:_online.._value",
	$dpe+".fValue3:_online.._value"....");
}
WS_CB(dyn_string dpe, dyn_float fValue)  //Good
{      		
...
}

leoknipp
Posts:2928
Joined: Tue Aug 24, 2010 7:28 pm

Re: How to address a variable name by combining strings

Post by leoknipp »

If you are defining the dp elements for the dpConnect() element by element do not pass a dyn_string to the dpConnect() it is possible better and easier to read when you do not use dyn_arrays in the work function.
In the work function you see then directly which dp elements and which value belongs to which dp elements in the dpConnect() call.

Best Regards
Leopold Knipp
Senior Support Specialist

6 posts • Page 1 of 1