-2

I'm creating an enum for a field in a Class and I need one of the values to be Start-up. However, when I type this, I get an error } expected. StartUp, on the other hand, is allowed but I really need the dash to be in there..

I also have other fields with the same problem for spaces..

Does anyone know of a way that I can get the value as shown above?

Code Example:

using System;

namespace XMLConverter.Models
{
    public enum SiteStatus { Hold, Planned, Proposed, Qualifying, StartUp, Open, Enrollment Complete, Closed, Stopped, Not Selected, Withdrew }

    public class Site
    {
        public string StudyName { get; set; }
        public string SiteNumber { get; set; }
        public string SiteName { get; set; }
        public SiteStatus SiteStatus { get; set; }
    }
}
Gravinco
  • 687
  • 1
  • 9
  • 28
  • 1
    Can you please show minimal but complete program demonstrating your problem? Take a look at [2.4.2 Identifiers](https://msdn.microsoft.com/en-us/library/aa664670.aspx) – Soner Gönül Nov 16 '15 at 13:13
  • 5
    _"I need one of the values to be `Start-up`"_ - **why**? If you're serializing this type, you may be able to alter the serialized value using attributes, for example. – CodeCaster Nov 16 '15 at 13:13
  • this may help: [Get Enum from Description](http://stackoverflow.com/questions/4367723/get-enum-from-description-attribute). Set a description and use this when you want to display your enum e.g. in a gui. – Jens Nov 16 '15 at 13:19

5 Answers5

2

You can't. Identifiers in C# don't allow dashes since it is an operator, so that is the same for enum values.

There is no way around this. I would suggest to replace that for another sign, like an underscore (_). Depending on the use of the enum, you might have luck with your serializer. If you for example use JSON, there are possibilities to serialize and deserialize a value differently.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
2

Enum members are just like every other identifier, and as such their names need to satisfy a set of rules. Part of these rules means that you cannot have dashes in identifier or enum names names.

And if you think about it practically, it does not make a lot of sense. Just imagine what would happen if Start and up are two variables. Since C# ignores whitespace in expressions, Start-up would mean Start - up which is a subtraction operation.

While the enum member itself needs to be a valid identifier, you can control how it is rendered to other parties. For example, you can use the DisplayAttribute to affect how it is rendered on UIs, and you can the DataMemberAttribute to affect how it is serialized.

poke
  • 369,085
  • 72
  • 557
  • 602
0

There is no reason to be putting "-" in the enum values.

1.If you want to display it that way to your applications users you can create an attribute and check the value of the attribute when displaying the enum value.

2.as @patric pointed out replace it with a character and check the existense of the character when displaying the value and replace it with '-'

Parv Sharma
  • 12,581
  • 4
  • 48
  • 80
0

An identifier in c# cannot contain the dash character, so you can't do this.

If you need to obtain a description for an enum value, you could use the [Description] attribute, For example:

enum TestEnum
{
    [Description("Start-up")]
    StartUp
}

TestEnum val = TestEnum.StartUp;
string desc = GetEnumDescription((TestEnum)val);

Where the method GetEnumDescription looks like this: (source: How to get C# Enum description from value?)

public static string GetEnumDescription(Enum value)
{
    FieldInfo fi = value.GetType().GetField(value.ToString());

    DescriptionAttribute[] attributes =
        (DescriptionAttribute[])fi.GetCustomAttributes(
        typeof(DescriptionAttribute),
        false);

    if (attributes != null &&
        attributes.Length > 0)
        return attributes[0].Description;
    else
        return value.ToString();
}
Community
  • 1
  • 1
Alan
  • 2,962
  • 2
  • 15
  • 18
0

At the first look, it's weird, but nowadays in microservices, some other languages (like GoLang) use 'dash' in the enum name, and it is okay for them, but in c# we have to add an attribute for them to handle this problem.

using System;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;

public  class Program
{
    public static void Main()
    {
        var toolViewMode = ToolViewModeType.Extended;
        Console.WriteLine(toolViewMode.GetEnumMemberValue());
    }
}

public static class  Extensions {
    
        public static string GetEnumMemberValue<T>(this T value)
            where T : struct, IConvertible
        {
            return typeof(T)
                .GetTypeInfo()
                .DeclaredMembers
                .SingleOrDefault(x => x.Name == value.ToString())
                ?.GetCustomAttribute<EnumMemberAttribute>(false)
                ?.Value;
        }
}

public enum ToolViewModeType
{
    [EnumMember(Value = "basic-view")]
    Basic,

    [EnumMember(Value = "extended-view")]
    Extended
}
Ali.Asadi
  • 643
  • 10
  • 16