Strings ints and enums

J

Jeremy

Hello all,

I'm having some problems working with enumerations. A function call to
a 3rd party dll has in it's parameter list an enumerated type which
connects me to a COM port. I'm trying to convert a stored parameter
from a database into this enumerated value and can't seem to get
anything to work. In some VB.NET code that was written, it works fine
and that's the frustrating part. I've included some code below:

Old vb.net code to get the enumerated value from a string:

myEnum = [Enum].Parse(GetType(EnumType), "Enum Member")

This code works fine, unfortunatly VB has no problems converting from
an Object to EnumType. When I try the same thing in VC++ with:

myEnum = Enum::parse(__typeof(EnumType), "Enum Member");

I get the compiler error C2440: Cannot convert from 'Object *' to
'EnumType'. I've also tried several ways of casting and had no luck
with that either, so what I've had to resort is below:

String *myItem = "Enum member;
EnumType myEnum;

if (myItem->Equals(__box(EnumType::Even)->ToString()))
theParity = EnumType::Even;
else if (myItem->Equals(__box(EnumType::Mark)->ToString()))
theParity = EnumType::Mark;
else if (myItem->Equals(__box(EnumType::None)->ToString()))
theParity = EnumType::None;
etc...

If anyone has any insight on converting a string or an integer to an
enumerated type, it would be greatly appreciated.

Thanks,
Jeremy
 
J

Jeremy

Thanks for the response Vladimir, but that doesn't seem to work either.

That throws the compiler error C2440, cannot convert from
'System::Enum' to 'EnumType', which doesn't make any sense to me
because I thought that was the point of *dynamic_cast.
 
V

Vladimir Nesterovsky

I'm having some problems working with enumerations. A function call to
a 3rd party dll has in it's parameter list an enumerated type which
connects me to a COM port. I'm trying to convert a stored parameter
from a database into this enumerated value and can't seem to get
anything to work. In some VB.NET code that was written, it works fine
and that's the frustrating part. I've included some code below:

Old vb.net code to get the enumerated value from a string:

myEnum = [Enum].Parse(GetType(EnumType), "Enum Member")

This code works fine, unfortunatly VB has no problems converting from
an Object to EnumType. When I try the same thing in VC++ with:

myEnum = Enum::parse(__typeof(EnumType), "Enum Member");

Did you try:

myEnum = *dynamic_cast<EnumType *>(Enum::parse(__typeof(EnumType), "Enum
Member")); ?
 
J

Jeremy

Okay, now I'm really confused. I've copied and pasted into a new
project, EXACTLY, and I get the compile error:

c:\Documents and Settings\jlueck\Desktop\C++.NET Projects\Enum
Test\Enum Test.cpp(23): error C2440: 'initializing' : cannot convert
from 'System::Enum' to 'Color'

*shrug* Is there an include or using namespace that I should be
worring about? I'm running VS .NET 2003 on an XP box...
 
V

Vladimir Nesterovsky

That throws the compiler error C2440, cannot convert from
'System::Enum' to 'EnumType', which doesn't make any sense to me
because I thought that was the point of *dynamic_cast.

In fact I succeed in such a conversion:

__value enum Color
{
Red,
Green,
Blue
};

int main(int argc, char* argv[])
{
Color color = *dynamic_cast<Color
*>(System::Enum::parse(__typeof(Color), S"Green"));

System::Console::WriteLine(color);
}
 
T

Tomas Restrepo \(MVP\)

Jeremy,
Okay, now I'm really confused. I've copied and pasted into a new
project, EXACTLY, and I get the compile error:

c:\Documents and Settings\jlueck\Desktop\C++.NET Projects\Enum
Test\Enum Test.cpp(23): error C2440: 'initializing' : cannot convert
from 'System::Enum' to 'Color'

*shrug* Is there an include or using namespace that I should be
worring about? I'm running VS .NET 2003 on an XP box...

Nope. It's just a long standing compiler problem. The solution is to use
static_cast<> instead with a bit of trickery. Try this:

Color color = *(static_cast<Color
__box*>(System::Enum::parse(__typeof(Color), S"Green")));


FWIW, I talked about this in more detail in an article I wrote a while back
on MSDN magazine (see the unboxing section):
http://msdn.microsoft.com/msdnmag/issues/02/02/ManagedC/default.aspx
 
J

Jeremy

Thanks Vladimir and Tomas for your help and that's an excellent article
Tomas.

Jeremy Lueck
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top