1

I work in C#, Visual Studio '05... In my enum, how can I use the '?' character? My enum is below:

public enum Questions
{
    How_old_are_you?_= 0,//How to set the ? character 
    What_is_your_name= 1
}

After I add the '?' character there are some errors.

Matthew Scharley
  • 127,823
  • 52
  • 194
  • 222

6 Answers6

13

? isn't a valid character in a C# identifier (including enum values). I strongly suggest you use something like:

public enum Question
{
    [Description("How old are you?")]
    Age,

    [Description("What is your name?")]
    Name
}

Or have a map or a resource file etc - just don't try to make the name of the enum into a natural language description.

EDIT: For reference, if you want to get at the Description from code, here's how to do it (thanks Christian Hayter):

((DescriptionAttribute) Attribute.GetCustomAttribute(
    typeof(Question).GetField("Age", BindingFlags.Public | BindingFlags.Static), 
        typeof(DescriptionAttribute), false)
).Description;
Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Perhaps some code on how to get the value of the description would be in order too, since I believe this would be useful in the context of the question. (And I'm curious.) – Matthew Scharley Jul 26 '09 at 09:43
  • 4
    ((DescriptionAttribute) Attribute.GetCustomAttribute(typeof(Question).GetField("Age", BindingFlags.Public | BindingFlags.Static), typeof(DescriptionAttribute), false)).Description – Christian Hayter Jul 26 '09 at 09:59
  • Thanks for the edits - I had to dash straight after writing this answer. – Jon Skeet Jul 26 '09 at 15:03
2

You can't. ? is an operator, so a sequence of characters containing it is not a valid identifier.

Niki
  • 15,662
  • 5
  • 48
  • 74
1

You can not use question marks in the enum identifiers. The identifiers in an enumeration follows the same rules as other identifiers.

You can use the @ character to use reserved keywords as identifiers, but you still can't use characters that are not allowed in an identifier.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0

You can add q after the question to make it descriptive. Ex:

  How_old_are_you_q= 0,        //Replace ? with q
    What_is_your_name_q= 1
Loai Abdelhalim
  • 1,901
  • 4
  • 24
  • 28
0

You can tag your Enum items with underscore characters, which are allowed in Enum, and replace them for display.

Eg Binding an Enum to a ComboBox, say, replacing first "__" with "?", then "_" with space Enum item How_old_are_you__ becomes ComboBox item "How old are you?"

I use this when I want to stick with Enum over other options. It works well for partially loading Enum items into lists and display controls as well.

SteveCinq
  • 1,920
  • 1
  • 17
  • 22
0

Another possible solution could be a mix between an enum and a dictionary :

public enum Question
{
  Age,
  Name
}

...

Dictionary<Question,string> Questions = new Dictionary<Question,string>();

Questions.Add(Question.Age,  "How old are you ?");
Questions.Add(Question.Name, "What is your name ?");
Mike Verrier
  • 484
  • 2
  • 16