Multiple levels of inheritance with UserControl

njsokalski

Well-known member
Joined
Mar 16, 2011
Messages
102
Programming Experience
5-10
I want to create a control from UserControl, and then have another class or control inherit that. Basically, I want the following:

Public Class MyBaseClass : Inherits UserControl

Public Class MyControl : Inherits MyBaseClass

I am having trouble doing this, but I am not sure what I am missing. In the codebehind for MyControl, I inherit MyBaseClass, but then I receive an error in the *.g.i.vb that says the base class cannot be different. What can I do to fix this? Basically, I just want to create controls that use multiple levels of inheritance. Any help would be appreciated. Thanks.
 
I may have phrased my original question incorrectly. I do not really want multiple inheritance, I want to inherit multiple levels up the hierarchy. I have my original class, declared as follows:

Public Class MyBaseClass : Inherits UserControl

Then I have a control, which I want use like a UserControl, that has the following in the codebehind:

Partial Public Class MyUserControl : Inherits MyBaseClass

So as you can see, none of these classes inherit from more than one class, it is just MyBaseClass inherits from UserControl, and MyUserControl inherits from MyBaseClass. When I do this, I receive an error such as the following in MyUserControl.g.i.vb:

Base class 'System.Windows.Controls.UserControl' specified for class 'MyUserControl' cannot be different from the base class 'MyBaseClass' of one of its other partial types.

How can I fix this? Thanks.
 
The answer is still the same. You're trying to set up a chain of inheritance (Class2 inherits Class1, Class3 inherits Class2 which inherits Class1) and VB.Net doesn't allow this (though C++ does). In VB.Net you can't create a User Control from a User Control using inheritance which has itself been created from a User Control using inheritance.
 
Base class 'System.Windows.Controls.UserControl' specified for class 'MyUserControl' cannot be different from the base class 'MyBaseClass' of one of its other partial types.
This means that designer code file has "MyUserControl:UserControl", while in the Partial user code file you attempt to set "MyUserControl:MyBaseClass", so the compiler see you trying to inherit from two different classes at the same time, which can't be done. You have to change the designer generated file to set the correct inherit from. In more advanced VS versions than what I'm using I think you can specify the base class in the new template dialog when adding new items to project.
 
Back
Top