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.

1 Answer 1

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
}
Sign up to request clarification or add additional context in comments.

2 Comments

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
You're looking for the Enum.HasFlag method - check out the docs msdn.microsoft.com/en-us/library/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.