Krowi
Krowi

Reputation: 1625

Enum with attribute vs class with properties

I'm working on an application and I'm wondering wether I should use an Enum with a StringValueAttribute vs a class with properties.

My first approach is Enum with a StringValueAttribute. This is the approach I'm using now. Through a method called GetStringValue() I'm able to get the resource value.

public enum Icon
{
    /// <summary>
    /// The NoruBox contains a symbol consisting of a cardboard box.
    /// </summary>
    [StringValue("/Noru;Component/NoruBox/Media/Box_512.png")]
    Box = 0,

    /// <summary>
    /// The NoruBox contains a symbol consisting of a bust.
    /// </summary>
    [StringValue("/Noru;Component/NoruBox/Media/Person_512.png")]
    Person = 1,

    // More values (total of 5).
}

The alternative approach would be a class with properties of the type Icon (which would have a Name and a Resource property. This would also give me the option to add a GetAll() method that returns a list with all the available icons.

public static class Icons
{
    public static List<Icon> GetAll()
    {
        return new List<Icon> { Box, Person, ... };
    }

    public static Icon Box = new Continent("Box", Localizer.GetString("/Noru;Component/NoruBox/Media/Box_512.png"));
    public static Icon Person = new Continent("Person", Localizer.GetString("/Noru;Component/NoruBox/Media/Person_512.png"));
    // More Icons (total of 5).
}

Which one of both would be the best approach and why? Right now, (though I'm using the Enums) the class approach looks a lot cleaner in code than with the Enum. With the Enum I have to type for example _icon.GetStringValue() vs _icon.Resource with the class.

EDIT1: A little more clarification: The Icon can be compared with one of the MessageBox except for having an extra resource reference in my project.

Upvotes: 1

Views: 5663

Answers (2)

Lorek
Lorek

Reputation: 855

Another approach would be to use an enum with extension methods for each of the additional properties you want to associate with the items in the enum. Consider the following:

public static class IconInfo
{
    public static string FileName(this Icon icon)
    {
        switch (icon)
        {
            case Icon.Box: return "/Noru;Component/NoruBox/Media/Box_512.png";
            case Icon.Person: return "/Noru;Component/NoruBox/Media/Person_512.png";
            default: return "";
        }
    }
}

public enum Icon
{
    Box = 0,
    Person = 1,
}

You could then iterate over "All" of the values similar to the following:

        foreach (Icon icon in Enum.GetValues(typeof(Icon)))
        {
            Console.WriteLine(string.Format("{0}:{1}", icon, icon.FileName()));
        }

You will need to decide whether to return a default value or throw an exception if an invalid value is passed to one of your extension methods.

Upvotes: 5

Hakan Fıstık
Hakan Fıstık

Reputation: 19521

1- Consider that using the attributes will decrease the performance because you need to make reflection of the Enum values to get the string value (this will cost) if the Performance is important in your application you can use the class way.

2- in other hand , the using of the class could give you more flexibility, May be in the later time you will add someThing to the Icon (for example the size of it) in this point also the class win.

3- I think that the using of the Classes will make the code more clean and readable over the using of the Enum.

4- From (design point of view) the Icon could be considered as an class (according to the specification of your application). if you are concentrating of the Icon in your application, then It could be better to use class.

Upvotes: 1

Related Questions