Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
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?
Posted

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

.nerd
Posted

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

Posted

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.

.nerd
Posted

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:

 

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()

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...