Converting string to property type

A forum devoted to the discussion of all topics having to do with scripting and other advanced programming using iX Developer.
Post Reply
bjornspockeli
Posts: 4
Joined: Tue Jun 18, 2013 12:33 am

Converting string to property type

Post by bjornspockeli »

I have a list of tags with values of the type Float, bool ,int16 etc. and i want to convert the value(string) to the appropriate type of the tag. However i get an error when i try to set myTag.Value to the converted value. Am i
missing something?

private GlobalDataItem GetGlobalDataItem(string propertyName)
{
PropertyInfo tagProperty = typeof (Neo.ApplicationFramework.Generated.Tags).GetProperty(propertyName);
if(tagProperty == null)
return null;
else
return tagProperty.GetValue(Globals.Tags, null) as GlobalDataItem;
}


var myTag = GetGlobalDataItem(tagName);

PropertyInfo tagProperty = typeof (Neo.ApplicationFramework.Generated.Tags).GetProperty(tagName);

var tagVal = Convert.ChangeType(value, tagProperty.PropertyType,null);

myTag.Value = tagVal;

User avatar
Edmund
Posts: 92
Joined: Thu Nov 29, 2012 2:27 pm

Re: Converting string to property type

Post by Edmund »

How about using:

Code: Select all

	Globals.Tags.myBool.Value = Convert.ToBoolean("false");
	Globals.Tags.myFloat.Value = Convert.ToDouble("994.4");
	Globals.Tags.myInt.Value = Convert.ToInt16("30");
Edmund Andersson

AITECH AB

Part of Beijer Integrator Group

Post Reply