Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i get enum to contain a dash (-)?

I'm generating business objects from this schema using Enterprise Architect.

The schema has the following enumeration specification:

<xs:simpleType name="PackageMedium">
    <xs:restriction base="xs:string">
        <xs:enumeration value="NTP"/>
        <xs:enumeration value="DAT"/>
        <xs:enumeration value="Exabyte"/>
        <xs:enumeration value="CD-ROM"/>
        <xs:enumeration value="DLT"/>
        <xs:enumeration value="D1"/>
        <xs:enumeration value="DVD"/>
        <xs:enumeration value="BD"/>
        <xs:enumeration value="LTO"/>
        <xs:enumeration value="LTO2"/>
        <xs:enumeration value="LTO4"/>
    </xs:restriction>
</xs:simpleType>

Enterprise architect generates the following code but Visual Studio doesn't like the dash(-) in CD-ROM and will not compile.

public enum PackageMedium : int {
    NTP,
    DAT,
    Exabyte,
    CD-ROM,
    DLT,
    D1,
    DVD,
    BD,
    LTO,
    LTO2,
    LTO4
}

What can i do to make this work?


based on @Craig Stuntz answer i was able to find this article which helped me retrieve these special characters from the Enum.

like image 887
capdragon Avatar asked Jan 13 '12 14:01

capdragon


People also ask

Can enum values have hyphen?

Hyphens are simply not allowed.

Can enum have special characters?

@HunterMcMillen: Yes, but using if(line == SpecialChars.

Can enum contain strings?

No they cannot. They are limited to numeric values of the underlying enum type.

Can enums contain methods?

The enum class body can include methods and other fields. The compiler automatically adds some special methods when it creates an enum. For example, they have a static values method that returns an array containing all of the values of the enum in the order they are declared.


2 Answers

You can't. Full stop. However, there are workarounds. You can, e.g., use DescriptionAttribute:

public enum PackageMedium : int {
    NTP,
    DAT,
    Exabyte,
    [Description("CD-ROM")]
    CDROM,
    DLT,
    D1,
    DVD,
    BD,
    LTO,
    LTO2,
    LTO4
}

This means, unfortunately, that you have more work to do when mapping values. On the other hand, it at lest compiles.

If you don't like that, pick another workaround, e.g., a dictionary:

var dict = Enum.GetValues(typeof(PackageMedium))
               .Cast<PackageMedium>()
               .Select(v => Tuple.Create(v == PackageMedium.CDROM ? "CD-ROM" : v.ToString(), v))
               .ToDictionary(t => t.Item1, t => t.Item2);

var myEnumVal = dict["CD-ROM"];
like image 175
Craig Stuntz Avatar answered Oct 12 '22 19:10

Craig Stuntz


Short answer: No.

The reason being is that the - character is used as a token by the lexer for other purposes such as representing the binary and unary minus operator.

Your best bet is to either remove the - or replace it with some other character that is a valid character in identifier names. The only non-letter character you can generally use is _.

You can find more information in the C# Specification.

like image 37
Joshua Rodgers Avatar answered Oct 12 '22 19:10

Joshua Rodgers