Click here to Skip to main content
11,701,590 members (68,974 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# static Singleton
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.
Posted 30-Jan-13 23:09pm
Edited 31-Jan-13 0:22am
v3

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Change the private constructor to protected.

Good luck!
  Permalink  
Comments
GigaKatowice at 31-Jan-13 6:19am
   
Hi. Changing private to protected allowed me to compile my code, but my solution doesn't work like I want. 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'.
E.F. Nijboer at 31-Jan-13 7:13am
   
In that case you need encapsulation instead of inheritance. Meaning both classes use the (static) singleton class but do not inherit from it. Inheritance will create two instances and therefor also will create an instance of everything inherited from BaseConfig. Inheritance will only work for code reuse and in this case you want to share instance data.
GigaKatowice at 1-Feb-13 4:24am
   
Hi. You are right. I tried solution with inheriting from singleton, but I get two instances of BaseConfig. One for BaseConfig itself, and second for ConfigA instance.

Maybe you can give an example of your idea, because I have no idea how to implement your solution.

Thank you for all your help.
E.F. Nijboer at 1-Feb-13 5:03am
   
It is actually quite simple. When you now inherit ConfigA and ConfigB from BaseConfig, it will use GlobalConfig for every instance.

public static class GlobalConfig {
public static string GlobalPath {get; set;}
}

public class BaseConfig {
public string GlobalPath {
get {
return GlobalConfig .GlobalPath;
}
set {
GlobalConfig .GlobalPath = value;
}
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 320
1 OriginalGriff 294
2 Maciej Los 275
3 Mika Wendelius 159
4 So_Keeee 100
0 OriginalGriff 8,324
1 Sergey Alexandrovich Kryukov 7,422
2 CPallini 4,912
3 Maciej Los 4,196
4 Mika Wendelius 3,556


Advertise | Privacy | Mobile
Web03 | 2.8.150819.1 | Last Updated 31 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100