Function pointers, Delegates, and Event Handlers (oh my)

Machaira

Well-known member
Joined
Aug 19, 2002
Messages
325
Location
Abingdon, MD
Ok, hopefully this will make sense. :)

I have a number of forms which call a Public procedure to display a form with a grid on it. The grid is populated with a dataset created in the procedure (via a function called from the procedure). What I want to do is pass a function pointer to a function on the form calling the procedure which will be called when the user double-clicks a row on the grid (confused yet ;)). Normally, an event handler would be set up to handle the double-click on the grid. What I cant quite figure out is passing a pointer to this event handler to the procedure and setting up the event handler in the procedure. Heres the procedure that shows the form:
Code:
    Public Sub ShowDataBrowser(ByVal sSQL As String, ByVal bNavigateToClickedRecord As Boolean, ByRef lClickEventHandle As Long)

        Dim oDataset As New DataSet()

        If GetDataset(sSQL, oDataset) > 0 Then

            Dim oBrowseForm As New DataBrowseForm(oDataset)

            If bNavigateToClickedRecord Then
                Add event handler for grid double-click event on DataBrowseForm


            End If
            oBrowseForm.ShowDialog()

        End If

    End Sub
The last parameter in the declaration would be the function pointer to the event handler. Its declared as a Long as I dont quite know how to declare it. Ive looked at Delegates and dont think they quite fit the bill here. I could be wrong however. :)

Any advice would be appreciated.
 
Here goes:
1) Create a delegate declaration on the, e.g. form, and add a module level variable of the delegate type:
Code:
Public Delegate Sub pointerToDelegate(enter params here)

private toUseLaterPointer as pointerToDelegate
2) Overload the constructor on your form to pass a delegate
Code:
Public Sub New(myPointer as pointerToDelegate)
  toUseLaterPointer = myPointer
End Sub
3) Now you can fire this method anywhere you want, e.g.
Code:
toUseLaterPointer(enter params here)

The main thing is to decide where you will declare your delegate (e.g. on the form).

Hope it helps and I understood your problem :)
 
Use AddHandler.

Code:
AddHandler oBrowseForm.myControl.myEvent, AddressOf myProcedure
 
The problem with the AddHandler is how to pass a pointer to the handling procedure to the procedure that actually has that code. Thats where Im caught up at. Any ideas?
 
Firstly, stop using modules, modules are bad!

Secondly, Im still not entirely clear what it is youre trying to do. I suspect its much easier than doing what youve been trying, though. Can you explain the problem entirely from the top?
 
Why do you think modules are bad?

As for the problem I found a way around it. It means duplicating a couple of lines of code, but thats ok.

What I was trying to do was have a procedure which took as a parameter a function pointer. The procedure displays a form with a datagrid. When a row in the datagrid is selected the function pointed to by the function pointer is called. The problem I was having was how to pass the function pointer to the procedure and hook it to the datagrid.

I hope that explains it better.
 
Yes, that does.

Modules are bad OOP design and should never have been included in VB.NET. There is no need for them. Any miscellaneous functions should be grouped together as static (shared) members of a class.
 
I have one question involving Modules; when you start a new Console
application, it gives you a standard module with a Sub Main() in it;
is it possible to replace this with a class in some way, or is a module
required in that case?
 
Simply changing "Module" to "Class" and adding the "Shared" modifier on the default Sub Main is all you need to do in that case.
 

Similar threads

V
Replies
0
Views
262
Vergassivellaunus
V
K
Replies
0
Views
192
Kashif Sattar
K
A
Replies
0
Views
170
Ann Maybury
A
Back
Top