Original Post
Hi, I have trouble casting in C#. Perhaps this is not allowed at all, but I want to do the following: I have a method that converts a string into a value of the type I provide in the enum PlyDataType. The return type is object to represent all data types: Then I want to use the values and do a cast like this: int numEntities = (int)SelectScalar(numEntityType, stringValue); ...and get a cast exception. I know the method SelectScalar works, the string is converted and the value assigned to obj. But apparently the cast back to an int is not allowed. Any ideas what may be wrong/or why that is not allowed? Constantin [Edited by - conman on March 20, 2005 3:27:10 PM]
private static Object SelectScalar(PlyDataType type, string str)
{
Object obj = null;
switch(type)
{
case PlyDataType.Char:
obj = (char)Convert.ToInt16(str);
break;
case PlyDataType.Int:
obj = (int)Convert.ToInt32(str);
break;
...
}
return obj;
}