tough inheritence question

  • Thread starter Thread starter Andrew Sharpe
  • Start date Start date
A

Andrew Sharpe

-Form A inherits from form B.
-I am iterating over the controls on an instance of form A.
-Is there any way for me to tell if a given control came
from form A or form B?

I suspect not, but I thought I'd ask.
 
Andrew Sharpe said:
-Form A inherits from form B.
-I am iterating over the controls on an instance of form A.

What do you mean by "the controls on an instance", exactly? Do you mean
you're iteratings through the contents of the ControlCollection
returned by the Controls property?
-Is there any way for me to tell if a given control came
from form A or form B?

I doubt it - if you think about it, it's possible that it didn't really
come from either, and that it was added by another class completely.
 
What do you mean by "the controls on an instance",
exactly? Do you mean
you're iteratings through the contents of the
ControlCollection
returned by the Controls property?

Yes, I'm iterating over the controls property.
I doubt it - if you think about it, it's possible that it
didn't really
come from either, and that it was added by another class
completely.

Yeah, I was just fishing. Thanks for responding.

Andrew
 
Hi,

I assume by "came from" you mean "parented by"? If so then you have a couple
of options (in C#)

A) use the "is" operator as follows...

if ( controlInQuestion.Parent is FormAtype)
{
// control in question's Parent is a FormA type Form
}
else
{
// control in question's Parent is NOT a FormA type Form
}

or....

B) you could try a cast of the parent property of the child control as
follows:-

try
{
FormAtype form = (FormAtype)controlInQuestion.Parent;
// cast succeeded therefore control is contained on a FormA type form
.... do whatever
}
catch
{
// control was NOT on a FormA type form
.... do whatever
}

any use?
Chris
 
Very nice try Chris. Unfortunately the parent property
always returns FormB. That is to say, if FormB inherits
from FormA, then the parent property of all members of
FormB.Controls will be of type FormB...even if the control
actually belongs to FormA.

If you could verify that it would be appreciated, as well
as any further suggestions. Thanks for the thought.

Andrew
 
Well. I guess I don't understand what you mean by ".... actually belongs to
FormA"

Do you mean "used to be a child of Form A at some random point in the past"
or "was originally created as a child of Form A" or "immediate previous
parent was Form A"?

Strictly speaking a child control can only have one parent (ie "belong to")
at a time.....
 
Hi,
I believe you miss something here. There is no FormA and FormB. There is
ClassA and ClassB. The type of the instance of the form in this case is
either ClassA and ClassB since ClassB is subclass of ClassA. What I'm traing
to say is that controls belong to the instance not to the class.

B\rgds
100
 
What I'm traing to say is that controls belong to the
instance not to the class.

Yeah I know, I just thought maybe there was some crazy way
to tell which class added each control to the instance.
Again, I was just fishing. Thanks to Chris, John, and 100
for replying.

Andrew
 
Back
Top