Ask a questionAsk a question
 

AnswerCreating an enum member

  • Tuesday, November 03, 2009 6:00 AMA n t Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,

    I just tried exposing an enum as a member of a class & i was surprised that it didn't work as expected. Basically I did this:

    MyNamespace
    {
       public enum MyEnum{Hello, Goodbye}

       public class MyClass
       {
          public static MyEnum MyEnum;
      
       }

    }

    When I try to use it, I only get the enums base members, not the enums values.

    e.g.


    MyEnum enum = Myclass.MyEnum.OtherStuffButNotMyValues

    What am I doinf wrong here?

    many thanks for your help


    @nt

Answers

  • Wednesday, November 04, 2009 10:58 AMMatthew Watson Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    You are correct in the context of what I've written, but say the class  is used in a different namespace. I would like the enum to be encapsulated in the class rather than importing the enum with it. (just neater in my mind).
    Unfortunately, you can't do that and still have the property have the same name as the enum; you have to have the enum declared outside the class (even if the enum is public).

    Microsoft say you should put the enum into a suitable namespace along with the class(es) that use it.
    • Marked As Answer byA n t Thursday, November 05, 2009 4:50 AM
    •  

All Replies

  • Tuesday, November 03, 2009 6:20 AMBadButBit Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    your public static variable has the same name as the enum list.
    change the name of your variable to something else like :

     public static MyEnum MyEnumVar;
    
    <br/><br/><br/>
    

    my code is perfect until i don't find a bug
  • Tuesday, November 03, 2009 6:21 AMDeborahKMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I would suggest that you do *not* name your variable and your Enum the same thing. At least not with the same casing. So something more like this:

    public static MyEnum myEnum;

    That will make it more clear which you are referencing.

    Hope this helps.


    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
  • Tuesday, November 03, 2009 6:32 AMA n t Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,

    Many thanks for that. I've changed it accordingly. While that may address best practice, it hasn't actually solved my issue. When I try to access the enum, I still don't get the values it is meant to expose: To be precise:

    internal

     

    enum SomeValue

    {

    True,

    False

    };



    internal

     

    class TestClass

    {
    public static SomeValue SomeValueEnum;

    }

    class SomeClass
    {
       public void SomeMethod()
       {
            TestClass.SomeEnumValue.[CompareTo;Equals;GetHashCode etc etc...]
       }  // I would expect True or False to be exposed...
    }


    I cannot access the enums values.

    Any ideas as to what I'm doing wrong here

    Thanks  

     


    @nt
  • Tuesday, November 03, 2009 6:40 AMVic Vega Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code

    Hi ,
    If you expecting True or false, then it is wrong. Those are enum values and can be as
    SomeValue.True
    SomeValue.False

    You can use SomeValueEnum variable as below
    TestClass.SomeValueEnum = SomeValue.True;

    SomeValueEnum holds a value of type SomeValue.

    Below code should help you understand more about enum and its variable:

    	enum State
    	{
    		Initial,
    		Read,
    		Close
    	}
    
    	class Reader
    	{
    		private State CurrentState;
    
    		internal State ReaderCurrentState
    		{
    			get { return CurrentState; }
    		}
    
    		public Reader()
    		{
    			CurrentState = State.Initial;
    		}
    
    		public void Reading()
    		{
    			CurrentState = State.Read;
    		}
    
    		public void Close()
    		{
    			CurrentState = State.Close;
    		}
    	}
    


    Hope it is helpful.
    Thanks
    PKR

  • Tuesday, November 03, 2009 8:31 AMMatthew Watson Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    It's actually very common to make the name of an enum property the same as the name of the enum itself.

    See, for example, these members of System.Windows.Forms.Control:

    public AccessibleRole AccessibleRole { get; set; }
    public ImeMode ImeMode { get; set; }

    The important thing here is that it is a property, not a field. You shouldn't really have public fields in a class anyway.
  • Tuesday, November 03, 2009 10:18 PMA n t Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks, Vic, I'm not expecting boolean values. I just expect to see True/False to be *exposed* by the enum :)
    @nt
    • Edited byA n t Tuesday, November 03, 2009 10:42 PMaddition
    •  
  • Tuesday, November 03, 2009 10:19 PMA n t Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks Matt, that's what I thought i nthe first place :) I didn't see nay issue in calling it the same name. I've always done that with properties. Thanks for the backup there :)

    However, it still doens't answer my question. If I expose the enum as a property using getters & setters, when I triy to access it, I still only get the native members, but not the members of the enum.

    e.g.

    enum Numbers
    {1,2};

    class MyClass
    {
       public static Numbers Numbers{get;set;}
    }

    MyClass.Numbers.CompareTo etc etc  (but no 1 or 2)


    I'd expect (or desire)
    MyClass.Numbers.1
    or
    MyClass.Numbers.2


    This is if it is static or instance.

    How Can I do this?

    Many thanks


    @nt
  • Wednesday, November 04, 2009 5:13 AMVic Vega Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    If you want to access MyClass.Numbers.1 , then you can do it as below:
    Numbers.1.
    Why to add MyClass.
    You want access members of enum, then you can use enum directly. Am I wrong in my understanding?
  • Wednesday, November 04, 2009 5:46 AMA n t Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks vic,

    You are correct in the context of what I've written, but say the class  is used in a different namespace. I would like the enum to be encapsulated in the class rather than importing the enum with it. (just neater in my mind). If the enum is specific to the class & won't be used for any other purpose, I'd like it to be represented as part of the classes members, so rather than do this:

    Myclass c = new Class();

    c.Value = MyEnum.1;

    you could do this:

    c.Value = MyClass.MyEnum.1;

    To me this would wrap it better. Just my own opinion but I think it would work more neatly.

    So, can this be done? Can you expose an enum out of a class?

    Cheers for your interest
    @nt
  • Wednesday, November 04, 2009 10:07 AMVic Vega Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Hi,
             Lets try to understand this with a example.
    	class EnumEncaps
    	{
    		public EnumEncaps() { }
    		public EnumExample CurrentValue
    		{
    			get { return EnumExample.Value1; }
    		}
    		public  enum EnumExample
    		{
    			Value1,
    			Value2,
    			Value3
    		}
    
    		public static void Check()
    		{
    			EnumEncaps e = new EnumEncaps();
    			System.Diagnostics.Debug.Assert(e.CurrentValue == EnumEncaps.EnumExample.Value1, "Check Value");
    		}
    	}
    
    In above code , I have kept enum within a class. look at the way I used the property. If enum is kept as private, then the property too has to be private, since enum is not exposed (Compiler will throw Inconsistent accessibility: property type error). If you are using enum only within class, then you can encapsulate enum as private.

    If client of this class has to use enum type property, then we have expose enum to client at least. If you keep access within a assembly, Please use internal keyword.

    Thanks & Regards
    Pavan Rai

  • Wednesday, November 04, 2009 10:58 AMMatthew Watson Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    You are correct in the context of what I've written, but say the class  is used in a different namespace. I would like the enum to be encapsulated in the class rather than importing the enum with it. (just neater in my mind).
    Unfortunately, you can't do that and still have the property have the same name as the enum; you have to have the enum declared outside the class (even if the enum is public).

    Microsoft say you should put the enum into a suitable namespace along with the class(es) that use it.
    • Marked As Answer byA n t Thursday, November 05, 2009 4:50 AM
    •  
  • Thursday, November 05, 2009 4:50 AMA n t Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    ok :( I guess that answers my question.

    many thanks for your patience on this one

    @nt