开发者

enums in C# - assignment

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 en开发者_如何学Cume 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 ?


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; }
}


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:

  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


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;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜