C# 中的枚举 - 赋值
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的问题有点模糊,但如果您实际上是在询问如何构造一个可以组合值的枚举,答案是用
Flags
属性:现在,如果您有一些
Days
类型的成员,您可以为其分配组合值:更新
根据要求;以下是如何检查是否设置了某个值:
为了回答当枚举在另一个类中定义时如何在一个类中创建枚举成员的问题,看起来像这样:
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:Now, if you have some member of the type
Days
, you can assign a combined value to it:Update
On request; here is how to check whether a certain value is set:
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:
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
无法从问题中看出,但也许你想要这样的东西:
用天定义一个枚举
公共枚举天数
{
星期日,
周一,
。
。
。
}
在您的班级中创建天数的列表属性
Can't tell from the question, but maybe you intend something like this:
Define an enum with the days
public enum Days
{
Sunday,
Monday,
.
.
.
}
Create a list attribute of Days within your class
假设你的主类是 mainClass 并且你提供了 apt 访问权限,那么取决于枚举的定义,即
1.如果是静态的那么
myclass.someDays = mainClass.Days.Monday;
2.否则,你必须通过 mainClass 的对象来访问它
mainClass mc = new mainClass();
myclass.someDays = mc.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;