Skip to main content
gamedev.net
🔒 Locked

c# casting object [solved]

Started by conman Mar 20, 2005 at 2:45 PM 0 replies 350+ views
Original Post
conman
conman
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:

    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;
    }




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]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.