What is this Pattern?

R

rorybecker

I recently some code similar to the following:

-------------------------------------------------------------
Public Delegate Sub IntDelegate(ByVal Int As Integer)
Public Class ParamMenuEventHandlerWrapper
Private mParam As Integer
Private mMethod As IntDelegate
Public Sub New(ByVal Param As Integer, ByVal Method As IntDelegate)
mParam = Param
mMethod = Method
End Sub
Public Sub HandleMenu(ByVal Sender As Object, ByVal e As System.EventArgs)
Call mMethod.Invoke(mParam)
End Sub
End Class
-------------------------------------------------------------

By then using the following code:
-------------------------------------------------------------
Public Class X
Public Sub WireupMenus()
Dim x As New System.Windows.Forms.ContextMenu()
x.MenuItems.Add(New System.Windows.Forms.MenuItem("Item 1", AddressOf
(New ParamMenuEventHandlerWrapper(1, AddressOf IntParamMethod)).HandleMenu))
x.MenuItems.Add(New System.Windows.Forms.MenuItem("Item 2", AddressOf
(New ParamMenuEventHandlerWrapper(2, AddressOf IntParamMethod)).HandleMenu))
x.MenuItems.Add(New System.Windows.Forms.MenuItem("Item 3", AddressOf
(New ParamMenuEventHandlerWrapper(3, AddressOf IntParamMethod)).HandleMenu))
End Sub
Public Sub IntParamMethod(ByVal Param As Integer)
MsgBox(Param)
End Sub
End Class
-------------------------------------------------------------

You can wire up a passed Delegate (which requires a param of Type Integer)
to a Menu_Click or other event which takes no such parameters by pre arranging
the parameter which will be passed.

Is there a name for this type of code? A pattern ?

Thanks
 
F

Family Tree Mike

I don't know that there is a name to this style. I would just declare the
delegate and method in the form class rather than separating it out in a
separate class.
 

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