views:

87

answers:

3

Hey guys,

I'm trying to access my class's private enum. But I don't understand the difference needed to get it working compared to other members;

If this works:

private double dblDbl = 2;

//misc code

public double getDblDbl{ get{ return dblDbl; } }

Why can I not do it with enum?

private enum myEnum{ Alpha, Beta};

//misc code

public Enum getMyEnum{ get{ return myEnum; } }
//throws "Window1.myEnum" is a "type" but is used like a variable
+3  A: 

The enumeration needs to be public so other types can reference it - you want to store a private reference to an instance of that enumeration:

public enum myEnum { Alpha, Beta }

public class Foo
{
    private myEnum yourEnum = myEnum.Alpha;
    public myEnum getMyEnum
    { 
        get { return yourEnum; } 
    }
}
Andrew Hare
+5  A: 

You have 2 very different things going on here.

In the first example you are defining a private field of a public type. You are then returning an instance of that already public type through a public method. This works because the type itself is already public.

In the second example you are defining a private type and then returning an instance through a public property. The type itself is private and hence can't be exposed publically.

A more equivalent example for the second case would be the following

public enum MyEnum { Alpha, Beta }
// ... 
private MyEnum _value;
public MyEnum GetMyEnum { get { return _value; } }
JaredPar
I think you mean `public MyEnum GetMyEnum { get { return _value; } }`
Travis Gockel
@Travis, yes you are correct. Thanks for the correction (updated)
JaredPar
Ahhhh, ok, I understand what's going on now, thanks :)Never really used Enums before, so having to get my head around them at somepoint
Psytronic
A: 

In your first example, you are declaring a field of type double and then declaring a property that accesses it. In your second example, you declare an enumerated type and then attempt to return the type in a property. You need to declare the enumerated type and then declare a field that uses it:

public enum MyEnum{ Alpha, Beta};

private MyEnum myEnum = MyEnum.Alpha;

//misc code

public Enum getMyEnum{ get{ return myEnum; } }

The enumerated type also needs to be made public because the property that uses it is public.

Phil Ross