我如何实现这个公共可访问的枚举
我正在尝试访问我班级的私人枚举。但我不明白与其他成员相比,让它发挥作用所需的差异;
如果这有效:
private double dblDbl = 2; //misc code public double getDblDbl{ get{ return dblDbl; } }
为什么我不能用枚举来做到这一点?
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里发生了两件截然不同的事情。
在第一个示例中,您定义了公共类型的私有字段。然后,您将通过公共方法返回该已公共类型的实例。这是有效的,因为类型本身已经是公共的。
在第二个示例中,您定义一个私有类型,然后通过公共属性返回一个实例。该类型本身是私有的,因此不能公开暴露。
第二种情况的更等效的示例如下
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
枚举需要是公共的,以便其他类型可以引用它 - 您希望存储对该枚举的实例的私有引用:
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:
在第一个示例中,您声明一个 double 类型的字段,然后声明一个访问它的属性。在第二个示例中,您声明一个枚举类型,然后尝试在属性中返回该类型。您需要声明枚举类型,然后声明使用它的字段:
枚举类型也需要公开,因为使用它的属性是公共的。
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:
The enumerated type also needs to be made public because the property that uses it is public.