Reputation: 11399
I have a class that should do different things with a form.
Because these "things" are specific to the form, I store the reference to the form like this:
Friend Class clsEdit
Private m_Form As frmMain
And I pass it to the class like this:
Public Sub New(ByRef uForm As frmMain)
m_Form = uForm
End Sub
Now when my class should do these "things", I do it like this:
MyEditClass.DoThings()
Internally it looks like this:
Public Sub DoThis()
m_Form.SetHookPaused(True)
m_Form.StopCommonTimers()
End Sub
Protected Overrides Sub Finalize()
m_Form.DoSomethingThatOnlyThisFormCanDo()
End Sub
I would now like to be able to use clsEdit on a different form as well. This other form also has the functions "DoThings" and "DoSomethingThatOnlyThisFormCanDo".
However, when I change the declaration of m_Form to this
Private m_Form As Form
... I can't do this anymore:
m_Form.DoThings()
... because "DoThings" is not a property / function of "Form".
And when I change it to this:
Private m_Form As frmOther
... I can't do that anymore:
Public Sub New(ByRef uForm As frmMain)
m_Form = uForm
End Sub
Can anybody tell me how I could do this?
Upvotes: 0
Views: 56
Reputation: 81620
Create your interface:
Public Interface IFormStuff
Sub SetHookPaused(value As Boolean)
Sub StopCommonTimers()
End Interface
Replace the form variable with the Interface variable in the class:
Public Class clsEdit
Private m_Form As IFormStuff
Public Sub New(f As IFormStuff)
m_Form = f
End Sub
Public Sub DoThis()
m_Form.SetHookPaused(True)
m_Form.StopCommonTimers()
End Sub
End Class
Implement the Interface in each form:
Public Class Form1
Implements IFormStuff
and each form needs to implement those interface stubs:
Public Sub SetHookPaused(value As Boolean) Implements IFormStuff.SetHookPaused
' do something
End Sub
Public Sub StopCommonTimers() Implements IFormStuff.StopCommonTimers
' do something
End Sub
then you need to create the class at the form level:
Private myEdit As clsEdit = Nothing
Protected Overrides Sub OnLoad(e As EventArgs)
MyBase.OnLoad(e)
myEdit = New clsEdit(Me)
End Sub
That's the gist of it.
Upvotes: 3