How to create an enumeration using numbers?

Is it possible to do an enumeration using only numbers in C #? In my program, I have a Gain variable that can only be set to 1, 2, 4, and 8. I use the propertygrid control to display and set this value. If I needed to create an enumeration like this ...

private enum GainValues {One, Two, Four, Eight} 

and I made my gain variable of type GainValues, then the drop-down list in the propertygrid will show only the available values ​​for the gain variable. The problem is that I want the gain values ​​to be read numerically not like words. But I cannot create an enumeration as follows:

  private enum GainValues {1,2,4,8} 

So is there another way to do this? Is it possible to create a custom type?

+7
c # properties numeric grid
Jun 01 '10 at 18:19
source share
7 answers

This does not work enums . Enumeration allows you to specify a specific value so that you can more intelligently refer to it in your code.

If you want to limit the scope, enums may not be the right choice. An alternative is to simply create a set of valid values ​​that can be used as a gain:

 private int[] ValidGainValues = new []{ 1, 2, 4, 8}; 

If you want to make it more typical, you can even create your own type using a private constructor, define all valid values ​​as static, public instances, and then expose them that way. But you still have to give each correct value a name - because in the names of members / variables, C # cannot start with a number (although they can contain them).

Now, if you really want to, you need to assign specific values ​​to the entries in the GainValues ​​enum , which you CAN:

 private enum GainValues { One = 1, Two = 2, Four = 4, Eight = 8 }; 
+15
Jun 01 '10 at 18:21
source share

There have already been many answers, but I am proposing a different solution. If you want your control to show the actual number, not the English word for the number in your control, you can use data annotations.

 using System.ComponentModel.DataAnnotations; private enum GainValues { [Display(Name = "1")] One, [Display(Name = "2")] Two, [Display(Name = "4")] Four, [Display(Name = "8")] Eight } 

This is useful anywhere in your program where you want to use a friendly name rather than the actual member name.

+10
Jun 09 '13 at 21:01
source share

If I understood the original question correctly, the answer may be as follows:

You can get the numbers listed in the listing in the inspector simply by using the underscore prefix, for example:

 public enum MyNumbers{ _1, _2, _3, _4 }; public MyNumbers myNumbers; 

Hope this helps!

+5
Nov 11 '12 at 14:41
source share
 private enum GainValues { One = 1, Two = 2, Four = 4, Eight = 8 } 

must work.

Update: OK, I think I misunderstood you.

Perhaps you could use KeyValuePair<string, int> , and then bind the name and value to the Key and Value property, respectively.

+4
Jun 01 '10 at 18:21
source share

use an explicit value assignment in an enumeration:

 private enum GainValues { One = 1, Two = 2, Four = 4, Eight = 8 } 

Then, to list these values, do the following:

 GainValues currentVal; foreach(currentVal in Enum.GetValues(typeof(GainValues)) { // add to combo box (or whatever) here } 

Then you can direct to / from ints if necessary:

 int valueFromDB = 4; GainValues enumVal = (GainValues) valueFromDB; // enumVal should be 'Four' now 
0
Jun 01 '10 at 18:21
source share

Unfortunately, characters in C # can contain numbers, but cannot begin with numbers. Therefore you will have to use words.

Alternatively, you can use Gain1, Gain2, etc.

Or you can refuse the enumeration as a whole and use constants and internal processing.

0
Jun 01 '10 at 18:22
source share

You can use one custom list as a data source for a drop-down list.

Code for:

 using GainItem = KeyValuePair<string, int>; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { List<GainItem> dic = new List<GainItem>(); dic.Add(new GainItem("First", 1)); dic.Add(new GainItem("Second", 2)); dic.Add(new GainItem("Fourth", 4)); ddl.DataSource = dic; ddl.DataBind(); } } protected void btn_Click(object sender, EventArgs e) { Response.Write(ddl.SelectedValue); } } 

Asp Page:

  <div> <asp:DropDownList runat="server" ID="ddl" DataValueField="Value" DataTextField="Key" /> <asp:Button ID="btn" runat="server" OnClick="btn_Click" /> </div> 

Alternatively, you can have an enumeration to set the default value, ...

hope this helps

0
Jun 01 '10 at 20:15
source share



All Articles