Last post Jul 30, 2008 10:23 AM by DanielWilson
Jul 28, 2008 01:08 PM|Jagarm|LINK
I trying to inherit from two classes as follows
Public Class Class1
Public Class Class2 Inherits System.Web.UI.UserControlEnd Class
Public Class Class3 Inherits Class1End Class
Is there anyway to inherit class3 from class1 and 2, is there any linking I can do?
Any help is appreciated
Jul 28, 2008 01:10 PM|DarrellNorton|LINK
Multiple inheritance is not possible in .NET. You can create an interface instead of a class and inherit as many interfaces as you want.
Looking at your example, though, why are you trying to inherit from Page AND UserControl? A user control goes on a page. It makes no sense.
Jul 28, 2008 01:17 PM|DanielWilson|LINK
Though the OP's example looks contrived, I've often wished for multiple inheritance. If I could inherit from both some UI control and a particular data or business object, I could have quite a potent custom control.
Jul 28, 2008 01:23 PM|Jagarm|LINK
When creating an asp.net I inherit the page from Class1 where class1 is inherited from UI.page, and that way it opens and closes the connection automatically, I don't have to worry about whether the connection has been closed or open, and I have many properties
and functions inside Class1. Once the asp.net page is inherited from Class1 and Class1 has a property called GetConnection, I can use the GetConn from any pages that got inherited from Class1 without making a new connection.
So I have created several web user controls and I want to use the same methods and properties that are inside Class1 for the user control.
I hope you know what I mean, if you think there is a better way to do this please let me know.
Jul 28, 2008 02:35 PM|DanielWilson|LINK
If I could use multiple inheritance, I would set up my object tree a little differently than you're saying ... but I would use multiple inheritance in this situation.
Unfortunately ... in .Net we can't.
In that case, I would have a UIClass1 (well, better named, but anyway ...) that would inherit from the control and have the same functionality you've added to the page pasted in.
You end up with the same code twice ... but not dozens of times.
Jul 29, 2008 09:19 AM|DkUltra|LINK
Has writing the mulit inhertance class in c# and then inheriting that class in vb been thought of ?
Jul 29, 2008 09:34 PM|mbanavige|LINK
c# is also limited to single inheritance.
Jul 30, 2008 07:53 AM|DanielWilson|LINK
Correct. .Net does not support multiple inheritance, so even C++ .Net has this limitation.
Jul 30, 2008 10:10 AM|Jagarm|LINK
Thanks alot everyone for you replies, I solved my problem by making a duplicate file of the Class1 in class2.
I hope soon this will be a feature added to .net because this is important.
Jul 30, 2008 10:23 AM|DanielWilson|LINK
It would be nice ... it's not in .Net 3.5 / VS 2008. I've read nothing yet regarding what will be in the next version.
it's a complex concept to implement ... but it is CERTAINLY powerful.