enum or static class

  • Thread starter Thread starter mavrick_101
  • Start date Start date
M

mavrick_101

Hi,

I have some constants that I would be using to specify some properties.

I found it hard to use enums for this purpose and feel more comfortable
using a static class that would have static properties returning the
constants.

What is THE RIGHT WAY to do it?

Thnx
 
Hi,

I have some constants that I would be using to specify some
properties.

I found it hard to use enums for this purpose and feel more
comfortable using a static class that would have static properties
returning the constants.

What is THE RIGHT WAY to do it?

Thnx

Right way? It depends.


If you are talking a "property bag" type setup, having a static class,
or perhaps a singleton, works nicely. If they are different for each
user, you can use session, or at least cache by session id, etc.

If they are truly constants for the app, spinning up a singleton for the
application settings is very nice, as it is quite clean. You can then
deploy more as needed, with application deployment of course.

You can also have your singleton derive from a dictionary, or similar,
and spin it up from config files or database at startup.

The point? The correct answer depends on the use and scope of the items.


--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
Look at System.Drawing.Color for example. They chose to make it "public
struct Color" and overloaded its != and == operators, and added few methods
like FromKnownColor or GetBrightness (instance methods), and all the colors
are declared as "public static Color" followed by color name. Here the
colors are something that could have been enums, but the developer wanted
some functionality to be associated with that data structure also so he
chose a class with a lot of static data. I think that if the overloading of
operators and public instance methods were not at all required by the
developer there, he would have made it into an enum.

Just my thoughts about "enum" vs "class with static members".

...ab
 
chose a class with a lot of static data. I think that if the overloading
I meant struct, but class can also be used
Just my thoughts about "enum" vs "class with static members".
class/struct ..

Now bcuz of my misused words up there I hope you dont get confused about
classes vs structs :). But before asking here just google a bit and u'll
find a lot of clear n easy to understand info. Just in case.
 
Back
Top