简体   繁体   English

C#中的枚举 - 赋值

[英]enums in C# - assignment

How does one do this in c#? 如何在c#中做到这一点?

Let's say that myclass has : 让我们说myclass有:

private enum Days{};


1) How does one add data to the enum inside the myclass with the help of the constructor? 1)如何在构造函数的帮助下将数据添加到myclass中的枚举? As in : 如:

myclass my = new myclass(Monday,Friday);

so that the enum inside the class gets the "Monday, Friday" properties. 这样类中的枚举就会得到“星期一,星期五”的属性。


2) Can one also make a property for an enume inside the class once it is initialized ? 2)一旦初始化,是否还可以为类中的enume创建属性? For example : 例如 :

my.Days = new enum Days(Tuesday); //where the old settings are replaced.

EDIT: 编辑:

What I want to do is following: 我想做的是:

Have an enum in the main class, like enum Days{Monday,Tuesday,Wednesday,Thursday,Friday}; 在主要课程中有一个枚举,如枚举天{星期一,星期二,星期三,星期四,星期五};

I would also have something inside the myclass (enum?) so that I can assign some values from Days enum to myclass internal (enum?) with only some but not all of the values in the Days enum. 我也会在myclass(enum?)中有一些东西,这样我就可以将Days enum中的一些值分配给myclass internal(enum?),只有一些但不是所有的值枚举。

So that I can get a myclass which contains an (enum?) with "Monday,Tuesday", while some myclass222 contains (enum?) "Friday" etc. 所以我可以得到一个myclass,其中包含一个带有“星期一,星期二”的(枚举?),而有些myclass222包含(枚举?)“星期五”等。

I don't know if the enum is the correct way to do this inside the myclass classes ? 我不知道枚举是否是在myclass类中执行此操作的正确方法?

Your question is somewhat vague, but if you are actually asking how to construct an enum where the values can be combined, the answer is to decorate it with the Flags attribute: 你的问题有些模糊,但是如果你实际上是在询问如何构造一个可以组合值的枚举,那么答案就是用Flags属性来装饰它:

[Flags]
public enum Days
{
    None = 0,
    Monday = 1,
    Tuesday = 2,
    Wednesday = 4,
    Thursday = 8,
    Friday = 16,
    Saturday = 32,
    Sunday = 64
}

Now, if you have some member of the type Days , you can assign a combined value to it: 现在,如果您有类型为Days成员,则可以为其分配组合值:

Days someDays = Days.Monday | Days.Wednesday;

Update 更新
On request; 根据要求; here is how to check whether a certain value is set: 这里是如何检查是否设置了某个值:

bool wednesdayIsSet = (someDays & Days.Wednesday) == Days.Wednesday;

In response to the question on how to create a member of the enum in one class when the enum is defined in another class, that would look like this: 回答在另一个类中定义枚举时如何在一个类中创建枚举成员的问题,如下所示:

public class SomeClass
{
    [Flags]
    public enum Days
    {
        None = 0,
        Monday = 1,
        // and so on
    }
}

public class SomeOtherClass
{
    public SomeClass.Days SomeDays { get; set; }
}

1) You can't. 1)你不能。 The enum must be defined at compile time 枚举必须在编译时定义

It seems like you might look for an AttributeCollection instead. 看起来你可能会寻找一个AttributeCollection。 (have a look at Fredrik Mörk's answer, that it what I was trying to talk about ;) ) (看看FredrikMörk的回答,这是我试图谈论的内容;))

2) my.Days = Days.Tuesday 2) my.Days = Days.Tuesday

Can't tell from the question, but maybe you intend something like this: 无法从问题中分辨出来,但也许您打算这样的事情:

  1. Define an enum with the days 用日期定义枚举

    public enum Days { Sunday, Monday, . public enum Days {Sunday,Monday ,. . . } }

  2. Create a list attribute of Days within your class 在类中创建Days的列表属性

  3. Add the required Days to your list when you need them 在需要时将所需天数添加到列表中

That seems to be what I want to do. 这似乎是我想要做的。 Question is, if enum Days is defined in my "main class", how can I have a member of Days inside another "myclass class" so that i can use myclass.someDays= Days.Monday ? 问题是,如果enum Days在我的“主类”中定义,我如何在另一个“myclass类”中拥有Days成员,以便我可以使用myclass.someDays = Days.Monday?

well assuming ur main class is mainClass and u have provided apt access perms, then depending on the definition of enum ie 1. If it is static then myclass.someDays = mainClass.Days.Monday; 假设你的主类是mainClass而你提供了apt访问权限,那么取决于枚举的定义,即1.如果它是静态的那么myclass.someDays = mainClass.Days.Monday; 2. else, u have to access it thru an object of mainClass mainClass mc = new mainClass(); 2.否则,你必须通过mainClass mainClass的对象访问它mc = new mainClass(); myclass.someDays = mc.Days.Monday; myclass.someDays = mc.Days.Monday;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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