Inter-related form

G

Guest

Hello:

I have two forms, FormA and FormB. FormA opens FormB. FormB needs to execute
a public method on FormA.


I am able to do it. But I want to generalize the design so that FormB can be
used with any form that required the functionality provided by FormB. Of
course, all these forms will have the public method that will be executed by
FormB.

Currently, I have it coded this way:

Private MyParentForm as FormA
Public Sub New(psForm as FormA)
...
...
MyParentForm = psForm
End Sub

Sub doSomething()
MyParentForm.ExecuteFunction
End Sub

So, in other words, how can I generalize it so the parameter can be any of
the form classes that will have a public method ExecuteFunction?

Thanks.

Venkat
 
C

Cor Ligthert

Hi Venkat,

Your method (create a formA with a method on it to use everywhere), is
against everything about OOP.

Create a class, which you use in FormB, FormC however as well in FormA.

Then you don't have to create something generic, it is the standard way to
go.

I hope this helps?

Cor
 
C

Cor Ligthert

VenKat,

To remember me where you where talking about in your previous question where
I asked you not to multipost in this newsgroup almost the same messages.

Passing Currencymanager and DataTable to a class

Cor
 
L

Larry Serflaten

vvenk said:
I have two forms, FormA and FormB. FormA opens FormB. FormB needs to execute
a public method on FormA.
Sub doSomething()
MyParentForm.ExecuteFunction
End Sub

So, in other words, how can I generalize it so the parameter can be any of
the form classes that will have a public method ExecuteFunction?


There are two general purpose ways to handle that, using events, or using interfaces.

Using events means that any form that wants to, can declare FormB WithEvents
such that FormB will raise the event for the parent form to act on. The parent
would not be required to listen for events, even while formB raises them as scheduled.

If the decision to act on FormB's event is supposed to be in the parent form, that
would be the way to go. If FormB requires that there be a routine to call in the
parent form, then use an interface.

Using interfaces means you declare an interface with the required method, and force
all callers to implement that interface to show FormB:


Private MyParentForm as IParentInterface
Public Sub New(psForm as IParentInterface)
...
...
MyParentForm = psForm
End Sub

Sub doSomething()
MyParentForm.ExecuteFunction
End Sub


That would assume you've declared an interface somewhere
(in a module perhaps):

Public Interface IParentInterface
Sub ExecuteFunction()
End Interface

And any form that uses FormB must implement that interface:

Public Class FormA
Inherits System.Windows.Forms.Form
Implements IParentInterface

'... <rest of form code>

Public Sub ExecuteFunction() Implements IParentInterface.ExecuteFunction
' FormB callback function goes here
End Sub

End Class


The calling syntax would be similar to what you are probably already
using:

Dim dialog As FormB = New FormB(Me)
dialog.Show()


HTH
LFS
 
G

Guest

Larry:

Awesome! Thank you!

Venkat

Larry Serflaten said:
There are two general purpose ways to handle that, using events, or using interfaces.

Using events means that any form that wants to, can declare FormB WithEvents
such that FormB will raise the event for the parent form to act on. The parent
would not be required to listen for events, even while formB raises them as scheduled.

If the decision to act on FormB's event is supposed to be in the parent form, that
would be the way to go. If FormB requires that there be a routine to call in the
parent form, then use an interface.

Using interfaces means you declare an interface with the required method, and force
all callers to implement that interface to show FormB:


Private MyParentForm as IParentInterface
Public Sub New(psForm as IParentInterface)
...
...
MyParentForm = psForm
End Sub

Sub doSomething()
MyParentForm.ExecuteFunction
End Sub


That would assume you've declared an interface somewhere
(in a module perhaps):

Public Interface IParentInterface
Sub ExecuteFunction()
End Interface

And any form that uses FormB must implement that interface:

Public Class FormA
Inherits System.Windows.Forms.Form
Implements IParentInterface

'... <rest of form code>

Public Sub ExecuteFunction() Implements IParentInterface.ExecuteFunction
' FormB callback function goes here
End Sub

End Class


The calling syntax would be similar to what you are probably already
using:

Dim dialog As FormB = New FormB(Me)
dialog.Show()


HTH
LFS
 
G

Guest

Larry:

It almost worked but I get an error at compilation:

On FormB's Public New method:

Public Sub New(ByVal psForm As IParentFormDataGridSearchInterface)

it says "'psForm' cannot expose a Friend type outside of the Public class
'Form2'."

How can I resolve it?

Thanks again.
 
L

Larry Serflaten

vvenk said:
It almost worked but I get an error at compilation:

On FormB's Public New method:

Public Sub New(ByVal psForm As IParentFormDataGridSearchInterface)

it says "'psForm' cannot expose a Friend type outside of the Public class
'Form2'."

How can I resolve it?

For use in your own application, simply declare the New sub as Friend, instead of Public.

Friend Sub New(ByVal psForm As IParentFormDataGridSearchInterface)

LFS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top