Home All Groups Group Topic Archive Search About

Delegates and AddressOf

Author
4 May 2007 9:45 PM
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

Author
5 May 2007 12:04 AM
David Anton
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


Show quoteHide quote
"kaczm***@hotmail.com" wrote:

> 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
>
>
Author
5 May 2007 1:58 AM
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!

On May 4, 8:04 pm, David Anton <DavidAn***@discussions.microsoft.com>
wrote:
Show quoteHide quote
> Get rid of the "AddressOf".  It would be needed if 'Method' was an actual
> method, but not for a delegate type instance.
> --
> David Antonwww.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
>
>
>
> "kaczm***@hotmail.com" wrote:
> > 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- Hide quoted text -
>
> - Show quoted text -
Author
5 May 2007 2:01 AM
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.


On May 4, 8:04 pm, David Anton <DavidAn***@discussions.microsoft.com>
wrote:
Show quoteHide quote
> Get rid of the "AddressOf".  It would be needed if 'Method' was an actual
> method, but not for a delegate type instance.
> --
> David Antonwww.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
>
>
>
> "kaczm***@hotmail.com" wrote:
> > 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- Hide quoted text -
>
> - Show quoted text -
Author
5 May 2007 2:12 AM
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.


On May 4, 10:01 pm, kaczm***@hotmail.com wrote:
Show quoteHide quote
> 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.
>
> On May 4, 8:04 pm, David Anton <DavidAn***@discussions.microsoft.com>
> wrote:
>
>
>
> > Get rid of the "AddressOf".  It would be needed if 'Method' was an actual
> > method, but not for a delegate type instance.
> > --
> > David Antonwww.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
>
> > "kaczm***@hotmail.com" wrote:
> > > 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- Hide quoted text -
>
> > - Show quoted text -- Hide quoted text -
>
> - Show quoted text -