-3

I'm trying to do an enum of Morse like so:

public enum Morse : String
{
    medzera = "       ",          
    a=".-",               
    b="-...",              
    c="-.-.",
    d="-..",
    e=".",
    f="..-.",
    g="--.",
    h="....",
    i="..",
    j=".---",
    k="-.-",
    l=".-..",
    m="--",
    n="-.",
    o="---",
    p=".--.",
    q="--.-",
    r=".-.",
    s="...",
    t="-",
    u="..-",
    v=...-",
    w=".--",
    x="-..-",
    y="-.--",
    z="--..",
}

But it throws an error.

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Arnye Mob
  • 3
  • 3

3 Answers3

4

No you can't create an enum of strings but you use Dictionary<char,string>

Dictionary<char,string> letters = new Dictionary<char,string>(){
   {'a' , ".-"},
   .....
   {'z' , "--.."}
};

Usage would be something like

var input = "az";
var output = string.Join("/", input.Select(c => letters[c]));
Eser
  • 12,346
  • 1
  • 22
  • 32
2

From MSDN:

The approved types for an enum are byte, sbyte, short, ushort, int, uint, long, or ulong.

Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
2

You are trying to have a string enum which is not possible. Every enumeration type has an underlying type, which can be any integral type except char. See Documentation for more information. You might want to check this post for alternative solutions How to define an enum with string value?

Rahul
  • 76,197
  • 13
  • 71
  • 125
  • so, if i want to have these strings there i can do it but i must add to every one some byte value pe? or it rly cant be done – Arnye Mob Jun 17 '18 at 22:54
  • @ArnyeMob nope it can't be ... not using `enum` see the linked documentation page – Rahul Jun 17 '18 at 22:55