How to implement this public listing

I am trying to access a private enum class. But I do not understand the difference needed for her work compared to other members;

If this works:

  private double dblDbl = 2;

 // misc code

 public double getDblDbl {get {return dblDbl;  }}

Why can't I do this with an enumeration?

  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
+4
source share
3 answers

You have two different things here.

In the first example, you define a private field of a public type. Then you return an instance of this public type using the public method. This works because the type itself is already open.

In the second example, you define a private type, and then return the instance through a public property. The type itself is private and therefore cannot be published publicly.

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; } } 
+5
source

An enumeration must be publicly available, so other types can reference it - you want to keep a personal link to an instance of this enumeration:

 public enum myEnum { Alpha, Beta } public class Foo { private myEnum yourEnum = myEnum.Alpha; public myEnum getMyEnum { get { return yourEnum; } } } 
+3
source

In the first example, you declare a field of type double and then declare a property that accesses it. In the second example, you declare the desired type and then try to return the type in the property. You need to declare the type you want, 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; } } 

An enumerated type must also be published, since the property that uses it is public.

0
source

Source: https://habr.com/ru/post/1304505/


All Articles