Inheritance in forms which inherit template forms

G

Guest

I have this "template" form which is based on System.Windows.Forms.Form. I
created a new form(the derived form) which inherits this form.

I have observed that if, at runtime, I run through all of the controls in
the controls collection of the "template" form, I can have access to all the
controls that exist in the derived form! Now this seems odd to me; why should
a base class have the controls of a class above it (the derived form), in its
controls collection.

Certainly I would expect the class derived from the template to see all the
controls on the base class but not the other way around.
 
J

joeycalisay

why should
a base class have the controls of a class above it (the derived form), in its
controls collection.

Technically, child classes are DOWN not up/above the hierarchy. Anyway, the
CONTROLS (Control Collection) property of the form is declared as public and
is therefore inherited down the hierarchy tree. Custom controls added in
your child forms are also added to this inherited property and if you are
debugging after the child class was instantiated and the Controls in it are
added (usually in InitializeComponent method), you will normally see them.
Your base form, child form all share the same Controls collection which is
defined in System.Windows.Forms.Control class which is a base class of
System.Windows.Forms.Form
 
G

Guest

Child classes are down! I always goof up this one!

So even though a control is placed on a child class, it will be added to the
Controls collection higher up in the inheritance chain. The child class
doesn't have it's own Controls collection, it uses the inherited one.

Unlike a control, if I create some other object local to a child class,
which is not stored in a "collection" in a higher base class, this local
object will not be visible to base classes.

Got it! Thanks for your help.

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top