casting from object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Karl Meissner

    #1

    casting from object

    I am having problem casting from object. I want to pass in an
    value type as an object and then store it as a particular type.
    The cast from object throws an exception if the types do not match.


    class test {
    short x;

    // o can be any of the value types int, long, float
    public void Set( object o ) {
    x = ( short ) o; /// thows an exception if o is an int
    }
    public void Set2( object o ) {
    int i = ( int ) o;
    x = ( short ) i; /// works
    }

    }

    ....
    test t = new test();
    int j = 42;
    t.Set2( j ); // good
    t.Set( j ); // bad

    Why wont the cast in Set work?
    What good is object if I need to know what freaking type it is to use it?
    Is there a stronger cast?

    I suppose I could write a long if-else-if that test all the possible types
    of the object o and cast it to the input o type and then cast that to
    short but that is pretty lame.

    Any ideas?
    Also I am not allowed to use generics. Sigh....



    Karl
  • Jonathan Schafer

    #2
    Re: casting from object

    Try Convert.ChangeT ype

    Jonathan Schafer

    On 28 Jul 2003 07:57:57 -0700, meissnersd@yaho o.com (Karl Meissner)
    wrote:
    [color=blue]
    >I am having problem casting from object. I want to pass in an
    >value type as an object and then store it as a particular type.
    >The cast from object throws an exception if the types do not match.
    >
    >
    >class test {
    > short x;
    >
    > // o can be any of the value types int, long, float
    > public void Set( object o ) {
    > x = ( short ) o; /// thows an exception if o is an int
    > }
    > public void Set2( object o ) {
    > int i = ( int ) o;
    > x = ( short ) i; /// works
    > }
    >
    >}
    >
    >...
    >test t = new test();
    >int j = 42;
    >t.Set2( j ); // good
    >t.Set( j ); // bad
    >
    >Why wont the cast in Set work?
    >What good is object if I need to know what freaking type it is to use it?
    >Is there a stronger cast?
    >
    >I suppose I could write a long if-else-if that test all the possible types
    >of the object o and cast it to the input o type and then cast that to
    >short but that is pretty lame.
    >
    >Any ideas?
    >Also I am not allowed to use generics. Sigh....
    >
    >
    >
    >Karl[/color]

    Comment

    • Chris Hornberger

      #3
      Re: casting from object

      If I had to guess, I'd say it's because of some pre-cast bounds or
      address or address-size checking or something along those lines.

      Personally I'd use a cascading thing like you're doing in Set2 anyway,
      start with a double -> int -> short, as an example.

      At least that way, you're always guaranteed to get a return value, if
      not boxed up in the type you're expecting, or you'd be able to stack
      trace the failures and log your problems.

      meissnersd@yaho o.com (Karl Meissner) wrote in message news:<463b8cae. 0307280657.6684 950a@posting.go ogle.com>...[color=blue]
      > I am having problem casting from object. I want to pass in an
      > value type as an object and then store it as a particular type.
      > The cast from object throws an exception if the types do not match.
      >
      >
      > class test {
      > short x;
      >
      > // o can be any of the value types int, long, float
      > public void Set( object o ) {
      > x = ( short ) o; /// thows an exception if o is an int
      > }
      > public void Set2( object o ) {
      > int i = ( int ) o;
      > x = ( short ) i; /// works
      > }
      >
      > }
      >
      > ...
      > test t = new test();
      > int j = 42;
      > t.Set2( j ); // good
      > t.Set( j ); // bad
      >
      > Why wont the cast in Set work?
      > What good is object if I need to know what freaking type it is to use it?
      > Is there a stronger cast?
      >
      > I suppose I could write a long if-else-if that test all the possible types
      > of the object o and cast it to the input o type and then cast that to
      > short but that is pretty lame.
      >
      > Any ideas?
      > Also I am not allowed to use generics. Sigh....
      >
      >
      >
      > Karl[/color]

      Comment

      Working...