C # bits [flags] Enumeration

Source: Internet
Author: User
In. NET, enumeration is generally used in two ways. One is to represent a unique sequence of elements, such as days in a week, and the other is to represent a variety of composite states. In this case, you need to add the [Flags] feature to the enumeration to mark it as a bit field, for example, [Flags].
Enum Styles {
ShowBorder = 1, // whether to display the border
ShowCaption = 2, // whether to display the title
ShowToolbox = 4 // whether to display the toolbox
}

In this way, we can combine multiple states with the "or" operator, for example

MyControl. Style = Styles. ShowBorder | Styles. ShowCaption;

In this case, the value of myControl. Style enumeration will be 1 + 2 = 3, and its ToString () will be changed to "Styles. ShowBorder, Styles. ShowCaption"
Here we can explain why the third value ShowToolbox can be 4, 5... but not 3. That is to say, its value should not be the compound value of the previous values. One simple method is to assign values to each item in turn using the N power of 2, for example, 1, 2, 4, 8, 16, 32, 64 .....

Here is a common example of the Flags application. For example, a simple permission system has two roles: "Admin" and "User". We can put a varchar () field in the table and store the permission word "Admin, user ". However, if the Flags type is used for enumeration, we can directly put the value of Roles. Admin | Roles. User in an int field.

The following are some common enumeration operations:
Change the enumerated value back to the enumerated object:
Styles style = (Styles) Enum. Parse (typeof (Styles), 4 );//-> Style = Styles. Toolbox;
Check whether the enumeration contains an element:
Bool hasFlag = (style & Styles. ShowBorder )! = 0 ); 

In fact, we also encounter a situation where we need to remove an element from the combination state. You can use the "^" operator:

Styles style = Styles. ShowBorder | Styles. ShowCaption;
Style = style ^ Styles. ShowBorder;

In this case, the value of the style changes to Styles. ShowCaption.

But here is a very serious problem (I have discovered it now)
We will execute it again at this time.
Style = style ^ Styles. ShowBorder;
According to our ideas, the style value is Styles. ShowCaption, which does not contain Styles. ShowBorder. Therefore, even if we remove this element, the style will not change. However, the actual style value is changed to Styles. ShowBorder | Styles. ShowCaption !! If you execute the statement again, the element is removed and the task starts again and again.
Of course, we can check before removing an element. If the enumeration contains this element, remove it:

If (style & Styles. ShowBorder )! = 0 ){
Style = style ^ Styles. ShowBorder;
}

I don't know if there are other methods to easily remove an element from the Flags enumeration state ..

Thanks to mobilebilly:
Style = style &(~ Styles. ShowBorder) to remove an element.


This article goes to http://blog.csdn.net/collinye/archive/2008/10/07/3027536.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.