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