Answered by:
Passing a class method as a parameter of another class's method

Question
-
Hi All,
I have 2 classes named A and B and I would like to pass in one of the method (e.g.public sub aa) of Class A, as a parameter in a method found in Class B (e.g. bb(aa)).
Is that possible to be done in VB.net and If so, could anyone provide me an example of it as a references.
Thanks in advance and have a great day!
Regards,
Hemingway11
Friday, August 17, 2012 12:08 AM
Answers
-
Hi Hemingway,
Class A Public Sub aa() End Sub End Class
Class B Public Sub bb(ByVal action As Action) action() 'calls the delegate pass as a param End Sub End Class
Call:
Dim a As New A Dim b As New B b.bb(AddressOf a.aa)
May I ask why you need that?
Armin
- Proposed as answer by Mike FengModerator Friday, August 17, 2012 10:18 AM
- Marked as answer by HeMingWay11 Tuesday, August 21, 2012 3:42 AM
Friday, August 17, 2012 1:17 AM
All replies
-
Hi Hemingway,
Class A Public Sub aa() End Sub End Class
Class B Public Sub bb(ByVal action As Action) action() 'calls the delegate pass as a param End Sub End Class
Call:
Dim a As New A Dim b As New B b.bb(AddressOf a.aa)
May I ask why you need that?
Armin
- Proposed as answer by Mike FengModerator Friday, August 17, 2012 10:18 AM
- Marked as answer by HeMingWay11 Tuesday, August 21, 2012 3:42 AM
Friday, August 17, 2012 1:17 AM -
Hi Armin,
Thanks for providing me the codes and it works!
However, I have trouble applying this concept into my project.
Basically, I am trying to create a button with "Dynamic" event handling capabilities. In details:
I have 2 classes as shown:
"Hello_World" Class : Class that is used to create a "Hello World" button and "attach" the event of showing "Hello World" prompt.
"UserButton" Class : Class that declares, creates and instantiates the button based on the parameter given (in this case: from "Hello_World" Class)
May I know if this concept of mine is feasible. If so, can you quote me a reference as I am facing difficulties passing my event method in it.
Class A Public Sub aa()'--> Event sub End Sub End Class
Class B Protected Sub AttachEvent(btn As Button) AddHandler btn.Command, AddressOf bb '--> How can i get the Sub bb to be used here since it now contain parameter of action type for Action? End Sub Public Sub bb(ByVal action As Action)
action() 'calls the delegate pass as a param
End Sub
End ClassCall: Dim a As New A Dim b As New B b.bb(AddressOf a.aa) '--> pass in event sub to B
HeiMingWay11
Tuesday, August 21, 2012 7:44 AM -
Hi Hemingway,
I have some problems in understanding your plan. In class B, you execute AddHandler, Sub bb being the handler. I expect 'action' to be the handler. No class creates the button. Which one is the Hello_World class and which one the UseButton? A or B? I'm not sure how the description matches the code example.
The type 'Action' is a delegate. It has no parameters. If you want a delegate matching an event handler, you can use one of the predefined event handler delegates. For example, the delegate type Sytem.EventHandler fits the Click event handler.
At the moment, I have no clue, what's your intention. Could you please describe it once more? Thx.
Armin
Tuesday, August 21, 2012 1:16 PM -
Hi Armin,
Thanks again for your kind assistances and sorry for my bad for not descrbing the qns explicitly.
Actually I can creating something whereby a class named "Hello_World" is being called, It would then build the control specified in it and add in the event method that is written in it to the created control.
Below are the codes which I had created for your referral. Hope it is much clearer this time :)
Class Hello_World Public Sub Hello_World(ByVal pnl As Panel) Dim ctl as New UserControls ctl.SetButton(Pnl, "btnHelloWorld", "Hello World", BtnClick) ' How can I pass in the BtnClick event mehtod? End Sub Protected Sub BtnClick(ByVal Sender as Object, ByVal e As CommandEventArgs) MsgBox("HELLO_ WORLD") '--> Event to be attached to the control created (which is the button) End Sub End Class
Class UserControl Public Sub SetButton(ByRef pnl As Panel, ByVal id As String, ByVal text As String, BtnEvent) Dim btn As New Button btn.ID = id btn.Text = text pnl.Controls.Add(btn) 'Add Event Handling Method to Click Event AddHandler btn.Command, AddressOf btnEvent '--> How can i get the Sub btnEvent to be used here? End Sub End Class
HeMingWay11
- Edited by HeMingWay11 Wednesday, August 22, 2012 7:02 AM
Wednesday, August 22, 2012 6:55 AM -
Try this
Option Strict On
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ButtonAdder.AddbuttonToForm(Me, New Button, AddressOf Button1_Click)
Dim Eh As New EventHandler(AddressOf Button1_Click)
ButtonAdder.AddButtonToForm2(Me, New Button, Eh)
End Sub
Public Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
MsgBox("This Works Perfectly.")
End Sub
End Class
Class ButtonAdder
Public Shared Sub AddbuttonToForm(ByVal Form As Form, ByVal Button As Button, ByVal ClickEventHandler As Action(Of Object, EventArgs))
Form.Controls.Add(Button)
AddHandler Button.Click, EventHandlerFromMI(ClickEventHandler.Method)
End Sub
Public Shared Sub AddButtonToForm2(ByVal Form As Form, ByVal Button As Button, ByVal ClickEventHandler As EventHandler)
Form.Controls.Add(Button)
AddHandler Button.Click, ClickEventHandler
End Sub
Public Shared Function EventHandlerFromMI(ByVal MethodInfo As System.Reflection.MethodInfo) As EventHandler
Dim EventHandlerType As Type = GetType(eventhandler)
Dim ConvertThisDelegate As System.Delegate = System.Delegate.CreateDelegate(EventHandlerType, Nothing, MethodInfo, False)
Return DirectCast(ConvertThisDelegate, EventHandler)
End Function
End Class
If you want something you've never had, you need to do something you've never done.
- Edited by Paul Ishak Wednesday, August 22, 2012 10:49 AM
Wednesday, August 22, 2012 10:19 AM -
Hi Armin,
Thanks again for your kind assistances and sorry for my bad for not descrbing the qns explicitly.
Actually I can creating something whereby a class named "Hello_World" is being called, It would then build the control specified in it and add in the event method that is written in it to the created control.
Below are the codes which I had created for your referral. Hope it is much clearer this time :)
Class Hello_World Public Sub Hello_World(ByVal pnl As Panel) Dim ctl as New UserControls ctl.SetButton(Pnl, "btnHelloWorld", "Hello World", BtnClick) ' How can I pass in the BtnClick event mehtod? End Sub Protected Sub BtnClick(ByVal Sender as Object, ByVal e As CommandEventArgs) MsgBox("HELLO_ WORLD") '--> Event to be attached to the control created (which is the button) End Sub End Class
Class UserControl Public Sub SetButton(ByRef pnl As Panel, ByVal id As String, ByVal text As String, BtnEvent) Dim btn As New Button btn.ID = id btn.Text = text pnl.Controls.Add(btn) 'Add Event Handling Method to Click Event AddHandler btn.Command, AddressOf btnEvent '--> How can i get the Sub btnEvent to be used here? End Sub End Class
HeMingWay11
If you declare "ByVal BtnEvent As EventHandler" you can write
AddHandler btn.Click, BtnEvent
IN class hello_world:
ctl.SetButton(Pnl, "btnHelloWorld", "Hello World", Addressof BtnClick)
and
Protected Sub BtnClick(ByVal Sender As Object, ByVal e As EventArgs)
EDIT: The bolded parts are still not displayed correctly.
Armin
- Edited by Armin Zingler Tuesday, August 28, 2012 1:39 PM
Tuesday, August 28, 2012 1:39 PM