0

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.

Elshan
  • 7,339
  • 4
  • 71
  • 106

1 Answers1

3

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
}
p e p
  • 6,593
  • 2
  • 23
  • 32
  • What is the best way of using it, ex by using switch or if statements,I mean `if(MyFormMode==FormMode.Insert){btnInsert.Enable= false;}` or single `IF ELSE` statements – Elshan Oct 14 '14 at 06:15
  • 1
    You're looking for the Enum.HasFlag method - check out the docs http://msdn.microsoft.com/en-us/library/system.enum.hasflag(v=vs.110).aspx – p e p Oct 14 '14 at 11:49