Page 1 of 1

Converting string to property type

Posted: Tue Jul 09, 2013 3:57 am
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;

Re: Converting string to property type

Posted: Tue Jul 09, 2013 4:49 am
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");