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

Machaira

Junior Contributor
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 can't quite figure out is passing a pointer to this event handler to the procedure and setting up the event handler in the procedure. Here's the procedure that shows the form:
Visual Basic:
    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. It's declared as a Long as I don't quite know how to declare it. I've looked at Delegates and don't 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:
Visual Basic:
Public Delegate Sub pointerToDelegate('enter params here)

private toUseLaterPointer as pointerToDelegate
2) Overload the constructor on your form to pass a delegate
Visual Basic:
Public Sub New(myPointer as pointerToDelegate)
  toUseLaterPointer = myPointer
End Sub
3) Now you can fire this method anywhere you want, e.g.
Visual Basic:
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.

Visual Basic:
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. That's where I'm caught up at. Any ideas?
 
Firstly, stop using modules, modules are bad!

Secondly, I'm still not entirely clear what it is you're trying to do. I suspect it's much easier than doing what you've 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 that's 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.
 
Back
Top