我如何实现这个公共可访问的枚举

发布于 2024-08-25 21:47:05 字数 366 浏览 2 评论 0原文

我正在尝试访问我班级的私人枚举。但我不明白与其他成员相比,让它发挥作用所需的差异;

如果这有效:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小糖芽 2024-09-01 21:47:05

这里发生了两件截然不同的事情。

在第一个示例中,您定义了公共类型的私有字段。然后,您将通过公共方法返回该已公共类型的实例。这是有效的,因为类型本身已经是公共的。

在第二个示例中,您定义一个私有类型,然后通过公共属性返回一个实例。该类型本身是私有的,因此不能公开暴露。

第二种情况的更等效的示例如下

public enum MyEnum { Alpha, Beta }
// ... 
private MyEnum _value;
public MyEnum GetMyEnum { get { return _value; } }

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; } }
养猫人 2024-09-01 21:47:05

枚举需要是公共的,以便其他类型可以引用它 - 您希望存储对该枚举的实例的私有引用

public enum myEnum { Alpha, Beta }

public class Foo
{
    private myEnum yourEnum = myEnum.Alpha;
    public myEnum getMyEnum
    { 
        get { return yourEnum; } 
    }
}

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; } 
    }
}
咆哮 2024-09-01 21:47:05

在第一个示例中,您声明一个 double 类型的字段,然后声明一个访问它的属性。在第二个示例中,您声明一个枚举类型,然后尝试在属性中返回该类型。您需要声明枚举类型,然后声明使用它的字段:

public enum MyEnum{ Alpha, Beta};

private MyEnum myEnum = MyEnum.Alpha;

//misc code

public Enum getMyEnum{ get{ return myEnum; } }

枚举类型也需要公开,因为使用它的属性是公共的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文