tags:

views:

86

answers:

2

Greetings,

I'm a beginner to OO and programming and I have the following situation:

I have a set of const values and enums that several of the classes I implement share.

These classes are independent i.e. other than sharing these values, they do not interact with each other, so inheriting them won't work.

I was wondering; If I create an empty class with just these consts and enums and then declare an object of these class in each class then i could use these values like:

globals.enum.enummember?

Is this sound programming or is there a better way of doing this?

Thanks for your time.

+2  A: 

The best practice is to simply declare the enums alongside with your classes, not nested in a class. Actually, nested types are usually a bad idea, unless they are private. There are exceptions, of course, but for the most part you're better without nesting.

Constants have to be defined in a class, but you don't need to instantiate an object of the class to use them. Also they can be public, so you don't need to inherit anything. If you think about it hard enough, you should be able to figure out a good name for the class that contains these constants.

Practice shows that constants and enums rarely are simply global. Most of the time they are closely coupled with one or few classes. You should then define these constants as a part of the appropriate class and put enums in the same namespace as the class using them.

Vilx-
+1, As long as it is not `public const int Yes = 1;` :) There are also caveats with using `const` versus a `static readonly`, but I doubt those will matter in this case.
sixlettervariables
You should then make it a YesNo enum and put it in some root namespace of your project. Although again - the need for such an enum, especially on a global level, already hints at some design flaw. This needs a closer inspection.
Vilx-
+1  A: 

You can use static class with static members to achieve desired behavior:

namespace A {
    public static class Enum1 {
       public static readonly int EnumMember1 = 1;
       public static readonly int EnumMember2 = 2;
       public static readonly int EnumMember3 = 3;
    }
}

You should use this in the following way:

int x = A.Enum1.EnumMember2;
Andrey Shvydky
WTF? o_O How is this related to the question?
Vilx-
Also - this is 100% equivalent to a simple enum. WTF x2.
Vilx-
huh? looks related to me. WTF @ your WTF
fearofawhackplanet
Looks to me like it matches with the question, and is a good answer. The question was regarding using enums and constants in an application accessible by many different, unrelated classes. A static class to contain the different bits of information seems good.
Codesleuth
Firstly - with what is this different from a plain enum? Secondly - the question was about style. Where to define enums and constants, not how to define them.
Vilx-
Thirdly - this is NOT equivalent to an Enum.
sixlettervariables
It is equivalent to an enum in all but the name. An enum is also just a plain int with some predefined values - as is this. Except that this forgoes what little type safety an Enum has by being directly an int. You won't even get Intellisense when working with variables of this "pseudo-enum" type.
Vilx-
If that provided type safety, then yes, it would be equivalent to an enum. Since it does not, it is not an enum. He could fake it better with a class by making it non-static w/ a private constructor, provide implicit casts from int to Enum1 and explicit casts to int from Enum1, along with static members which contained the appropriate values. Then it would be "equivalent" minus being a reference type.
sixlettervariables
Ok. Seems that Vilx is correct and this is very like to enum. But this class is just example. Change fields type to string and this will be not like to enum! But this will be a good place for global string constants. Guys?
Andrey Shvydky