How to get all child components from base class

  • Thread starter Thread starter james
  • Start date Start date
J

james

I have a custom UserControl, which can have many sub class levels
derived from it. I want to be able to discover all the components at
Load time, but the only components I can see from the base class
are the private components internal to the base class itself. What
I want are ALL components for the entire class no matter how many
levels of sub-classing this particular control contains.
I do not want to have to force the child classes to implement a method
so that the base class can get access to its components. It would
like to hide this fact from the child classes, and just handle it in the
base. Is there any way at all I can do this ?

Thanks in advance

JIM
 
James,

I don't think that this is possible. The parents have no sense of the
children unless the children explicitly expose something that the parent can
access. If the parent has to know what classes can derive from it, then it
would make things very hairy, since the possibility of a class deriving from
it would be high, and difficult to track (how do you know there is not a
class in some assembly that derives from you, and how do you dynamically
link to it and access the type? It's just not possible).

Hope this helps.
 
James,

This is something that is easily remedied. You can make it so that the
classes that you design all implement a pattern such that they expose their
components in a way that is accessible through a sub-class. Just have a
protected property which has the components.

Or do you need to be able to access this information for any type?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

james said:
Well, actually what you say isn't totally correct. Since this is a
UserControl,
all sub classes will always be user controls, and since my Base class can
handle the Load event, It does not need to know anything about the subclass
because it already knows that all UserControls are the same, they have
components.
So, while you are probably correct on the point that it isn't possible due
to the protection level, your second point about sub classes in different
assemblies does not follow in this particular case.

This brings up a design flaw in Controls that I have always thought could be
made better. Why does each sub-class have it's own private components.
Shouldn't the components be protected instead, so that each sub-class shares
the list and can add it components to that shared list ?

Thanks

JIM


in message news:[email protected]...
James,

I don't think that this is possible. The parents have no sense of the
children unless the children explicitly expose something that the parent can
access. If the parent has to know what classes can derive from it, then it
would make things very hairy, since the possibility of a class deriving from
it would be high, and difficult to track (how do you know there is not a
class in some assembly that derives from you, and how do you dynamically
link to it and access the type? It's just not possible).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

james said:
I have a custom UserControl, which can have many sub class levels
derived from it. I want to be able to discover all the components at
Load time, but the only components I can see from the base class
are the private components internal to the base class itself. What
I want are ALL components for the entire class no matter how many
levels of sub-classing this particular control contains.
I do not want to have to force the child classes to implement a method
so that the base class can get access to its components. It would
like to hide this fact from the child classes, and just handle it in the
base. Is there any way at all I can do this ?

Thanks in advance

JIM
 
Well, actually what you say isn't totally correct. Since this is a
UserControl,
all sub classes will always be user controls, and since my Base class can
handle the Load event, It does not need to know anything about the subclass
because it already knows that all UserControls are the same, they have
components.
So, while you are probably correct on the point that it isn't possible due
to the protection level, your second point about sub classes in different
assemblies does not follow in this particular case.

This brings up a design flaw in Controls that I have always thought could be
made better. Why does each sub-class have it's own private components.
Shouldn't the components be protected instead, so that each sub-class shares
the list and can add it components to that shared list ?

Thanks

JIM


Nicholas Paldino said:
James,

I don't think that this is possible. The parents have no sense of the
children unless the children explicitly expose something that the parent can
access. If the parent has to know what classes can derive from it, then it
would make things very hairy, since the possibility of a class deriving from
it would be high, and difficult to track (how do you know there is not a
class in some assembly that derives from you, and how do you dynamically
link to it and access the type? It's just not possible).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

james said:
I have a custom UserControl, which can have many sub class levels
derived from it. I want to be able to discover all the components at
Load time, but the only components I can see from the base class
are the private components internal to the base class itself. What
I want are ALL components for the entire class no matter how many
levels of sub-classing this particular control contains.
I do not want to have to force the child classes to implement a method
so that the base class can get access to its components. It would
like to hide this fact from the child classes, and just handle it in the
base. Is there any way at all I can do this ?

Thanks in advance

JIM
 
Nicholas,

You are correct, I can do this, but not exactly the way you describe.
The method would have to be recursive, with the base class calling it
at load time, then the bottom most child class adding his components to
an IList and appending his parents components via a recursive call
and so on, so that each class, on up to the root will append their
components
to the IList, then the root class will have one big IList of all components.
Remember, I can have an unlimited number of child classes like so
A->B->C->D with A being the root control and D being the child.
Each class, A, B, C, D will have components.

The problem with this solution is that I have to copy/re-implement the same
routine in each child class, which leaves room for error. What if the
designer of class C or D does not implement it correctly.

I would rather do it all from the root, hiding the fact from B, C, and D
that
anything is happening


JIM


Nicholas Paldino said:
James,

This is something that is easily remedied. You can make it so that the
classes that you design all implement a pattern such that they expose their
components in a way that is accessible through a sub-class. Just have a
protected property which has the components.

Or do you need to be able to access this information for any type?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

james said:
Well, actually what you say isn't totally correct. Since this is a
UserControl,
all sub classes will always be user controls, and since my Base class can
handle the Load event, It does not need to know anything about the subclass
because it already knows that all UserControls are the same, they have
components.
So, while you are probably correct on the point that it isn't possible due
to the protection level, your second point about sub classes in different
assemblies does not follow in this particular case.

This brings up a design flaw in Controls that I have always thought
could
be
made better. Why does each sub-class have it's own private components.
Shouldn't the components be protected instead, so that each sub-class shares
the list and can add it components to that shared list ?

Thanks

JIM


in message news:[email protected]...
James,

I don't think that this is possible. The parents have no sense of the
children unless the children explicitly expose something that the
parent
can
access. If the parent has to know what classes can derive from it,
then
it
would make things very hairy, since the possibility of a class
deriving
from
it would be high, and difficult to track (how do you know there is not a
class in some assembly that derives from you, and how do you dynamically
link to it and access the type? It's just not possible).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I have a custom UserControl, which can have many sub class levels
derived from it. I want to be able to discover all the components at
Load time, but the only components I can see from the base class
are the private components internal to the base class itself. What
I want are ALL components for the entire class no matter how many
levels of sub-classing this particular control contains.
I do not want to have to force the child classes to implement a method
so that the base class can get access to its components. It would
like to hide this fact from the child classes, and just handle it in the
base. Is there any way at all I can do this ?

Thanks in advance

JIM
 
Back
Top