Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set space on Enum

Tags:

c#

People also ask

Can you have spaces in enum?

Normally, the name of an enum in c# can't contain any special characters or spaces. There can be situations where we need to use friendly names for enums which have spaces in between. You will get syntax error since this is not the correct way of enum declaration.

Can enum have spaces Java?

The problem has nothing (specifically) to do with enums: in Java, names can't have spaces. Try eliminating the space (using capitalization to tell the bits apart) or use underscores instead.

How much space does enum take in C?

On an 8-bit processor, enums can be 16-bits wide. On a 32-bit processor they can be 32-bits wide or more or less. The GCC C compiler will allocate enough memory for an enum to hold any of the values that you have declared. So, if your code only uses values below 256, your enum should be 8 bits wide.

Should enum values be capital?

Because they are constants, the names of an enum type's fields are in uppercase letters. You should use enum types any time you need to represent a fixed set of constants.


You can decorate your Enum values with DataAnnotations, so the following is true:

using System.ComponentModel.DataAnnotations;

public enum Boys
{
    [Display(Name="Good Boy")]
    GoodBoy,
    [Display(Name="Bad Boy")]
    BadBoy
}

I'm not sure what UI Framework you're using for your controls, but ASP.NET MVC can read DataAnnotations when you type HTML.LabelFor in your Razor views.

Here' a Extension method

If you are not using Razor views or if you want to get the names in code:

public class EnumExtention
{
    public Dictionary<int, string> ToDictionary(Enum myEnum)
    {
        var myEnumType = myEnum.GetType();
        var names = myEnumType.GetFields()
            .Where(m => m.GetCustomAttribute<DisplayAttribute>() != null)
            .Select(e => e.GetCustomAttribute<DisplayAttribute>().Name);
        var values = Enum.GetValues(myEnumType).Cast<int>();
        return names.Zip(values, (n, v) => new KeyValuePair<int, string>(v, n))
            .ToDictionary(kv => kv.Key, kv => kv.Value);
    }
}

Then use it:

Boys.GoodBoy.ToDictionary()

You are misunderstanding what an enum is used for. An enum is for programming purposes, essentially giving a name to a number. This is for the programmer's benefit while reading the source code.

status = StatusLevel.CRITICAL; // this is a lot easier to read...
status = 5;                    // ...than this

Enums are not meant for display purposes and should not be shown to the end user. Like any other variable, enums cannot use spaces in the names.

To associate internal values with "pretty" labels you can display to a user, can use a dictionary or hash.

myDict["Bad Boy"] = "joe blow";

Think this might already be covered, some suggestions:

  • How do I have an enum bound combobox with custom string formatting for enum values?
  • C# Getting Enum values

Just can't beat stackoverflow ;) just sooo much on here nowdays.


using System.ComponentModel;

then...

public enum category
{
    [Description("Good Boy")]
    goodboy,
    [Description("Bad Boy")]
    badboy
}

Solved!!