4
Answers

How to design/write GlobalConfig class that can be inherited

Ask a question
Peter

Peter

11y
1.3k
1
Hi.

I need to create a GlobalConfig class. But I want to derive from it in another class.

Here's an example:

public class BaseConfig {
 public string GlobalPath {get; set;}
}

public class ConfigA :  BaseConfig  {
 public string pathA {get; set;}
}

public class ConfigB :  ConfigA  {
 public string pathB {get; set;}
}

The idea behind is that I don't want to write the code multiple times and what's more important
in class ConfigA I want to set GlobalPath and have access to it in ConfigB.

In other word I want class ConfigB to have a property GlobalPath which was set in class ConfigA.

To clarify I want to have only one object of Config in memory.

When I set BaseConf.GlobalPath to 'A', I want to access it from ConfigB.GlobalPath and also get 'A'.



I always design GlobalConfig as a static class, but static classes can't be inherited.
So I tried to implement Singleton Pattern, but ConfigA can't find constructor of class BaseConfig because
it's private.

I'll appreciate all help and suggestions.
Thank you.

Answers (4)