0

Can I do this ? It doesn't seem so.

public enum Options
{
   [Display(Name = string.Format("{0} - {1}","Option One", MyClass.myVariable))]
   OptionOne=1,
   [Display(Name = string.Format("{0} - {1}","Option Two", MyClass.myVariable))]
   OptionTwo=2
}

As opposed to this

public enum Options
{
   [Display(Name = "Option 1")]
   OptionOne=1,
   [Display(Name = "Option 2")]
   OptionTwo=2
}

If not, how can I make the Display Name for an enum variable ?

7
  • 2
    Do you mean how to use attributes with enum? Commented Oct 17, 2014 at 12:18
  • @Sinatr well I guess Display is an attribute. Updated question. Commented Oct 17, 2014 at 12:20
  • I dont think that you can make them variable, they need to be known at compile time Commented Oct 17, 2014 at 12:21
  • 1
    Attribute parameters can only be primitive constants. Commented Oct 17, 2014 at 12:22
  • @DavidPilkington I need to change the display name based on a value elsewhere. Commented Oct 17, 2014 at 12:22

4 Answers 4

3

Seems like nobody's dealing with:

If not, how can I make the Display Name for an enum variable ?

I can think about some kind of enum map plus extension method which could work like this:

using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
    public enum Foo
    {
        One = 1,
        Two = 2,
    }

    public static class ExtensionMethods
    {
        private static readonly Dictionary<Enum, string> s_EnumMap = new Dictionary<Enum, string>
            {
                { Foo.One, string.Format("{0} - {1}","Option One", 1) },
                { Foo.Two, string.Format("{0} - {1}","Option Two", 2) }
            };

        public static String ConvertToString(this Enum eff)
        {
            return s_EnumMap[eff];
        }
    }

    internal class Program
    {
        private static void Main(string[] args)
        {
            Console.WriteLine(Foo.One.ConvertToString());
            Console.WriteLine(Foo.Two.ConvertToString());
        }
    }
}

Integers 1 and 2 can be of course replaced by e.g. static variable, such as MyClass.myVariable. If that is the way you would use this code, then keep in mind that s_EnumMap will store the values that MyClass.myVariable variable had at the time when you first used ExtensionMethods class (i.e. when static fields of MyClass were getting initialized). So modifying the code like this:

public MyClass
{
    public static int myVariable = 5;
}

public static class ExtensionMethods
{
    private static readonly Dictionary<Enum, string> s_EnumMap = new Dictionary<Enum, string>
        {
            { Foo.One, string.Format("{0} - {1}","Option One", MyClass.myVariable) },
            { Foo.Two, string.Format("{0} - {1}","Option Two", 2) }
        };

    public static String ConvertToString(this Enum eff)
    {
        return s_EnumMap[eff];
    }
}

internal class Program
{
    private static void Main(string[] args)
    {
        Console.WriteLine(Foo.One.ConvertToString());
        Console.WriteLine(Foo.Two.ConvertToString());
        MyClass.myVariable = 100;
        Console.WriteLine(Foo.One.ConvertToString());
        Console.WriteLine(Foo.Two.ConvertToString());
    }
}

Would result into:

Option One - 5
Option Two - 2
Option One - 5
Option Two - 2

While after commenting out the first two Console.WriteLines, the output would be:

Option One - 100
Option Two - 2

So if you want to dynamicaly react to changes of MyClass.myVariable then you have to implement some logic to update s_EnumMap`, but as long as I don't know more about the goal you are trying to achieve I cannot provide a better answer.

Sign up to request clarification or add additional context in comments.

Comments

1

You could write a separate method to get the display name, or even a small class that has an option member and a display name member. I like Michal's idea better, but since I already started writing this I figured I'd throw it out there!

public enum Option
{
    OptionOne = 1,
    OptionTwo = 2
}

public static string GetOptionDisplayName(Option option)
{
    switch (option)
    {
        case Option.OptionOne:
            return string.Format("{0} - {1}", "Option One", MyClass.MyProperty);
        case Option.OptionTwo:
            return string.Format("{0} - {1}", "Option Two", MyClass.MyProperty);
        default:
            return option.ToString();
    }
}

public class AnotherOption
{
    public Option Option { get; set; }

    public string DisplayName
    {
        get { return GetOptionDisplayName(this.Option); }
    }
}

Comments

0

What you want cannot be done. The compiler needs to know the value at compile time.

Comments

0

The short answer no. The value within [Display....] can be known at compile time only. E.g. you can define literals, like string or enum value. string.Format() is called at run time. If possible you should call it in you other logic, using you enum. Or use code generating, e.g. with a tt template

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.