none
Design Enum Type for Class Library RRS feed

  • Question

  • Hi All,

    We have a requirement where enum types should be as follows:

    Enum TabGroups
    {
            Enum Group1
           {
                 Name = 0,
                 Id =1,
                 Label = 3

            }
           
            Enum Group2
           {
                 Name = 4,
                 Id =5,
                 Label = 6

            }

            Enum Group3
           {
                 Name = 3,
                 Id =4,
                 Label = 5

            }
    }

    This enum type will be passed to a function in C# COM Library
    void PrintMe(TabGroups tabGroup)
    {
       //do something
    }


    Now C#/VB client code should use my Enum as follows:
    PrintMe(TabGroups.Group1.Name)
    PrintMe(TabGroups.Group2.Name)


    Looks to me this is not possible using Enum as it does not support embedding enum inside another enum. I would like to know if there is any other way I can achive the same.

    Thanks in advance,
    Sanban

    Tuesday, January 13, 2009 1:38 PM

Answers

  • I would use a namespace instead:

    namespace TabGroups
    {
       enum Group1 { Name = 0, Id = 1, Label = 3 }
       ...
    }

    Then you would use it like so:

    PrintMe(TabGroups.Group1.Name);

    For interop scenarios this is reasonable.  I wouldn't recommend this approach for general .NET enums though.

    Michael Taylor - 1/13/09
    http://p3net.mvps.org
    • Marked as answer by Zhi-Xin Ye Monday, January 19, 2009 9:27 AM
    Tuesday, January 13, 2009 2:59 PM
    Moderator

All replies

  • Can't you use regular classes and properties? Look at how the System.Drawing.Color type is defined for example.
    Mattias, C# MVP
    Tuesday, January 13, 2009 2:46 PM
  • By what I can see, you might be stuck to using classes instead.

    static class Module1   
    {   
          
        public static void Main()   
        {   
              
            PrintMe(new Group1());   
              
        }   
          
        public static void PrintMe(TabGroups tabGroup)   
        {   
              
        }   
    }   
     
     
    public abstract class TabGroups   
    {   
          
        protected int _name;   
        protected int _id;   
        protected int _label;   
          
        public object Name {   
            get { return _name; }   
        }   
          
        public object Id {   
            get { return _id; }   
        }   
          
        public object Label {   
            get { return _label; }   
        }   
    }   
     
     
    public class Group1 : TabGroups   
    {   
          
        public Group1()   
        {   
              
            _name = 0;   
            _id = 1;   
            _label = 3;   
              
        }   
    }   
     
     
    public class Group2 : TabGroups   
    {   
          
        public Group2()   
        {   
              
            _name = 4;   
            _id = 5;   
            _label = 6;   
              
        }   
    }   
     
     
    public class Group3 : TabGroups   
    {   
          
        public Group3()   
        {   
              
            _name = 3;   
            _id = 4;   
            _label = 5;   
              
        }   
    }  

    or

    static class Module1   
    {   
          
        public static void Main()   
        {   
              
            List<TabGroups> groups = default(List<TabGroups>);   
              
            groups = new List<TabGroups>();   
              
            groups.Add(new TabGroups(0, 1, 3));   
            groups.Add(new TabGroups(4, 5, 6));   
            groups.Add(new TabGroups(3, 4, 5));   
              
            PrintMe(groups(0));   
              
        }   
          
        public static void PrintMe(TabGroups tabGroup)   
        {   
              
        }   
    }   
     
     
    public class TabGroups   
    {   
          
        protected int _name;   
        protected int _id;   
        protected int _label;   
          
        public TabGroups(int name, int id, int label)   
        {   
              
            _name = name;   
            _id = id;   
            _label = label;   
              
        }   
          
        public object Name {   
            get { return _name; }   
        }   
          
        public object Id {   
            get { return _id; }   
        }   
          
        public object Label {   
            get { return _label; }   
        }   
    }  

    Puzzles, brain teases, riddles, enigmas: http://www.toysforthebrain.com
    Tuesday, January 13, 2009 2:56 PM
  • I would use a namespace instead:

    namespace TabGroups
    {
       enum Group1 { Name = 0, Id = 1, Label = 3 }
       ...
    }

    Then you would use it like so:

    PrintMe(TabGroups.Group1.Name);

    For interop scenarios this is reasonable.  I wouldn't recommend this approach for general .NET enums though.

    Michael Taylor - 1/13/09
    http://p3net.mvps.org
    • Marked as answer by Zhi-Xin Ye Monday, January 19, 2009 9:27 AM
    Tuesday, January 13, 2009 2:59 PM
    Moderator
  •  After reading it again, I think I haven't posted a solution related to your issue. TaylorMichaelL one seems good.
    Puzzles, brain teases, riddles, enigmas: http://www.toysforthebrain.com
    Tuesday, January 13, 2009 3:32 PM
  • You can use a class to achieve the same thing:

     
    public class TabGroups  
    {  
        public readonly int Name;  
        public readonly int Id;  
        public readonly int Label;  
     
        private TabGroups(int name, int id, int label)  
        {  
            this.Name = name;  
            this.Id = id;  
            this.Label = label;  
        }  
     
        public static readonly TabGroups Group1 = new TabGroups(0, 1, 3);  
        public static readonly TabGroups Group2 = new TabGroups(4, 5, 6);  
        public static readonly TabGroups Group3 = new TabGroups(3, 4, 5);  
    }  
     


     


    Mark "Mark Rendle" Rendle
    Wednesday, January 14, 2009 10:52 AM