简体   繁体   中英

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

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM