Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / July 2005

Tip: Looking for answers? Try searching our database.

Strings ints and enums

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jeremy - 14 Jul 2005 20:27 GMT
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
Vladimir Nesterovsky - 14 Jul 2005 21:18 GMT
> 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
[quoted text clipped - 11 lines]
>
>     myEnum = Enum::Parse(__typeof(EnumType), "Enum Member");

Did you try:

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

Signature

Vladimir Nesterovsky
e-mail: vladimir@nesterovsky-bros.com
home: http://www.nesterovsky-bros.com

Jeremy - 14 Jul 2005 21:22 GMT
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.
Vladimir Nesterovsky - 14 Jul 2005 23:53 GMT
> 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);
}

Signature

Vladimir Nesterovsky
e-mail: vladimir@nesterovsky-bros.com
home: http://www.nesterovsky-bros.com

Jeremy - 15 Jul 2005 00:13 GMT
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...
Tomas Restrepo (MVP) - 15 Jul 2005 01:08 GMT
Jeremy,
> Okay, now I'm really confused.  I've copied and pasted into a new
> project, EXACTLY, and I get the compile error:
[quoted text clipped - 5 lines]
> *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

Signature

Tomas Restrepo
tomasr@mvps.org
http://www.winterdom.com/

Jeremy - 15 Jul 2005 18:17 GMT
Thanks Vladimir and Tomas for your help and that's an excellent article
Tomas.

Jeremy Lueck

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.