Can a Form inherit?

vnarod

Regular
Joined
Mar 22, 2002
Messages
84
I would like my form to inherit functions from another class(not a form). Since I have to inherit from Form and VB allows only one Inherits statement, the only way to do that is to create an intermediate form that would inherit from my class, and then inherit from it. Is this correct?
 
No.
Your intermediuate form would then be a form that does not inherit correctly.

I see two possible ways of tackling your problem:

a) Introduce a public property that is of your original classes' type.
Then you can read and write it's properties and call it's methods as you like. In this way you will however have a hard time if your classes properties and methods were meant to affect the form's behaviour.

b) Same as a) but make it a private property.
Then declare an Interface that contains all relevant properties, methods and events of the class.
Let the class implement the interface.
Let the form implement the interface.
Write custom code for properties/methods in the form that implement the interface. Many of them might call the private instance of the class directly, some may perform additional operations.

An outside class will make calls to the interface and thus needs not know whether the object it is calling is actually your form or your original class.

HTH
Heiko
 
In you example b I will still need to have a third class (interface). Wouldn't this be simpler then?

Class A
Inherits Forms.Form

Class B ' (form)
Inherits A
 
Not sure what you mean,
but of course you could also let your original class inherit from form (in your example class "A") . Then implement your additions in this class A and let the form you want to display inherit from A.

However this would on the other hand mean, that every other object that makes a call to class A (or you, with the intellisense function) would also see the typical properties and events of a class, though.
 
If I understand correctly, you would like to use the access the functionality of another class from the currently opened form. The method I have found useful to do this is:

Visual Basic:
Public Class YourForm
Inherits Systems.Windows.Forms.Form

Private cls as Class  'Class you need to reference


#Region "Windows Form Designer genereated code"

	
'Default constructor created by designer

Public Sub New()	
	
	MyBase.New

	'This process is called by Windows Form Designer
	InitializeComponent()

	'Add any initialization after this point
	
	End Sub

	
'Create your own constructor to accept reference to 
'to the instantiated class passed in.  You can now
'reference the class's functionality through
'me.cls.function()

Public Sub New(ByRef clsIn as Class)	
						
	MyBase.New			
	InitializeComponent()		
						
	Me.cls = clsIn
	
End Sub

#End Region



'This is how you would open the form from another place in you app
	
	Dim cls as New Class()
	Dim frm as New YourForm(cls)
	frm.Show()
 
Back
Top