Psytronic
Psytronic

Reputation: 6113

How do I implement this public accesible enum

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

Upvotes: 4

Views: 229

Answers (3)

Phil Ross
Phil Ross

Reputation: 26100

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.

Upvotes: 0

JaredPar
JaredPar

Reputation: 754763

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; } }

Upvotes: 5

Andrew Hare
Andrew Hare

Reputation: 351526

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; } 
    }
}

Upvotes: 3

Related Questions