CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2004
    Posts
    2

    Question get enum declarator to string

    Hi,
    is it possible to get the declarator name of an enum, for example :

    enum day {
    monday,
    tuesday
    };

    and

    day today=monday;
    CString dayname=howto?( today );

    so that "dayname" is "monday"??

    I don´t want to use extra arrays with the names for exisiting big enums that i use in my programs, but want the names to show for status info..

    I think that there must be a way to do this, cause for example the VS variable viewer can show during debugging the "names" for variables of type enum..

    thanks,
    Siggi

    PS: sorry for the broken english.. :-(

  2. #2
    Join Date
    Mar 2004
    Posts
    2
    If this post is in the wrong discussion group, maybe one of the administrators could move it to the right one.

  3. #3
    Join Date
    Apr 2003
    Location
    UK
    Posts
    83
    In C++, there is no way to automatically convert enums into string equivalents.

    The Visual Studio IDE is able to do it because it has access to your source code, which your running program doesn't (as the source code will have been compiled).

    This kind of thing is normally done with a lookup table, or resource dll.

    By the way, you can do this in C#, as all types are objects derived from the base object, which has a ToString method.

  4. #4
    Join Date
    May 1999
    Location
    West Sussex, England
    Posts
    1,939
    You could declare a map:

    map<day, CString> x;

    // populate it
    x[monday] = "monday";
    x[tuesday] = "tuesday";
    ...

    // access it
    dayname = x[enumVariable];
    Please use meaningful question titles - "Help me" does not let me know whether I can help with your question, and I am unlikely to bother reading it.
    Please remember to rate useful answers. It lets us know when a question has been answered.

  5. #5
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by sdude
    If this post is in the wrong discussion group, maybe one of the administrators could move it to the right one.
    No....it is fine...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured