__value enumeration casting woes! (InvalidCastException) 
Author Message
 __value enumeration casting woes! (InvalidCastException)

Does anyone know how to "unbox" an enumeration type in MC++???  I thought it
simply was using dynamic_cast or __try_cast but neither work.  No matter
what I do, I either get an InvalidCastException or an internal compiler
error.   I have a class that implements the IComparer interface in MC++, see
example below:

namespace Test
{
  public __value EnumColor
  {
    Red,
    Green,
    Blue
  };

  public __gc class Comparer : public IComparer
  {
    public:
      int Compare(Object *x, Object *y)
      {
        Type *type = x->GetType();
        // Displays Test.EnumColor
        Debug::Writeline(type->ToString());
        // Displays True!!!
        Debug::Writeline((type == __typeof(EnumColor)).ToString());

  // This throws an exception InvalidCastException...
        EnumColor __gc *xx = dynamic_cast<EnumColor __gc *>(x);

  // As well as this...
        EnumColor xx = *dynamic_cast<EnumColor __gc *>(x);

         // If I try this, I get an internal compiler error
  __box EnumColor __gc *xx = dynamic_cast<__box EnumColor __gc *>(x);

  return 0;
       }
    };

Quote:
}

The class is used to sort an Array of enumerations.  The problem is that
when this class is used in C#, I always get an InvalidCastException during
execution.  The array is also defined and constructed on the C# side.  The
code looks like this:

protected static Array sm_arrayColors;

...
{
  sm_arrayColors = Enum.GetValues(typeof(EnumColor));
  Array.Sort(sm_arrayColor, null, 0, sm_arrayColor.Length, new
Test.Comparer());

Quote:
}

The enumeration is defined on the MC++ side...  Anyway, I can't understand
why this is happening.  I go as far as to compare the types (as shown in the
above example) and they equate to true.  The other thing is even if this was
an invalid conversation isn't dynamic_cast suppose to return a null
reference instead of throwing an exception??    Inside the de{*filter*} I can
see that the objects x and y do point to the correct values.   I also
thought that maybe I had to use the __box keyword somehow, but this doesn't
work either, and results in a compiler internal error.   I can't just do an
EnumColor(x) either, because this results in a syntax error.  So does anyone
know what in the world is going on?

Note:  I quickly copied the example above from a much larger piece of code
of mine, so forgive me it there are a few typos.

Thanks,
James C. Papp
Guy Carpenter, Inc.
(Note: My e-mail will block all incoming e-mail messages unless prior
arrangements are made.)



Sat, 15 Jan 2005 03:03:44 GMT  
 __value enumeration casting woes! (InvalidCastException)
FYI,

I just submitted a similar bug to MS a few days ago. You
cannot unbox a 'bool' in MC++ -- you get a runtime
InvalidCastException.

The following code always fails:

void main()
{
   // It works for ints...

   __box int *intBox = __box(42);
   int i = *intBox; // works fine (i gets value '42')

   // ... but not for bools

   __box bool *box = __box(false);
   bool b = *box;  // error here

Quote:
}

They have not provided a resolution yet. For now I use
System::Convert::ToBoolean(box).

Best of luck.

--Bob

Quote:
>-----Original Message-----
>Does anyone know how to "unbox" an enumeration type in

MC++???  I thought it
Quote:
>simply was using dynamic_cast or __try_cast but neither
work.  No matter
>what I do, I either get an InvalidCastException or an
internal compiler
>error.   I have a class that implements the IComparer

interface in MC++, see
Quote:
>example below:

>namespace Test
>{
>  public __value EnumColor
>  {
>    Red,
>    Green,
>    Blue
>  };

>  public __gc class Comparer : public IComparer
>  {
>    public:
>      int Compare(Object *x, Object *y)
>      {
>        Type *type = x->GetType();
>        // Displays Test.EnumColor
>        Debug::Writeline(type->ToString());
>        // Displays True!!!
>        Debug::Writeline((type == __typeof

(EnumColor)).ToString());

- Show quoted text -

Quote:

>  // This throws an exception InvalidCastException...
>        EnumColor __gc *xx = dynamic_cast<EnumColor __gc
*>(x);

>  // As well as this...
>        EnumColor xx = *dynamic_cast<EnumColor __gc *>(x);

>         // If I try this, I get an internal compiler
error
>  __box EnumColor __gc *xx = dynamic_cast<__box EnumColor
__gc *>(x);

>  return 0;
>       }
>    };
>}

>The class is used to sort an Array of enumerations.  The
problem is that
>when this class is used in C#, I always get an

InvalidCastException during
Quote:
>execution.  The array is also defined and constructed on
the C# side.  The
>code looks like this:

>protected static Array sm_arrayColors;

>....
>{
>  sm_arrayColors = Enum.GetValues(typeof(EnumColor));
>  Array.Sort(sm_arrayColor, null, 0,

sm_arrayColor.Length, new
Quote:
>Test.Comparer());
>}

>The enumeration is defined on the MC++ side...  Anyway, I
can't understand
>why this is happening.  I go as far as to compare the

types (as shown in the
Quote:
>above example) and they equate to true.  The other thing
is even if this was
>an invalid conversation isn't dynamic_cast suppose to
return a null
>reference instead of throwing an exception??    Inside
the de{*filter*} I can
>see that the objects x and y do point to the correct
values.   I also
>thought that maybe I had to use the __box keyword

somehow, but this doesn't
Quote:
>work either, and results in a compiler internal error.  
I can't just do an
>EnumColor(x) either, because this results in a syntax

error.  So does anyone
Quote:
>know what in the world is going on?

>Note:  I quickly copied the example above from a much

larger piece of code

- Show quoted text -

Quote:
>of mine, so forgive me it there are a few typos.

>Thanks,
>James C. Papp
>Guy Carpenter, Inc.
>(Note: My e-mail will block all incoming e-mail messages
unless prior
>arrangements are made.)

>.



Sat, 15 Jan 2005 04:08:35 GMT  
 __value enumeration casting woes! (InvalidCastException)
Ugh...
I just found out from a blurb in the MC++ help file that you should do this:

EnumColor  xx = dynamic_cast<__box EnumColor __gc *>(x);

Notice the use of the box keyword... However, when I try this I get an
internal compiler error (msc1.cpp, line 2844)...

Can anyone from Microsoft \confirm this is a problem with enumerations and
not just bool types?    Work around?

Thanks,
James C. Papp
Guy Carpenter, Inc.
(Note: My e-mail will block all incoming e-mail messages unless prior
arrangements are made.)


Quote:
> FYI,

> I just submitted a similar bug to MS a few days ago. You
> cannot unbox a 'bool' in MC++ -- you get a runtime
> InvalidCastException.

> The following code always fails:

> void main()
> {>    // It works for ints...

>    __box int *intBox = __box(42);
>    int i = *intBox; // works fine (i gets value '42')

>    // ... but not for bools

>    __box bool *box = __box(false);
>    bool b = *box;  // error here
> }

> They have not provided a resolution yet. For now I use
> System::Convert::ToBoolean(box).

> Best of luck.

> --Bob

> >-----Original Message-----
> >Does anyone know how to "unbox" an enumeration type in
> MC++???  I thought it
> >simply was using dynamic_cast or __try_cast but neither
> work.  No matter
> >what I do, I either get an InvalidCastException or an
> internal compiler
> >error.   I have a class that implements the IComparer
> interface in MC++, see
> >example below:

> >namespace Test
> >{
> >  public __value EnumColor
> >  {
> >    Red,
> >    Green,
> >    Blue
> >  };

> >  public __gc class Comparer : public IComparer
> >  {
> >    public:
> >      int Compare(Object *x, Object *y)
> >      {
> >        Type *type = x->GetType();
> >        // Displays Test.EnumColor
> >        Debug::Writeline(type->ToString());
> >        // Displays True!!!
> >        Debug::Writeline((type == __typeof
> (EnumColor)).ToString());

> >  // This throws an exception InvalidCastException...
> >        EnumColor __gc *xx = dynamic_cast<EnumColor __gc
> *>(x);

> >  // As well as this...
> >        EnumColor xx = *dynamic_cast<EnumColor __gc *>(x);

> >         // If I try this, I get an internal compiler
> error
> >  __box EnumColor __gc *xx = dynamic_cast<__box EnumColor
> __gc *>(x);

> >  return 0;
> >       }
> >    };
> >}

> >The class is used to sort an Array of enumerations.  The
> problem is that
> >when this class is used in C#, I always get an
> InvalidCastException during
> >execution.  The array is also defined and constructed on
> the C# side.  The
> >code looks like this:

> >protected static Array sm_arrayColors;

> >....
> >{
> >  sm_arrayColors = Enum.GetValues(typeof(EnumColor));
> >  Array.Sort(sm_arrayColor, null, 0,
> sm_arrayColor.Length, new
> >Test.Comparer());
> >}

> >The enumeration is defined on the MC++ side...  Anyway, I
> can't understand
> >why this is happening.  I go as far as to compare the
> types (as shown in the
> >above example) and they equate to true.  The other thing
> is even if this was
> >an invalid conversation isn't dynamic_cast suppose to
> return a null
> >reference instead of throwing an exception??    Inside
> the de{*filter*} I can
> >see that the objects x and y do point to the correct
> values.   I also
> >thought that maybe I had to use the __box keyword
> somehow, but this doesn't
> >work either, and results in a compiler internal error.
> I can't just do an
> >EnumColor(x) either, because this results in a syntax
> error.  So does anyone
> >know what in the world is going on?

> >Note:  I quickly copied the example above from a much
> larger piece of code
> >of mine, so forgive me it there are a few typos.

> >Thanks,
> >James C. Papp
> >Guy Carpenter, Inc.
> >(Note: My e-mail will block all incoming e-mail messages
> unless prior
> >arrangements are made.)

> >.



Sat, 15 Jan 2005 04:36:09 GMT  
 __value enumeration casting woes! (InvalidCastException)
Bob, thanks for the tip...
I used a similar work around: EnumColor(System::Convert::ToInt32(x)); ...

Thanks!
James C. Papp
Guy Carpenter, Inc.
(Note: My e-mail will block all incoming e-mail messages unless prior
arrangements are made.)



Sat, 15 Jan 2005 04:42:38 GMT  
 __value enumeration casting woes! (InvalidCastException)
Yea, looks like a compiler bug.
You can look at static_cast, it will generate IL instruction that bypasses
 the runtime checking and can also be used to cast one type to another when
you're
*absolutely* sure about the types.
For example,
{
    EnumColor ec = EnumColor::Red;
    Object* o1 = __box(ec);
    __box EnumColor* pec = static_cast<__box EnumColor*>(o1);
    Console::WriteLine(pec->ToString());

Quote:
}



Quote:
> Ugh...
> I just found out from a blurb in the MC++ help file that you should do
this:

> EnumColor  xx = dynamic_cast<__box EnumColor __gc *>(x);

> Notice the use of the box keyword... However, when I try this I get an
> internal compiler error (msc1.cpp, line 2844)...

> Can anyone from Microsoft \confirm this is a problem with enumerations and
> not just bool types?    Work around?

> Thanks,
> James C. Papp
> Guy Carpenter, Inc.
> (Note: My e-mail will block all incoming e-mail messages unless prior
> arrangements are made.)



> > FYI,

> > I just submitted a similar bug to MS a few days ago. You
> > cannot unbox a 'bool' in MC++ -- you get a runtime
> > InvalidCastException.

> > The following code always fails:

> > void main()
> > {>    // It works for ints...

> >    __box int *intBox = __box(42);
> >    int i = *intBox; // works fine (i gets value '42')

> >    // ... but not for bools

> >    __box bool *box = __box(false);
> >    bool b = *box;  // error here
> > }

> > They have not provided a resolution yet. For now I use
> > System::Convert::ToBoolean(box).

> > Best of luck.

> > --Bob

> > >-----Original Message-----
> > >Does anyone know how to "unbox" an enumeration type in
> > MC++???  I thought it
> > >simply was using dynamic_cast or __try_cast but neither
> > work.  No matter
> > >what I do, I either get an InvalidCastException or an
> > internal compiler
> > >error.   I have a class that implements the IComparer
> > interface in MC++, see
> > >example below:

> > >namespace Test
> > >{
> > >  public __value EnumColor
> > >  {
> > >    Red,
> > >    Green,
> > >    Blue
> > >  };

> > >  public __gc class Comparer : public IComparer
> > >  {
> > >    public:
> > >      int Compare(Object *x, Object *y)
> > >      {
> > >        Type *type = x->GetType();
> > >        // Displays Test.EnumColor
> > >        Debug::Writeline(type->ToString());
> > >        // Displays True!!!
> > >        Debug::Writeline((type == __typeof
> > (EnumColor)).ToString());

> > >  // This throws an exception InvalidCastException...
> > >        EnumColor __gc *xx = dynamic_cast<EnumColor __gc
> > *>(x);

> > >  // As well as this...
> > >        EnumColor xx = *dynamic_cast<EnumColor __gc *>(x);

> > >         // If I try this, I get an internal compiler
> > error
> > >  __box EnumColor __gc *xx = dynamic_cast<__box EnumColor
> > __gc *>(x);

> > >  return 0;
> > >       }
> > >    };
> > >}

> > >The class is used to sort an Array of enumerations.  The
> > problem is that
> > >when this class is used in C#, I always get an
> > InvalidCastException during
> > >execution.  The array is also defined and constructed on
> > the C# side.  The
> > >code looks like this:

> > >protected static Array sm_arrayColors;

> > >....
> > >{
> > >  sm_arrayColors = Enum.GetValues(typeof(EnumColor));
> > >  Array.Sort(sm_arrayColor, null, 0,
> > sm_arrayColor.Length, new
> > >Test.Comparer());
> > >}

> > >The enumeration is defined on the MC++ side...  Anyway, I
> > can't understand
> > >why this is happening.  I go as far as to compare the
> > types (as shown in the
> > >above example) and they equate to true.  The other thing
> > is even if this was
> > >an invalid conversation isn't dynamic_cast suppose to
> > return a null
> > >reference instead of throwing an exception??    Inside
> > the de{*filter*} I can
> > >see that the objects x and y do point to the correct
> > values.   I also
> > >thought that maybe I had to use the __box keyword
> > somehow, but this doesn't
> > >work either, and results in a compiler internal error.
> > I can't just do an
> > >EnumColor(x) either, because this results in a syntax
> > error.  So does anyone
> > >know what in the world is going on?

> > >Note:  I quickly copied the example above from a much
> > larger piece of code
> > >of mine, so forgive me it there are a few typos.

> > >Thanks,
> > >James C. Papp
> > >Guy Carpenter, Inc.
> > >(Note: My e-mail will block all incoming e-mail messages
> > unless prior
> > >arrangements are made.)

> > >.



Sat, 15 Jan 2005 04:50:17 GMT  
 __value enumeration casting woes! (InvalidCastException)
Hi Leon,

Quote:
> Yea, looks like a compiler bug.

I just wanted to mention this is indeed a bug, and one MS has known for
quite a while, but couldn't be fixed for this release. As you correctly
mention, the easiest workaround is to use static_cast<> indeed, i.e.:

 EnumColor myA = *(static_cast<EnumColor __box*>(object));

I mentioned this problem on the section on unboxing in my Feb MC++ article
on MSDN Magazine...

--
Tomas Restrepo



Sat, 15 Jan 2005 09:13:26 GMT  
 __value enumeration casting woes! (InvalidCastException)
Hi,

This was a known issue in VC7.0 when unbox an enumeration. It will be fixed
in the latter version VC7.0.

Have a nice day!

Regards,
HuangTM
This posting is provided "AS IS" with no warranties, and confers no rights.



Sat, 15 Jan 2005 13:42:36 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Discarded Function Values (To Cast or Not to Cast)

2. CodeDom => enumeration value

3. CDHtmlDialog woes.. getting the value from a listbox

4. casting null value

5. casting return value from malloc

6. Casting char when printing its value

7. Should we cast the value from strlen ?

8. Casting return value to Typedef enum?

9. Cast on l-value

10. Invalid character value for cast specification

11. l-value casting

12. Casting Pointer Value

 

 
Powered by phpBB® Forum Software