Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inheritance issue

Status
Not open for further replies.

infimo

Technical User
Apr 8, 2004
22
US
I have 3 UserControls (i.e. they inherit System.Windows.Forms.UserControls)

In each of these UserControls, I have a method named "Info()". How can I get this method to be executed without having a direct instantiation of one of the actual controls?

Lets call my 3 controls as
ControlA
ControlB and
ControlC.

They all inherit UserControl (or if need be another class / interface that I have written and which would eventually inherit UserControl).

I am instantiating each Control (A, B and C) and putting it into a structure. This structure consists of two members:
* A Descriptive Name for the Control
* A object of type UserControl (or the other class / interface that is inherited by my 3 controls)

SO when I put the instantiated control into the structure, it gets casted to its parent type.

Ultimately I place this structure into a listbox.

What I want is this:
When the user clicks on any particular item in the listbox, the method "Info()" of the particular control out of the 3 should be executed.

HOw do I do this?

Thanks a lot in advance!! I am quite desperate.

Infimo
 
But Info() is to be defined in the child classes. And I am not able to use an interface, because I have to somehow use the UserControl as the base class.

So if I define an intermediary interface that has info() in it (to be implemented in the child classes), then this interface cannot inherit the Windows.Forms.UserControl.

Secondly, I need to be able to instantiate an object of the parent class in the structure.

How do I do this?
 
A Class can implement any number of interfaces besides inheriting another class. I am sure that you need an interface. How else do you suppose DataBinding works for so many objects. Each class should implement the interface in which you have defined Info().

Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
Thanks JohnYingling for your reply.

I still don't understand.

If I create an interface with a method Info() and implement this method in each of my 3 controls. I can make these controls inherit UserControl directly.

But the problem occurs where I need a generalized instantiation of these controls (in the structure that I described in my above post).

Using this generalized instance I need to make calls to the Windows.Forms.UserControl specific methods & variables alongwith making calls to the method Info().

Thanks!

Mohit
 
Make your three controls derived classes of an abstract class.

public must inherit class ExtendedUserControl
inherits Usercontrol

public must override Info() as string

end class


then have your 3 user control classes inherit the ExtendedUserControl, and give them each the .Info you want. you'll have to touch up the syntax there, that was off the top of my head and I'm pretty sure it's not exactly correct.

-Rick

----------------------
 
Thanks a lot Rick. That did the trick!

But I already used a laborous work around. Next time, I'll keep abstract classes in mind.

Infimo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top