1

i have to create an enum that contains values that are having spaces

public enum MyEnum
        {
            My cart,
            Selected items,
            Bill            
        }

This is giving error. Using concatenated words like MyCart or using underscore My_Cart is not an option. Please guide.

Thanks in advance.

HotTester
  • 5,620
  • 15
  • 63
  • 97
  • Possible duplicate - http://stackoverflow.com/questions/1187085/string-to-enum-conversion-in-c – ChrisF Mar 03 '10 at 10:13
  • 1
    In fact see this answer - http://stackoverflow.com/questions/1187085/string-to-enum-conversion-in-c/1187098#1187098 - to the above question – ChrisF Mar 03 '10 at 10:14
  • possible duplicate of [How to set space on Enum](http://stackoverflow.com/questions/1101872/how-to-set-space-on-enum) – nawfal Jun 09 '13 at 12:10
  • Possible duplicate of [How to set space on Enum](https://stackoverflow.com/questions/1101872/how-to-set-space-on-enum) – StayOnTarget Apr 11 '18 at 19:28
  • Related answer on another post - [Can my enums have friendly names?](https://stackoverflow.com/a/1415460/465053) – RBT May 03 '21 at 11:13

5 Answers5

4

From enum (C# Reference)

An enumerator may not contain white space in its name.

Adriaan Stander
  • 162,879
  • 31
  • 289
  • 284
  • Comments have been posted to your question regarding displaying the enum without the underscore, if you so wish. – Adriaan Stander Mar 03 '10 at 10:15
  • @HotTester You can use `DisplayAttribute` - See my marvelous answer to the question https://stackoverflow.com/questions/1101872/how-to-set-space-on-enum/31988435#31988435 – Luke T O'Brien Aug 24 '17 at 13:39
2

Enum just cant have space! What do you need it for? If you need it simply for display purpose, you can stick with underscore and write an extension method for your enum so that you can ask for the display text by doing this (assuming your ext method is call DisplayText). Internally you just implement the DisplayText method to substitute "_" with space

MyEnum.My_Cart.DisplayText();   // which return "My Cart"
Fadrian Sudaman
  • 6,405
  • 21
  • 29
1

As per the C# specification, "An enumerator may not contain white space in its name." (see http://msdn.microsoft.com/en-us/library/sbbt4032.aspx) Why do you need this?

Will
  • 1,561
  • 9
  • 6
1

**

enums can't have spaces in C#!" you say. Here is the way System.ComponentModel.DescriptionAttribute to add a more friendly description to the enum values. The example enum can be rewritten like **

public enum DispatchTypes
    {
        Inspection = 1,
        LocalSale = 2,[Description("Local Sale")]
        ReProcessing=3,[Description("Re-Processing")]
        Shipment=4,
        Transfer=5
    }

Hence it returns "LocalSale" or "ReProcessing", when we use .ToString()

Jishnu KM
  • 234
  • 1
  • 8
-2

I agree the use of DisplayText if you are following convention. But if you need different display value to represent the enum constant, then you could have a constructor passing that value.

public enum MyEnum { My cart ("Cart"), Selected items("All Selected Items"), Bill("Payment");

private String displayValue; 
private MyEnum(String displayValue) {

this.displayValue = displayValue; }
public String displayText() { return this.displayValue; }

}

You can have a displayText method or a toString method which would return the displayValue

hp.
  • 44
  • 1
  • This doesn't compile - an Enum is simply a list of values, it cannot contain members, constructors, etc. The most you can do is assign a specific integer value to a constant, but even then you cannot use spaces in the constant names. – Andy Shellam Mar 03 '10 at 10:43