By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,661 Members | 1,508 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,661 IT Pros & Developers. It's quick & easy.

How to get all child components from base class

P: n/a
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

Nov 13 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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]
- ni**************@exisconsulting.com

"james" <no****@hypercon.net> wrote in message
news:Oc****************@TK2MSFTNGP12.phx.gbl...
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

Nov 13 '05 #2

P: n/a
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 [.NET/C# MVP]" <ni**************@exisconsulting.com> wrote
in message news:eH**************@TK2MSFTNGP12.phx.gbl...
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]
- ni**************@exisconsulting.com

"james" <no****@hypercon.net> wrote in message
news:Oc****************@TK2MSFTNGP12.phx.gbl...
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


Nov 13 '05 #3

P: n/a
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]
- ni**************@exisconsulting.com

"james" <no****@hypercon.net> wrote in message
news:ey**************@TK2MSFTNGP12.phx.gbl...
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 [.NET/C# MVP]" <ni**************@exisconsulting.com> wrote in message news:eH**************@TK2MSFTNGP12.phx.gbl...
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]
- ni**************@exisconsulting.com

"james" <no****@hypercon.net> wrote in message
news:Oc****************@TK2MSFTNGP12.phx.gbl...
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



Nov 13 '05 #4

P: n/a
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 [.NET/C# MVP]" <ni**************@exisconsulting.com> wrote
in message news:eO*************@TK2MSFTNGP11.phx.gbl...
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]
- ni**************@exisconsulting.com

"james" <no****@hypercon.net> wrote in message
news:ey**************@TK2MSFTNGP12.phx.gbl...
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 [.NET/C# MVP]" <ni**************@exisconsulting.com>

wrote
in message news:eH**************@TK2MSFTNGP12.phx.gbl...
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]
- ni**************@exisconsulting.com

"james" <no****@hypercon.net> wrote in message
news:Oc****************@TK2MSFTNGP12.phx.gbl...
> 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
>
>
>



Nov 13 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.