How can I create an enum using numbers?
Asked Answered
T

7

13

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?

Thou answered 1/6, 2010 at 18:19 Comment(0)
A
18

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 };
Auspicate answered 1/6, 2010 at 18:21 Comment(2)
You'd probably want to make this static readonly, and maybe internal or public depending on scope requirements, but yeah, this is the best solution.Sloan
@Cylon Cat: Yes, good point about the static/readonly, although in practice, you'd need it to be a collection like List<int> rather than an array to really enforce immutability.Auspicate
S
15

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.

Stedfast answered 9/6, 2013 at 21:1 Comment(0)
U
6

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

You can get numbers listed in an enum in the inspector by simply using an underscore prefix, e.g:

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

Hope this is helps!

Unpack answered 11/11, 2012 at 14:41 Comment(0)
A
4
private enum GainValues { One = 1, Two = 2, Four = 4, Eight = 8 }

should work.

Update: OK, I think I misunderstood you there.

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

Australorp answered 1/6, 2010 at 18:21 Comment(5)
It's not clear that this is what the OP is looking for. It looks more like he wants to use the numeric literal values as the names of the enumeration members - which is illegal.Auspicate
@LBushkin: given the description it is probably what the OP wants.Phillisphilly
@Henk: I'm not so sure, the OP writes: The problem is I want the gain values to read numerically an not as words. My interpretation is the OP wants a limited list of valid values for gains, rather than a named enumeration.Auspicate
Right, the PropertyGrid applies the ToString(). Maybe the Description attribute can help, I'm not sure: blogs.msdn.com/b/paulwhit/archive/2008/03/31/…Phillisphilly
@ALL: basically I want the drop-down in the property grid to show only 1, 2, 4 and 8 as the options for the gain.Thou
T
0

use explicit value assignment in the enum:

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

Then to enumerate through these values do as follows:

GainValues currentVal;

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

Then you can cast to/from ints as necessary:

int valueFromDB = 4;

GainValues enumVal = (GainValues) valueFromDB;

// enumVal should be 'Four' now
Trollop answered 1/6, 2010 at 18:21 Comment(0)
D
0

Unfortunately, symbols in C# can contain numbers, but cannot start with numbers. So you're gonna have to use words.

Alternatively, you could do Gain1, Gain2, etc.

Or you could forgo an enum altogether and use constants and internal processing.

Duodiode answered 1/6, 2010 at 18:22 Comment(0)
R
0

You can use one custom list as a datasource for your drop down list.

Code Behind:

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>

In addition, you can have enum for setting default value, ...

hope this helps

Ruttger answered 1/6, 2010 at 20:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.