Elshan
Elshan

Reputation: 7693

C# How to use property by setting multiple values

I want to do something like this.

public partial class Form1 : Form{
    // bla bla.....
}
public enum FormMode { 
        Insert,
        Update,
        Delete,
        View,
        Print
    }
private FormMode frmMode = FormMode.Insert;
        public FormMode MyFormMode
        {
            get { return this.frmMode; }
            set { this.frmMode = value; }
        }

And use it's like this.

fmDetails.MyFormMode= FormMode.Insert | FormMode.Delete | FormMode.Update;

I want to do like this.Already in .net have this type of thing.But I don't know what they use, if it's struct,enum or any other type.

Upvotes: 0

Views: 477

Answers (1)

p e p
p e p

Reputation: 6674

In order to do what you are doing, you have to declare an enum as shown below. The most important thing is that the enum values are all powers of two to allow them to be used in statements like FormMode.Insert | FormMode.Delete | FormMode.Update.

[Flags] //Not necessary, it only allows for such things such as nicer formatted printing using .ToString() http://stackoverflow.com/questions/8447/what-does-the-flags-enum-attribute-mean-in-c
public enum FormMode { 
    Insert=1,
    Update=2,
    Delete=4,
    View=8,
    Print=16
}

Upvotes: 3

Related Questions