locked
AddressOf as parameter RRS feed

  • Question

  • I am building an API where I want a particular method to accept a function pointer. For example I want the consumer of the method to call it like this: MyMethod(AddressOf MyFunction)

    I know how to make the method work like this: MyMethod(new MyDelegate(addressof MyFunction)) but I don't want this. I see Microsoft has methods that can pass AddressOf directly. How is this achieved?

    Wednesday, February 29, 2012 8:53 AM

Answers

  • Perry,

    your method has to take a defined delegate as parameter and then you can simply use the AddressOf Somefunction to call it like:

    Public Class Form1
        
    Private Delegate Sub theDelegate(ByVal somevalue As String)
        
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            test(
    AddressOf mytest)
        
    End Sub
        Private Sub test(ByVal mydel As theDelegate)

        
    End Sub
        Private Sub mytest(ByVal myvalue As String)

        
    End Sub
    End
     Class


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/


    • Edited by Heslacher Wednesday, February 29, 2012 9:03 AM
    • Proposed as answer by Armin Zingler Wednesday, February 29, 2012 1:00 PM
    • Marked as answer by Youen Zen Thursday, March 8, 2012 6:05 AM
    Wednesday, February 29, 2012 9:02 AM

All replies

  • Perry,

    your method has to take a defined delegate as parameter and then you can simply use the AddressOf Somefunction to call it like:

    Public Class Form1
        
    Private Delegate Sub theDelegate(ByVal somevalue As String)
        
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            test(
    AddressOf mytest)
        
    End Sub
        Private Sub test(ByVal mydel As theDelegate)

        
    End Sub
        Private Sub mytest(ByVal myvalue As String)

        
    End Sub
    End
     Class


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/


    • Edited by Heslacher Wednesday, February 29, 2012 9:03 AM
    • Proposed as answer by Armin Zingler Wednesday, February 29, 2012 1:00 PM
    • Marked as answer by Youen Zen Thursday, March 8, 2012 6:05 AM
    Wednesday, February 29, 2012 9:02 AM
  • The problem is that I don't know at design time what the delegate signature will be. I want the user to be able to pass in the address of any method without them having to create a delegate and passing that. I am building a scripting engine and I want to abstract the whole delegate stuff from users.
    Friday, March 2, 2012 10:16 AM
  • If you dont know the signatures at design time, create your delegate dynamicaly

    Here an example

    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim Del As [Delegate]
            Del = [Delegate].CreateDelegate(GetType(Action(Of String, Integer)), Me, "MyMethod1")
            ABC.DEF(Del, "AString", 22)
            Del = [Delegate].CreateDelegate(GetType(Action(Of String)), Me, "MyMethod2")
            ABC.DEF(Del, "AString")
        End Sub
    
        Private Sub MyMethod1(ByVal A As String, ByVal B As Integer)
            Stop
        End Sub
    
        Private Sub MyMethod2(ByVal A As String)
            Stop
        End Sub
    End Class
    
    Class ABC
    
        Public Shared Sub DEF(ByVal D As [Delegate], ByVal ParamArray Arg() As Object)
            D.DynamicInvoke(Arg)
        End Sub
    
    End Class

    Friday, March 2, 2012 10:57 AM
  • Luke,

    I did not test it, but for me it looks to a crazy penny lane method. 

    In my mind that was the name for these machines which could be used with those big old English Pennies when one Pound was worth the equivalent of the current 5 Euro.

    I'm not sure if they use those also in Canada, probably for sure in English Canada.

     


    Success
    Cor


    Friday, March 2, 2012 1:35 PM
  • Cool machine !!! I need a few of those.

    Can we get the money?

    Friday, March 2, 2012 2:22 PM
  • Cor,

    The method can also be used for function ... Or dynamicaly create event handlers

    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim Del As [Delegate]
            Del = [Delegate].CreateDelegate(GetType(Action(Of String, Integer)), Me, "MyMethod1")
            ABC.DEF(Del, "AString", 22)
            Del = [Delegate].CreateDelegate(GetType(Func(Of String, Integer)), Me, "MyMethod2")
            Dim TheCount As Integer = CInt(ABC.DEF(Del, "AString"))
            Stop
            Del = [Delegate].CreateDelegate(GetType(EventHandler), Me, "Button1_Click")
            Button1.GetType.GetEvent("Click").AddEventHandler(Button1, Del)
        End Sub
        Private Sub MyMethod1(ByVal A As String, ByVal B As Integer)
            Stop
        End Sub
        Private Function MyMethod2(ByVal A As String) As Integer
            Return A.Length
        End Function
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Beep()
        End Sub
    End Class
    Class ABC
        Public Shared Function DEF(ByVal D As [Delegate], ByVal ParamArray Arg() As Object) As Object
            Return D.DynamicInvoke(Arg)
        End Function
    End Class

    Friday, March 2, 2012 2:46 PM