Enum design question?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Donal McWeeney

    #1

    Enum design question?

    Hi,

    I have a quick question on a design approach using enums - based on the
    model of something like the HtmlTextWriterT ag 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


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Enum design question?

    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)paldin o=at=exisconsul ting<dot>com

    "Donal McWeeney" <donal_mcweeney @NO-SP-AM.aimware.com> wrote in message
    news:unpIR0NjDH A.2704@TK2MSFTN GP10.phx.gbl...[color=blue]
    > Hi,
    >
    > I have a quick question on a design approach using enums - based on the
    > model of something like the HtmlTextWriterT ag enum. I presume that what[/color]
    the[color=blue]
    > text writer does is use the Enum.GetName method to convert the enum value[/color]
    to[color=blue]
    > its corresponding tag string.
    >
    > My question is, which is better and less overhead - using this enum[/color]
    approach[color=blue]
    > or declare a sealed class of string constants.
    >
    > Thanks
    >
    > Donal
    >
    >[/color]


    Comment

    • Donal McWeeney

      #3
      Re: Enum design question?

      Thanks for the quick reply...

      "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldi no@exisconsulti ng.com> wrote
      in message news:eL$W05NjDH A.1940@TK2MSFTN GP09.phx.gbl...[color=blue]
      > Donal,
      >
      > Well, if you use a sealed class of string constants, then you will[/color]
      have[color=blue]
      > to use reflection in order to find the value. I assume the Enum class[/color]
      does[color=blue]
      > something similar (maybe there is some optimization in there), so I would[/color]
      go[color=blue]
      > with that. Also, it makes it easier, as you don't have to write the code[/color]
      to[color=blue]
      > do the lookup.
      >
      > Hope this helps.
      >
      >
      > --
      > - Nicholas Paldino [.NET/C# MVP]
      > - nick(dot)paldin o=at=exisconsul ting<dot>com
      >
      > "Donal McWeeney" <donal_mcweeney @NO-SP-AM.aimware.com> wrote in message
      > news:unpIR0NjDH A.2704@TK2MSFTN GP10.phx.gbl...[color=green]
      > > Hi,
      > >
      > > I have a quick question on a design approach using enums - based on the
      > > model of something like the HtmlTextWriterT ag enum. I presume that what[/color]
      > the[color=green]
      > > text writer does is use the Enum.GetName method to convert the enum[/color][/color]
      value[color=blue]
      > to[color=green]
      > > its corresponding tag string.
      > >
      > > My question is, which is better and less overhead - using this enum[/color]
      > approach[color=green]
      > > or declare a sealed class of string constants.
      > >
      > > Thanks
      > >
      > > Donal
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Donal McWeeney

        #4
        Re: Enum design question?

        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...


        Comment

        • Nicholas Paldino [.NET/C# MVP]

          #5
          Re: Enum design question?

          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.


          --
          - Nicholas Paldino [.NET/C# MVP]
          - nick(dot)paldin o=at=exisconsul ting<dot>com

          "Donal McWeeney" <donal_mcweeney @NO-SP-AM.aimware.com> wrote in message
          news:eNxFVIOjDH A.3212@tk2msftn gp13.phx.gbl...[color=blue]
          > 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[/color]
          then[color=blue]
          > I would have to use the GetName method to get the string value - wheras[/color]
          with[color=blue]
          > the string constant I'd already have it...
          >
          >[/color]


          Comment

          • Jeff Louie

            #6
            Re: Enum design question?

            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.WriteLi ne(MyEnum.OPENE D.ToString());
                    Console.WriteLi ne(MyEnum.OPENE D.ToOrdinal().T oString());
                    Console.WriteLi ne(MyEnum.INVAL ID.ToString());
                    Console.WriteLi ne(MyEnum.INVAL ID.ToOrdinal(). ToString());
                    Console.WriteLi ne(MyEnum.CLOSE D.ToString());
                    Console.WriteLi ne(MyEnum.CLOSE D.ToOrdinal().T oString());
                    Console.ReadLin e();
                }
            }

            Regards,
            Jeff[color=blue]
            >If your constants were numeric, then you would still need to use[/color]
            reflection to get the name
            associated with the value.<


            *** Sent via Developersdex http://www.developersdex.com ***
            Don't just participate in USENET...get rewarded for it!

            Comment

            • Donal McWeeney

              #7
              Re: Enum design question?

              Thanks for the sample code... understood.

              Donal



              Comment

              Working...