Enum design question?

  • Thread starter Thread starter Donal McWeeney
  • Start date Start date
D

Donal McWeeney

Hi,

I have a quick question on a design approach using enums - based on the
model of something like the HtmlTextWriterTag enum. I presume that what the
text writer does is use the Enum.GetName method to convert the enum value to
its corresponding tag string.

My question is, which is better and less overhead - using this enum approach
or declare a sealed class of string constants.

Thanks

Donal
 
Donal,

Well, if you use a sealed class of string constants, then you will have
to use reflection in order to find the value. I assume the Enum class does
something similar (maybe there is some optimization in there), so I would go
with that. Also, it makes it easier, as you don't have to write the code to
do the lookup.

Hope this helps.
 
Thanks for the quick reply...

Nicholas Paldino said:
Donal,

Well, if you use a sealed class of string constants, then you will have
to use reflection in order to find the value. I assume the Enum class does
something similar (maybe there is some optimization in there), so I would go
with that. Also, it makes it easier, as you don't have to write the code to
do the lookup.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- nick(dot)paldino=at=exisconsulting<dot>com

Donal McWeeney said:
Hi,

I have a quick question on a design approach using enums - based on the
model of something like the HtmlTextWriterTag enum. I presume that what the
text writer does is use the Enum.GetName method to convert the enum
value
to
its corresponding tag string.

My question is, which is better and less overhead - using this enum approach
or declare a sealed class of string constants.

Thanks

Donal
 
Just thinking about your reply - If I was using a sealed class of string
constants I would be using the constants for passing to arguments to
methods - I would be doing the same with the enum - but using the enum then
I would have to use the GetName method to get the string value - wheras with
the string constant I'd already have it...
 
Donal,

If the constants were strings, then yes, you would have it already.
However, in the enumeration, the values are numeric. If your constants were
numeric, then you would still need to use reflection to get the name
associated with the value.
 
Since this is not an obvious idiom. Here is a sample for lurkers.

sealed class MyEnum
{
    private String name;
    private static int nextOrdinal= 1;
    private int ordinal= nextOrdinal++;
    private MyEnum(String name)
    {
        this.name= name;
    }
    public override String ToString()
    {
        return name;
    }
    public int ToOrdinal()
    {
        return ordinal;
    }
    public static MyEnum INVALID= new MyEnum("Invalid"); // ordinal 1
    public static MyEnum OPENED= new MyEnum("Opened"); // ordinal 2
    public static MyEnum CLOSED=new MyEnum("Closed"); // ordinal 3
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
        //
        // TODO: Add code to start application here
        //
        Console.WriteLine(MyEnum.OPENED.ToString());
        Console.WriteLine(MyEnum.OPENED.ToOrdinal().ToString());
        Console.WriteLine(MyEnum.INVALID.ToString());
        Console.WriteLine(MyEnum.INVALID.ToOrdinal().ToString());
        Console.WriteLine(MyEnum.CLOSED.ToString());
        Console.WriteLine(MyEnum.CLOSED.ToOrdinal().ToString());
        Console.ReadLine();
    }
}

Regards,
Jeff
If your constants were numeric, then you would still need to use
reflection to get the name
associated with the value.<
 
Back
Top