Delegates and AddressOf

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kaczmar2@hotmail.com

    Delegates and AddressOf

    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(Me thod);

    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:
    MyCustomControl 1.AddButton("bt nTest", "A Test", Button_Click);


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

    public delegate void MyDelegate(obje ct sender, EventArgs e);

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

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

    //NewButton.Click += new EventHandler(Ev entHandler);
    NewButton.Click += new EventHandler(Me thod);
    Buttons.Add(New Button);
    }


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

    Public Delegate Sub MyDelegate(ByVa l 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(New Button)

    End Sub

  • =?Utf-8?B?RGF2aWQgQW50b24=?=

    #2
    RE: Delegates and AddressOf

    Get rid of the "AddressOf" . It would be needed if 'Method' was an actual
    method, but not for a delegate type instance.
    --
    David Anton
    Source code converters: Convert between C#, C++, Java, and VB with the most accurate and reliable source code converters

    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


    "kaczmar2@hotma il.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(Me thod);
    >
    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:
    MyCustomControl 1.AddButton("bt nTest", "A Test", Button_Click);
    >
    >
    USER CONTROL CODE : C# -- this works...
    >
    public delegate void MyDelegate(obje ct sender, EventArgs e);
    >
    public void AddButton(strin g Id, string ButtonText, MyDelegate Method)
    {
    MyCoolButton NewButton = new Button();
    >
    NewButton.ID = Id;
    NewButton.Text = ButtonText;
    >
    //NewButton.Click += new EventHandler(Ev entHandler);
    NewButton.Click += new EventHandler(Me thod);
    Buttons.Add(New Button);
    }
    >
    >
    USER CONTROL CODE : VB.NET - this does not work...
    >
    Public Delegate Sub MyDelegate(ByVa l 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(New Button)
    >
    End Sub
    >
    >

    Comment

    • kaczmar2@hotmail.com

      #3
      Re: Delegates and AddressOf

      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 'TestWebCustomC ontrol.MyCustom Control.MyDeleg ate' cannot
      be converted to 'System.EventHa ndler'.


      Any other suggestions? I have been going crazy!

      On May 4, 8:04 pm, David Anton <DavidAn...@dis cussions.micros oft.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.tangib lesoftwaresolut ions.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...@hotma il.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(Me thod);
      >
      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:
      MyCustomControl 1.AddButton("bt nTest", "A Test", Button_Click);
      >
      USER CONTROL CODE : C# -- this works...
      >
      public delegate void MyDelegate(obje ct sender, EventArgs e);
      >
      public void AddButton(strin g Id, string ButtonText, MyDelegate Method)
      {
      MyCoolButton NewButton = new Button();
      >
      NewButton.ID = Id;
      NewButton.Text = ButtonText;
      >
      //NewButton.Click += new EventHandler(Ev entHandler);
      NewButton.Click += new EventHandler(Me thod);
      Buttons.Add(New Button);
      }
      >
      USER CONTROL CODE : VB.NET - this does not work...
      >
      Public Delegate Sub MyDelegate(ByVa l 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(New Button)
      >
      End Sub- Hide quoted text -
      >
      - Show quoted text -

      Comment

      • kaczmar2@hotmail.com

        #4
        Re: Delegates and AddressOf

        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.EventHan dler)
        ....
        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...@dis cussions.micros oft.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.tangib lesoftwaresolut ions.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...@hotma il.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(Me thod);
        >
        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:
        MyCustomControl 1.AddButton("bt nTest", "A Test", Button_Click);
        >
        USER CONTROL CODE : C# -- this works...
        >
        public delegate void MyDelegate(obje ct sender, EventArgs e);
        >
        public void AddButton(strin g Id, string ButtonText, MyDelegate Method)
        {
        MyCoolButton NewButton = new Button();
        >
        NewButton.ID = Id;
        NewButton.Text = ButtonText;
        >
        //NewButton.Click += new EventHandler(Ev entHandler);
        NewButton.Click += new EventHandler(Me thod);
        Buttons.Add(New Button);
        }
        >
        USER CONTROL CODE : VB.NET - this does not work...
        >
        Public Delegate Sub MyDelegate(ByVa l 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(New Button)
        >
        End Sub- Hide quoted text -
        >
        - Show quoted text -

        Comment

        • kaczmar2@hotmail.com

          #5
          Re: Delegates and AddressOf

          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...@hotmai l.com wrote:
          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.EventHan dler)
          ...
          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...@dis cussions.micros oft.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.tangib lesoftwaresolut ions.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...@hotma il.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(Me thod);
          >
          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:
          MyCustomControl 1.AddButton("bt nTest", "A Test", Button_Click);
          >
          USER CONTROL CODE : C# -- this works...
          >
          public delegate void MyDelegate(obje ct sender, EventArgs e);
          >
          public void AddButton(strin g Id, string ButtonText, MyDelegate Method)
          {
          MyCoolButton NewButton = new Button();
          >
          NewButton.ID = Id;
          NewButton.Text = ButtonText;
          >
          //NewButton.Click += new EventHandler(Ev entHandler);
          NewButton.Click += new EventHandler(Me thod);
          Buttons.Add(New Button);
          }
          >
          USER CONTROL CODE : VB.NET - this does not work...
          >
          Public Delegate Sub MyDelegate(ByVa l 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(New Button)
          >
          End Sub- Hide quoted text -
          >
          - Show quoted text -- Hide quoted text -
          >
          - Show quoted text -

          Comment

          Working...