473,385 Members | 1,661 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Enum design question?

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
Nov 15 '05 #1
6 1654
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" <do************@NO-SP-AM.aimware.com> wrote in message
news:un**************@TK2MSFTNGP10.phx.gbl...
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

Nov 15 '05 #2
Thanks for the quick reply...

"Nicholas Paldino [.NET/C# MVP]" <ni**************@exisconsulting.com> wrote
in message news:eL**************@TK2MSFTNGP09.phx.gbl...
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" <do************@NO-SP-AM.aimware.com> wrote in message
news:un**************@TK2MSFTNGP10.phx.gbl...
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


Nov 15 '05 #3
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...
Nov 15 '05 #4
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)paldino=at=exisconsulting<dot>com

"Donal McWeeney" <do************@NO-SP-AM.aimware.com> wrote in message
news:eN**************@tk2msftngp13.phx.gbl...
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...

Nov 15 '05 #5
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().ToStri ng());
******* Console.WriteLine(MyEnum.INVALID.ToString());
******* Console.WriteLine(MyEnum.INVALID.ToOrdinal().ToStr ing());
******* Console.WriteLine(MyEnum.CLOSED.ToString());
******* Console.WriteLine(MyEnum.CLOSED.ToOrdinal().ToStri ng());
******* 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.<
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #6
Thanks for the sample code... understood.

Donal

Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: andy2O | last post by:
Hello comp.lang.py, Can you help me with ideas for the following (somewhat newbie) OO design question in Python? Note, I'm using psuedo-code, not actual Python for the examples! Background:...
0
by: James Walters | last post by:
Hello, DB novice checking in here with a basic design question. I have a table called 'nms_apps' which stores information about all of our applications which we have developed/maintained for...
0
by: Krist | last post by:
Hi All, I have a database design question, pls give me some help.. I want to define tables for salesman's sales target commission . The commission could be given per EITHER sales amount of :...
1
by: Krist | last post by:
Hi All, There is some additional info I forget on this same topic I just posted. I have a database design question, pls give me some help.. I want to define tables for salesman's sales target...
1
by: dixp | last post by:
I'm new to writing multithreaded apps and I have a design question. I have a winforms app and a class which has a method that does processing which is time intensive. I want the user to be able...
2
by: Stan | last post by:
Suppose there is class with enum public enum Color { Red = 20, Black = 13 } And suppose there is a method
29
by: Brad Pears | last post by:
Here is a simple OO design question... I have a Contract class. The user can either save an existing contract or they start off fresh with a blank contract, fill in the data and then save a...
9
by: fjm | last post by:
Hey everyone, I lost my internet connection for about 5 months and finally got it back. This is one of the first places I came back to. I have to say that I had a heck of a time finding it...
2
by: RoaringChicken | last post by:
Hi. Vista Ultimate Access 2007 I'm developing an inventory database and have question on design. The database stores collection details. One item in the collection, one record. The design...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.