locked
Enum as a Byte type RRS feed

  • Question

  • User1064403333 posted

    Say I have an Enum:

    public enum Colors : Byte {  Unknown = 0,  Red = 1,  Blue = 2,  Green = 3,  etc ... }

    and I want to show this list in a Drop Down List. Previosly when I had the enum as Int32 I could prepend:

    <asp:ListItem Value="-1">Please select</asp:ListItem>

    but not anymore because byte is valid from 0 to 255 only - ideal for keeping storage and memory low and efficient which is what I need.

    Question: Any suggestions on how to include the list option of "Please select" with a value whilst still showing Unknown as zero (due to the recommendation in below links)?

    PS. I do not want to use sbyte because that only leaves 127 positive values available. Also, I thought of using short i.e. Int16 becuase it can deal with negative numbers, but it's not as efficient because twice the memory is used when compared to byte.

    From: http://msdn.microsoft.com/en-us/library/sbbt4032(v=vs.110).aspx

    ... including a constant that has the value of 0 is recommended.

    From: http://msdn.microsoft.com/en-us/library/cc138362.aspx

    When you create an enum, select the most logical default value and give it a value of zero. That will cause all enums to have that default value if they are not explicitly assigned a value when they are created.

    Sunday, January 20, 2013 11:14 PM

Answers

  • User1759999623 posted

    As for setting your initial enum constant to 1, I do not see this as an issue.  The actual values to an enumeration are relative and should reflect how you intend to use them.

    Note also that if all of the remaining values are incremented by one, then all you need is:

    public enum Colors: Byte { Unknown = 1, Red, Blue, Green, .... }

    Furthermore, remember that flexibility in programming comes with some price.  I find it more information to be flexible in how I code as opposed to making an application "efficient" but lacks the capability to adapt and expand.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 21, 2013 8:23 AM

All replies

  • User1759999623 posted

    Frankly, if you want to truly keep it simple, then use the short.  Instead of trying to jump through hoops to save on storage, your application may become inefficient in other ways, which might impact performance.

    In the greater scheme of things, you should have plenty of memory available for your needs.  Remember that this is supposed to be a web application.  Thus, the page object is created and destroyed on each request made.

    Sunday, January 20, 2013 11:41 PM
  • User1064403333 posted

    Careed, many thanks for your response.

    When I was building the sites, I was focused on functionality becuase there was so much to do. Hence, I used int just about everywhere. Now, in hindsight I am focused on storage (in SQL Server) and performance (in the web app) because:

    • it's just not one field, rather there are many fields per record (the app grew in functionality over the years)
    • there may be many records if it turns into a cloud SaaS app

    So, how about using:

    public enum Colors : Byte {  Unknown = 1,  Red = 2,  Blue = 3,  Green = 4,  etc ... }

    i.e. starting at 1 instead of zero, thus leaving zero for the "Please select"?

    Aside form the actual data values moving up by one (which I can handle with an update), are there any negative side effects by not taking Microsoft's recommendation of starting at zero for an enum?

    Monday, January 21, 2013 1:53 AM
  • User1759999623 posted

    As for setting your initial enum constant to 1, I do not see this as an issue.  The actual values to an enumeration are relative and should reflect how you intend to use them.

    Note also that if all of the remaining values are incremented by one, then all you need is:

    public enum Colors: Byte { Unknown = 1, Red, Blue, Green, .... }

    Furthermore, remember that flexibility in programming comes with some price.  I find it more information to be flexible in how I code as opposed to making an application "efficient" but lacks the capability to adapt and expand.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 21, 2013 8:23 AM