enum or static class

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
 
G

Gregory A. Beamer

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

Abubakar

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
 
A

Abubakar

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top