Delegates and AddressOf

K

kaczmar2

I have a custom web control that is called from an aspx page. The
custom control dynamically renders out buttons via a public method
(AddButton). One of the attributes of the method is a delegate, so on
the button click event I can invoke a method at the calling page
level. Everything works great if the control is written in C#, but it
needs to be written in VB.NET.

In C#, in the control I can attach an event like so:

NewButton.Click += new EventHandler(Method);

where "Method" is the name of the delegate parameter in the AddButton
function.


In VB.NET, I try to attach an event like so:

AddHandler NewButton.Click, AddressOf Method

This throws the error: 'AddressOf' operand must be the name of a
method (without parentheses).

If I pass AddressOf a method declared at the page level, it works, but
I need to pass it the delegate param that is passed in to the
function. How can I do this in VB.NET? code is below. Thanks for
your help!


Here is the code on the aspx page:

// Button_Click is a delegate, and is defined in the calling page:
MyCustomControl1.AddButton("btnTest", "A Test", Button_Click);


USER CONTROL CODE : C# -- this works...

public delegate void MyDelegate(object sender, EventArgs e);

public void AddButton(string Id, string ButtonText, MyDelegate Method)
{
MyCoolButton NewButton = new Button();

NewButton.ID = Id;
NewButton.Text = ButtonText;

//NewButton.Click += new EventHandler(EventHandler);
NewButton.Click += new EventHandler(Method);
Buttons.Add(NewButton);
}


USER CONTROL CODE : VB.NET - this does not work...

Public Delegate Sub MyDelegate(ByVal sender As Object, ByVal e As
EventArgs)

Public Sub AddButton(ByVal Id As String, ByVal ButtonText As String,
ByVal Method As MyDelegate)

Dim NewButton As New Button

NewButton.ID = Id
NewButton.Text = ButtonText

AddHandler NewButton.Click, AddressOf Method ' *** THIS IS THE LINE
WITH THE ERROR

Buttons.Add(NewButton)

End Sub
 
G

Guest

Get rid of the "AddressOf". It would be needed if 'Method' was an actual
method, but not for a delegate type instance.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
C++ to C# Converter: converts C++ to C#
Instant C++: converts C# or VB to C++/CLI
 
K

kaczmar2

I have tried that as well. If I change the erroneous line to:

AddHandler NewButton.Click, Method

I get thiis compiler-time error:

Value of type 'TestWebCustomControl.MyCustomControl.MyDelegate' cannot
be converted to 'System.EventHandler'.


Any other suggestions? I have been going crazy!
 
K

kaczmar2

Also, I did try changing the signature of my Method to compensate for
the above compile-time error:

Public Sub AddButton(ByVal Id As String, ByVal ButtonText As String,
ByVal Method As System.EventHandler)
....
AddHandler NewButton.Click, Method ' This will no longer throw a
compile error...

But then the event isn't raised when I click the button.
 
K

kaczmar2

Igonre that last post. The event did fire! I forgot to properly
register the envent handler in my page. It now works after I changed
the signature of my method to accept an EventArgs delegate instead of
my custom delegate type.

Thanks for your help.
 

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