VB & delegates
-
I am new to using delegates and am getting "Error Binding To Target Method" error on the CreateDelegate line. What am I doing wrong? Thanks in Advance. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim sTest As String = "MyTest" Dim test As ToolStripMenuItem Dim currentType As Type = Me.GetType Dim method As MethodInfo Dim delTest As [Delegate] method = currentType.GetMethod(sTest) If Not method Is Nothing Then MsgBox(method.Name) delTest = CreateDelegate(GetType(EventHandler), method) test = New ToolStripMenuItem("Testing", Nothing, delTest) Else test = New ToolStripMenuItem("Testing") End If MyBase.mnuMain.Items.Add(test) End Sub Public Sub MyTest(ByVal sender As Object, ByVal e As EventArgs) MsgBox("testing") End Sub
-
I am new to using delegates and am getting "Error Binding To Target Method" error on the CreateDelegate line. What am I doing wrong? Thanks in Advance. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim sTest As String = "MyTest" Dim test As ToolStripMenuItem Dim currentType As Type = Me.GetType Dim method As MethodInfo Dim delTest As [Delegate] method = currentType.GetMethod(sTest) If Not method Is Nothing Then MsgBox(method.Name) delTest = CreateDelegate(GetType(EventHandler), method) test = New ToolStripMenuItem("Testing", Nothing, delTest) Else test = New ToolStripMenuItem("Testing") End If MyBase.mnuMain.Items.Add(test) End Sub Public Sub MyTest(ByVal sender As Object, ByVal e As EventArgs) MsgBox("testing") End Sub
This isn't working because, for some reason,
MyTest
's signature doesn't match the signature ofEventHandler
. You'll run into another problem after that one's fixed. The method you're binding the delegate to must be a Shared method (Static in C#).Imports System.Reflection
Imports System.Delegate
.
.
.
Public Shared Sub MyTest(ByVal sender As Object, ByVal e As EventArgs)
MsgBox("Testing...")
End SubRageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
This isn't working because, for some reason,
MyTest
's signature doesn't match the signature ofEventHandler
. You'll run into another problem after that one's fixed. The method you're binding the delegate to must be a Shared method (Static in C#).Imports System.Reflection
Imports System.Delegate
.
.
.
Public Shared Sub MyTest(ByVal sender As Object, ByVal e As EventArgs)
MsgBox("Testing...")
End SubRageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome