question regarding use of delegates?

A

astro

I want to build a Filter form that displays a list of values in a listbox
which the user selects from. The values in this listbox are passed to the
filterform. When the user closes this filter form it notifies the calling
form of the filter criteria selected from this listbox. Since any form can
call this filterform the return call address is not known until the filter
form is opened.

Could I setup a callback for this purpose? Is there a better way to do this?
Any advice appreciated. Air code follows............

MainForm:
============
sub openfinder()
dim frm as new FinderForm
dim mydelegate as FinderForm.thisone
mydelegate = addressof SenditHere
frm.show
end sub

sub SenditHere(astr as string)
msgbox ("here it is: " & astr)
end sub


FinderForm:
============
private sFilter as string
public Delegate sub thisOne(byval astr_ as string)
....
....
private sub closeForm (sender, e) handles btnOk.click
call thisOne (sFilter) << DOES NOT COMPILE - "thisOne is a type
and cannot be used as an expression"
end sub
 
M

Marina

I think in this case you need to declare a variable of type thisOne, and set
it to a method.

However, I would suggest that you look into using events here. That way the
form opening the dialog can listen to the dialog's event that says that a
value was selected, and handle it appropriately.
 
A

astro

woot! that worked (setting up event)

thanks

Marina said:
I think in this case you need to declare a variable of type thisOne, and
set it to a method.

However, I would suggest that you look into using events here. That way
the form opening the dialog can listen to the dialog's event that says
that a value was selected, and handle it appropriately.
 
B

branco.medeiros

astro said:
I want to build a Filter form
When the user closes this filter form it notifies the calling
form
Could I setup a callback for this purpose? Is there a better way to do this?
MainForm:
============
sub openfinder()
dim frm as new FinderForm
dim mydelegate as FinderForm.thisone
mydelegate = addressof SenditHere
frm.show
end sub

sub SenditHere(astr as string)
msgbox ("here it is: " & astr)
end sub


FinderForm:
============
private sFilter as string
public Delegate sub thisOne(byval astr_ as string)
...
...
private sub closeForm (sender, e) handles btnOk.click
call thisOne (sFilter) << DOES NOT COMPILE - "thisOne is a type
and cannot be used as an expression"
end sub

The compilation error exists because you are simply declaring a
delegate type, but not a varible of that type.

You must declare a variable of the delegate type in the filter form, so
interested partners may 'connect' to the form.

In FinderForm, you'd have something like this:

<code style="Air code">
Private mTarget AS ThisOne

Public Property OnThisOne As ThisOne
Get
Return mTarget
End Get
Set(Value As ThisOne)
mTarget = Value
End Set
End Property

</code>

The code above declares a *variable* of type ThisOne. The public
property allows a caller to asssign a value to the variable, Then at
the appropriate time (when the FormClose sub kicked in), you'd have:

If mTarget IsNot Nothing Then
Call mTarget(sFilter)
End If

Notice the test to see if your delegate reference (mTarget) IsNot(tm)
Nothing: If you'd just call it without testing first, you'd risk
getting a NullReference exception...

Given the complications involving the directly handling of Delegates, I
suggest, as Marina already did, that you use Events instead:

<code style="Air code">

'Inside the Filter form
Public Event OnFilter(Value As String)

'...
Private Sub CloseForm(Sender As Object, E As EventArgs) _
Handles Button1.Click
RaiseEvent OnFilter(sFilter)
End SUb

'Inside Main Form
Private WithEvents MyFinderForm As FinderForm

Sub OpenFinder()
MyFinderForm = New FinderForm
MyFinderForm.Show
End Sub

Private Sub OnFilter(Value As String) _
Handles MyFinderForm.OnFilter
MsgBox("here it is: " & Value)
End Sub
</code>

Hope this helps.

Regards,

Branco.
 

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