Passing AddressOf a function to constructor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David A. Osborn

    Passing AddressOf a function to constructor

    I need to pass the address of a function to the contructor of a class when I
    create an instance of it so that in the new instance I can dynamically
    hookup a handler by doing the following:

    AddHandler ButtonNew.Click , AddressOf My_Passed_Funct ion

    I thought I could use a delegate but that doesn't seem to be working. Am I
    on the right track?



  • Jay B. Harlow [MVP - Outlook]

    #2
    Re: Passing AddressOf a function to constructor

    David,
    You need to define your parameter as the type of handler you are going to
    use, when you call your function/constructor you use addressof, when you
    call AddHandler you can use the parameter (as its already has the addressof
    something.

    Something like:

    Public Class Widget

    Public Sub New(ByVal theHandler As EventHandler)
    Dim ButtonNew As New Button
    AddHandler ButtonNew.Click , theHandler
    End Sub

    End Class

    Public Sub Widget_Click(By Val sender As Object, ByVal e As EventArgs)

    End Sub

    Dim aWidget As New Widget(AddressO f Widget_Click)

    You can find out the type of handler (Delegate) needed by looking up the
    event in the online help.

    Hope this helps
    Jay

    "David A. Osborn" <dosborn278@hot mail.com> wrote in message
    news:lLkFd.3072 $IV5.1712@attbi _s54...[color=blue]
    >I need to pass the address of a function to the contructor of a class when
    >I create an instance of it so that in the new instance I can dynamically
    >hookup a handler by doing the following:
    >
    > AddHandler ButtonNew.Click , AddressOf My_Passed_Funct ion
    >
    > I thought I could use a delegate but that doesn't seem to be working. Am I
    > on the right track?
    >
    >
    >[/color]


    Comment

    • Cor Ligthert

      #3
      Re: Passing AddressOf a function to constructor

      David,

      Why not passing the control itself so. Something as

      \\\
      Private Sub Form1_Load(ByVa l sender As Object, _
      ByVal e As System.EventArg s) Handles MyBase.Load
      Button1.Text = "0"
      Dim mb As New myButtonClass(M e.Button1)
      End Sub
      End Class
      Friend Class myButtonClass
      Friend Sub New(ByVal this As Control)
      AddHandler this.Click, AddressOf ButtonClick
      End Sub
      Private Sub ButtonClick(ByV al sender As System.Object, _
      ByVal e As System.EventArg s)
      Dim mybutton As Button = DirectCast(send er, Button)
      mybutton.Text = (CInt(mybutton. Text) + 1).ToString
      End Sub
      End Clas
      ///
      I hope this gives some ideas

      Cor


      Comment

      Working...