| Nested classes have access to their parent class'es data. |
|
You would think I would know that . This is a good to know thing and I will sure look into that. However, if I'm not mistaken, this still would force me to write the implemantion of the code in each of the internal classes (or at least implement so kind of access to the protected members for the base class).
Thanks again for taking the time (and patience) to help me out.
I made a little example to clarify the track I've been following so far. The classes 'Item' and 'Thing' are 2 existing classes over which I don't have control.
class Item {
private string _color;
public string Color {
get { return _color; }
set { _color = value; }
}
private string _name;
public string Name {
get { return _name; }
set { _name = value; }
}
protected void SomeMethod(){
//do stuff here
}
}
class Thing {
private string _color;
public string Color {
get { return _color; }
set { _color = value; }
}
private string _name;
public string Name {
get { return _name; }
set { _name = value; }
}
protected void SomeMethod(){
//do stuff here
}
}
Now I would like to add an extra property to these classes that is called FullName and return the string concatenation of the Color and Name properties. This is what I got at first:
interface IExtension {
string FullName{get;}
}
class ItemEx : Item, IExtension {
public string FullName {
get { return base.Color + " " + base.Name; }
}
}
class ThingEx : Thing, IExtension {
public string FullName {
get { return base.Color + " " + base.Name; }
}
}
In both derrived classes, the implementation of the FullName property is the same and the code is duplicated. When I use nested classes, I still have to write the implementation of the FullName property in both classes (in a real world situation, the implemtation of the FullName property would be of course very complex and contain 1000's of lines of code).
My next attempt, looked something like this (the interface IExtension is the same as above):
class ExtentionBase : IExtension {
private object _parent;
public ExtentionBase(object parent) {
_parent = parent;
}
public string FullName {
get {
if (_parent is Item)
return ((Item)_parent).Color + " " + ((Item)_parent).Name;
else if (_parent is Thing)
return ((Thing)_parent).Color + " " + ((Thing)_parent).Name;
else
return null;
}
}
}
class ItemEx2 : Item, IExtension {
private IExtension _myExt;
public ItemEx2() : base()
{
_myExt = new ExtentionBase(this);
}
public string FullName {
get { return _myExt.FullName; }
}
}
class ThingEx2 : Item, IExtension {
private IExtension _myExt;
public ThingEx2()
: base() {
_myExt = new ExtentionBase(this);
}
public string FullName {
get { return _myExt.FullName; }
}
}
The implementation of the FullName property is now contained in the class ExtensionBase and does not need duplicating. There's still a lot work to be done by 'wrapping' each member of the IExtention interface in each of the classes, but I could live with that. That is, until I realized that I couldn't access the protected members of the Item or Thing class from within the ExtentionBase class (duh).
There probably isn't 'one' solution for this, but I'm looking for the best approach.
Regards
|