HectorLector
HectorLector

Reputation: 1911

Managing constants in different projects

I have a C# Application consisting of a product core and different special cases for each customer project. I need a way to handle and manage global constants (everything at a central point) and came up with the following solution:

In project "Core":

public abstract class Config
{
   //Core values, which are always needed
   public const string Value1 = "abc";
   ...
}

In the specific project "SpecialProject1" (which references Core):

public abstract class SpecialConfig : Config
{
   //additional extra values for this special case
   public const string Value2 = "xyz";
   ...
}

This way I can inherit from my Core Config class and avoid that somebody accidently creates an instance of my Config-classes (they have no state, just fixed values).

Is there a better way to do this? Is there a problem with using "abstract" in this cases?

Upvotes: 0

Views: 820

Answers (1)

Sean
Sean

Reputation: 62542

Instread of an abstract class use a static class:

public static class Config
{
   //Core values, which are always needed
   public const string Value1 = "abc";
   ...
}

and refer to your variables as Config.Value1 rather than inheriting from the class. Since C# only allows you to inherit from one base class it's not a great idea to force uses to inherit needlessly.

Also, a static class can never be instanciated, so you don't have to worry about people creating an instance of it.

In "SpecialProject1" have a config class that relates just to the config for that project, so that you keep core config and project config seperate.

Upvotes: 2

Related Questions