C# 中的枚举 - 赋值

发布于 2024-08-26 06:00:14 字数 713 浏览 5 评论 0原文

在 C# 中如何做到这一点?

假设 myclass 有:

private enum Days{};


1) 如何借助构造函数向 myclass 内的枚举添加数据?如:

myclass my = new myclass(Monday,Friday);

以便类内的枚举获得“星期一,星期五”属性。


2) 一旦初始化后,还可以为类内部的枚举创建一个属性吗?例如:

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

编辑:

我想做的是:

在主类中有一个枚举,例如 enum Days{Monday,Tuesday,Wednesday,Thursday,Friday};

我还会在 myclass (枚举?) 中包含一些内容,以便我可以将 Days 枚举中的一些值分配给 myclass 内部(枚举?),仅使用 Days 枚举中的一些值,而不是全部值。

这样我就可以获得一个包含(枚举?)“星期一,星期二”的 myclass,而某些 myclass222 包含(枚举?)“星期五”等。

我不知道枚举是否是在内部执行此操作的正确方法myclass 课程?

How does one do this in c#?

Let's say that myclass has :

private enum Days{};

1) How does one add data to the enum inside the myclass with the help of the constructor? 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 ? 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.

So that I can get a myclass which contains an (enum?) with "Monday,Tuesday", while some myclass222 contains (enum?) "Friday" etc.

I don't know if the enum is the correct way to do this inside the myclass classes ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

一人独醉 2024-09-02 06:00:14

您的问题有点模糊,但如果您实际上是在询问如何构造一个可以组合值的枚举,答案是用 Flags 属性:

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

现在,如果您有一些 Days 类型的成员,您可以为其分配组合值:

Days someDays = Days.Monday | Days.Wednesday;

更新
根据要求;以下是如何检查是否设置了某个值:

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

为了回答当枚举在另一个类中定义时如何在一个类中创建枚举成员的问题,看起来像这样:

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

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

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]
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 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; }
}
山人契 2024-09-02 06:00:14

1)你不能。枚举必须在编译时定义

似乎您可能会寻找 AttributeCollection。 (看看 Fredrik Mörk 的回答,这就是我想要谈论的;))

2)my.Days = Days.Tuesday

1) You can't. The enum must be defined at compile time

It seems like you might look for an AttributeCollection instead. (have a look at Fredrik Mörk's answer, that it what I was trying to talk about ;) )

2) my.Days = Days.Tuesday

可是我不能没有你 2024-09-02 06:00:14

无法从问题中看出,但也许你想要这样的东西:

  1. 用天定义一个枚举

    公共枚举天数
    {
    星期日,
    周一,



    }

  2. 在您的班级中创建天数的列表属性

  3. 在需要时将所需的天数添加到您的列表中

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,
    .
    .
    .
    }

  2. Create a list attribute of Days within your class

  3. Add the required Days to your list when you need them
享受孤独 2024-09-02 06:00:14

这似乎就是我想做的。
问题是,如果定义了枚举 Days
在我的“主课”中,我怎样才能有一个
另一个“myclass”内的Days成员
类”这样我就可以使用
myclass.someDays= Days.Monday ?

假设你的主类是 mainClass 并且你提供了 apt 访问权限,那么取决于枚举的定义,即
1.如果是静态的那么
myclass.someDays = mainClass.Days.Monday;
2.否则,你必须通过 mainClass 的对象来访问它
mainClass mc = new mainClass();
myclass.someDays = mc.Days.Monday;

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 ?

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;
2. else, u have to access it thru an object of mainClass
mainClass mc = new mainClass();
myclass.someDays = mc.Days.Monday;

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