Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create an enum using numbers?

Is it possible to make an enum using just numbers in C#? In my program I have a variable, Gain, that can only be set to 1, 2, 4, and 8. I am using a propertygrid control to display and set this value. If I were to create an enum 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 would only show the available values for the gain variable. The problem is I want the gain values to read numerically an not as words. But I can not create an enum like this:

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

So is there another way of doing this? Perhaps creating a custom type?

like image 839
PICyourBrain Avatar asked Jun 01 '10 18:06

PICyourBrain


People also ask

Can enum use numbers?

Numeric EnumNumeric enums are number-based enums i.e. they store string values as numbers. Enums are always assigned numeric values when they are stored. The first value always takes the numeric value of 0, while the other values in the enum are incremented by 1.

How do I create a new enum?

An enum is defined using the enum keyword, directly inside a namespace, class, or structure. All the constant names can be declared inside the curly brackets and separated by a comma. The following defines an enum for the weekdays. Above, the WeekDays enum declares members in each line separated by a comma.


2 Answers

This isn't how enums work. An enumeration allow you to name a specific value so that you can refer to it in your code more sensibly.

If you want to limit the domain of valid numeric, enums may not be the right choice. An alternative, is to just create a collection of valid values that can be used as gains:

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

If you want to make this more typesafe, you could even create a custom type with a private constructor, define all of the valid values as static, public instances, and then expose them that way. But you're still going to have to give each valid value a name - since in C# member/variable names cannot begin with a number (although they can contain them).

Now if what you really want, is to assign specific values to entries in a GainValues enumeration, that you CAN do:

private enum GainValues { One = 1, Two = 2, Four = 4, Eight = 8 };
like image 128
LBushkin Avatar answered Oct 05 '22 00:10

LBushkin


There have been a good amount of answers on this already however I will offer another solution. If you want your control to show an actual number as opposed to 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 as opposed to a member's actual name.

like image 20
Adrian Avatar answered Oct 05 '22 00:10

Adrian