Jump to content
Xtreme .Net Talk

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


Recommended Posts

Posted

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:

   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's what I'm up to.
Posted

Here goes:

1) Create a delegate declaration on the, e.g. form, and add a module level variable of the delegate type:

Public Delegate Sub pointerToDelegate('enter params here)

private toUseLaterPointer as pointerToDelegate

2) Overload the constructor on your form to pass a delegate

Public Sub New(myPointer as pointerToDelegate)
 toUseLaterPointer = myPointer
End Sub

3) Now you can fire this method anywhere you want, e.g.

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 :)

Howzit??
Posted
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?
Here's what I'm up to.
  • *Gurus*
Posted

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?

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Posted

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.

Here's what I'm up to.
  • *Experts*
Posted

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?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...