Results 1 to 8 of 8

Thread: Enum type from (e.g.) string???

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 1999
    Location
    Prague, Czech Republic
    Posts
    350

    Unhappy Enum type from (e.g.) string???

    Well, let's say I would like to do something like this:

    Code:
     enum Fruit {Apple, Orange, Banana}
     enum Vegetable {Onion, Carrot, Cabbage}
    
     string GetItemName(int Index, string category)
     {
         return ((category)Index).ToString();
     }
    [/b]
    I hope this illustrates the problem...

    I need to return "Cabbage" passing 2,"Vegetable" to the function

  2. #2
    Hyperactive Member Scott Penner's Avatar
    Join Date
    Dec 2000
    Location
    Mountain View
    Posts
    327
    Sorry I don't have the time to work this out right now, but you should look up the Enum class. There are a lot of static methods to help you determine if an index or string is part of an enum and also to convert enums to string and integers etc.

    I can figure this out next week if you're still needing help.
    -scott
    he he he

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 1999
    Location
    Prague, Czech Republic
    Posts
    350
    Well, I don't have problems with returning index of any enum member.

    Actually, if you look to the example more carefully, you would find out the issue is (probably) behind the enum class.

    I need to tell C# compiler the name of enum to use by (string) variable.

  4. #4
    Hyperactive Member Scott Penner's Avatar
    Join Date
    Dec 2000
    Location
    Mountain View
    Posts
    327
    If you want help, you might want to use more tact in the future.
    Of course I read your post.
    There is a function in the unum class to return an enum index from the string. There are also functions to check the type of a passed enum.

    Hope that helps. I was going to figure it out for you tomorrow, but oh well...
    -scott
    he he he

  5. #5
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Try using an indexer in a class or a struct.
    Dont gain the world and lose your soul

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 1999
    Location
    Prague, Czech Republic
    Posts
    350
    At first I would like to apologize to Scott if he feels umbraged. I am sure he read the post and tried to help as possible. I was just in a hurry and I also think my previous skills with this way of support were unfortunately not so good. However, I really appreciate every suggestion to the question discussed.




    To DevGrp: As you said, indexer can be used in a class or a struct, but I am afraid about using it with enum type.

  7. #7
    Hyperactive Member Scott Penner's Avatar
    Join Date
    Dec 2000
    Location
    Mountain View
    Posts
    327

    Smile

    OK here we go...
    No hard feelings?

    PHP Code:
    static string GetItemName(int Indexstring Category)
    {
        
    // The name of the object must be explicitly specified, so the Category
        // must have the prefix of the NameSpace,Class and a "+" (not sure why the +)
        
    string    TestString "EnumTestNamespace.EnumTest+" Category ;
        
    string    ReturnVal "";

        
    // Need the error check in case the category doesn't exist
        
    try
        {
            
    // Creates a type from the string and parses the index / type into an Enum
            // Then we turn the enum into a string for return...
            
    ReturnVal Enum.Parse(Type.GetType(TestString),Index.ToString()).ToString();
        }
        catch
        {
            
    ReturnVal "Not found";
        }

        return 
    ReturnVal;

    -scott
    he he he

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 1999
    Location
    Prague, Czech Republic
    Posts
    350
    Well, thank you Scott, you solved the problem!

    It works even without EnumTestNamespace.EnumTest

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