How to Set Space on Enum

How to give enum values that are having space

From
enum (C# Reference)

An enumerator may not contain white
space in its name.

Java enum elements with spaces?

You can't put a space in the middle of an identifier.

Doing so ends that identifier and the parser assumes whatever comes next is a valid token in that statement's context. There are few (if any) places that would be legal.

Conventional Java value names would be:

INDIA,         // Or India,
RUSSIA, // Russia,
NORTH_AMERICA; // NorthAmerica;

An enum can have associated properties, like human-readable names, e.g.,

public enum CountryAndOneContinent {
INDIA("India"),
RUSSIA("Russia"),
NORTH_AMERICA("North America");

private String displayName;

CountryAndOneContinent(String displayName) {
this.displayName = displayName;
}

public String displayName() { return displayName; }

// Optionally and/or additionally, toString.
@Override public String toString() { return displayName; }
}

I'm ambivalent about using toString to provide presentation-layer representations.

I prefer methods communicate their purpose explicitly–it's more expressive and obvious.

toString is pretty generic, and allows only a single representation. Multiple output formats may be required depending on context, parameters, etc. which toString doesn't allow.

Advantages of toString include using default string operations on the object, and in this case, using valueOf to directly translate from the human-readable version to the enum value.

Can I use a space between two words in enumeration?

It is not possible to give a space as it would generate a compilation error. If you want it for displaying purposes. Then you could write a method that returns a UI friendly strings when passed enumeration

something like:

public string GetUIFriendlyString(SoftwareEmployee employee)
{
switch (employee):
{
case TeamLeader: return "Team Leader";
// same for the rest
}
}

or use attributes in the enum as suggested by @Christian Hayter

enum with space property for dropdownlist

I have seen this generally handled by putting a [StringValue("New York")] attribute on the enum members. A quick google search returns this blog post, which has a pretty good way of doing it.

Basically make the attribute class:

public class StringValueAttribute : Attribute {

public string StringValue { get; protected set; }

public StringValueAttribute(string value) {
this.StringValue = value;
}
}

And an extension method to access it:

   public static string GetStringValue(this Enum value) {
// Get the type
Type type = value.GetType();

// Get fieldinfo for this type
FieldInfo fieldInfo = type.GetField(value.ToString());

// Get the stringvalue attributes
StringValueAttribute[] attribs = fieldInfo.GetCustomAttributes(
typeof(StringValueAttribute), false) as StringValueAttribute[];

// Return the first if there was a match, or enum value if no match
return attribs.Length > 0 ? attribs[0].StringValue : value.ToString();
}

Then your enum would look like this:

public enum EnumState{
[StringValue("New York")]
NewYork,
[StringValue("New Mexico")]
NewMexico,
}

and you can just use myState.GetStringValue();



Related Topics



Leave a reply



Submit