3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace NumberedMusicScores
{
    public enum KeySignatures
    {
        C,
        G,
        D,
        A,
        E,
        B,
        FCress,
        CCress,
        F,
        Bb,
        Eb,
        Ab,
        Db,
        Gb,
        Cb
    }
}

I want FCress and CCress shown as F# and C# if I use it. How to achieve this?

I tried this : How to use ? character in an enum , but the Description in [Description("F#")] seems doesn't exist. (Underlined by red line, and it even doesn't shows anything to "Resolve" if I right-clicked it.

Update : Clarifications :

  1. It's not a duplicate. Since duplicate answer is not an enum, but a class which configured as an enum. I want enum solution.
  2. If it doesn't possible, then please answer "it's not possible", rather than marking it as duplicate.
  3. I already read them before I made this post.

Thank you.

Community
  • 1
  • 1
Moses Aprico
  • 1,951
  • 5
  • 30
  • 53
  • @GrantWinney still no `Description` shown. dunno what I did wrong. I'll edit my code to include libraries. – Moses Aprico Jun 15 '14 at 17:54
  • @GrantWinney Portable class library. – Moses Aprico Jun 15 '14 at 17:56
  • @GrantWinney .Net Framework 4.5 Portable Class Library (check every checkbox -winphone,silverlight,(1 more forgotten) except XBox support) – Moses Aprico Jun 15 '14 at 18:03
  • 5
    Some many duplicates pointing to more duplicates pointing to even more duplicates; so many answers either incomplete or ridiculously complicated or both. Where are those __canonicals__ when you need them?? – TaW Jun 15 '14 at 18:05
  • The workaround I would probably use is to map the enum int values into a Dictionary. (Or go for the Dictionary directly, depending what you want to do with the notes.. or even for a real class..?) Retrieving the description, even when you got it working doesn't sound inviting to me.. – TaW Jun 15 '14 at 18:33
  • @TaW indeed. It's over-complicating things. – Moses Aprico Jun 15 '14 at 18:36
  • _"... if I use it"_ is very vague and ambiguous. Use it how, where? – H H Jun 15 '14 at 19:12
  • @HenkHolterman inside the code, for e.g. `KeySignature.C#` – Moses Aprico Jun 16 '14 at 07:20

3 Answers3

8

The PCL framework won't allow the Description attribute. You could just create a simplified version of the attribute.

public class MyDescription : Attribute
{        
    public string Description = { get; private set; }

    public MyDescription(string description)
    {
       Description = description;
    }
}

Then using Thomas' answer from this thread, do this:

public static string GetDescription(this Enum value)
{
    Type type = value.GetType();
    string name = Enum.GetName(type, value);
    if (name != null)
    {
        FieldInfo field = type.GetField(name);
        if (field != null)
        {
            MyDescription attr = 
                   Attribute.GetCustomAttribute(field, 
                     typeof(MyDescription)) as MyDescription;
            if (attr != null)
            {
                return attr.Description;
            }
        }
    }
    return null;
}

For your enum:

public enum KeySignatures
{
    //...
    [MyDescription("F#")]
    FCress,
    [MyDescription("C#")]
    CCress,
    //...
}
Community
  • 1
  • 1
keyboardP
  • 68,824
  • 13
  • 156
  • 205
  • I think this will be useful to someone, even though I finally decided to use a "legal character" instead of this code. So I'll accept it. Thanks. – Moses Aprico Jun 15 '14 at 18:40
  • UPDATE : I think this will be useful to someone, even though for now I decided to use a "legal character" instead of additional code. I might change my mind in the future, so I'll accept it. Thanks. – Moses Aprico Jun 15 '14 at 18:54
1

The DescriptionAttribute is part of System.ComponentModel.

So if you want to associate text to an enum, you can do so by using System.ComponentModel;

Melvinr
  • 514
  • 2
  • 8
0

you can use System.ComponentModel.DescriptionAttribute Specify a description of your Enum, as follows:

public enum KeySignatures
    {
        C,
        G,
        D,
        [Description("F#")]
        FCress,
        [Description("C#")]
        CCress,
        //...
    }

then add a method Description for Enum, as follows:

public static class Util
    {
        public static string Description(this Enum value)
        {
            Type type = value.GetType();
            string name = Enum.GetName(type, value);
            if (name != null)
            {
                FieldInfo field = type.GetField(name);
                if (field != null)
                {
                    DescriptionAttribute attr =
                        Attribute.GetCustomAttribute(field,
                            typeof(DescriptionAttribute)) as DescriptionAttribute;
                    if (attr != null)
                    {
                        return attr.Description;
                    }
                }
            }
            return value.ToString();
        }
    }