Simple Application of enumeration

Source: Internet
Author: User

New jobs, new environments, and new projects. Haha, one of the new knowledge: enumeration. This is the most basic knowledge. I have been familiar with it before, but it is not used much. Take a closer look and find that there are quite a few places to use.

Application scenarios:

If multiple choices are stored in a form like this, it is estimated that everyone has encountered it, and there are also many solutions.

Method 1:

Set the data field to nvarchar. The save format is as follows: | id1 | Id2 | ID3 |. This is what I did in my previous project. It is absolutely not a problem to complete the requirement: When I extract the data with Id2 selected, as long as | Id2 | text data appears in this field, OK.

Method 2:

Create another table, that is, one-to-multiple table join. The inner join statement is used for extraction.

Method 3:

Use enumeration. Code:

Define enumeration:

/// <Summary> /// business type // </Summary> [flags] public Enum enumtrade {radio station advertising agent = 0x01, radio station advertisement planning = 0x02, Program Production = 0x04, Program Distribution = 0x08, audio production = 0x10, others = 0x20}

If you are not familiar with the enumerated bits, you can look at the enumerated bits.

As a data source, we can write a read method to read the data source that can be bound to the control or custom rendering. Of course, text is the name of Enum, and value is the value of enum.The database field can be set to an int type, storing the results of id1 | Id2 | id5. Note that | yes is the bitwise no operator. The result we store is the result of the bitwise no operation of the selected value. When Id2 is selected for data extraction, the result of executing the corresponding field and Id2 is not 0.

Well, needless to say, bitwise operations are also the most basic operations. Compared with fuzzy queries or connecting tables, the efficiency must be much higher, and the benefits of enumeration are also quite a lot, because an enumeration contains two values, it is quite convenient to obtain the value or name. For such a data object containing name and value, enumeration is quite convenient and efficient.

However, this solution is not perfect. It is only suitable for situations where there are not many data types. For example, you can select multiple cities for your items. For example, you can select thousands of cities. Well, it is more practical to connect tables. Haha.

Summary: enumeration is a good thing.

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.