Strings ints and enums

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

    #1

    Strings ints and enums

    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(__t ypeof(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(E numType::Even)->ToString()))
    theParity = EnumType::Even;
    else if (myItem->Equals(__box(E numType::Mark)->ToString()))
    theParity = EnumType::Mark;
    else if (myItem->Equals(__box(E numType::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

    #2
    Re: Strings ints and enums

    > I'm having some problems working with enumerations. A function call to[color=blue]
    > 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(__t ypeof(EnumType) , "Enum Member");[/color]

    Did you try:

    myEnum = *dynamic_cast<E numType *>(Enum::Parse( __typeof(EnumTy pe), "Enum
    Member")); ?

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



    Comment

    • Jeremy

      #3
      Re: Strings ints and enums

      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.

      Comment

      • Vladimir Nesterovsky

        #4
        Re: Strings ints and enums

        [color=blue]
        > 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.[/color]

        In fact I succeed in such a conversion:

        __value enum Color
        {
        Red,
        Green,
        Blue
        };

        int main(int argc, char* argv[])
        {
        Color color = *dynamic_cast<C olor
        *>(System::Enum ::Parse(__typeo f(Color), S"Green"));

        System::Console ::WriteLine(col or);
        }

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


        Comment

        • Jeremy

          #5
          Re: Strings ints and enums

          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++.NE T 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...

          Comment

          • Tomas Restrepo \(MVP\)

            #6
            Re: Strings ints and enums

            Jeremy,[color=blue]
            > 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++.NE T 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...[/color]

            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<C olor
            __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/is...C/default.aspx


            --
            Tomas Restrepo
            tomasr@mvps.org



            Comment

            • Jeremy

              #7
              Re: Strings ints and enums

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

              Jeremy Lueck

              Comment

              Working...