Results 1 to 17 of 17

Thread: What is different from a Delegate to a pointer?

Threaded View

  1. #10
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: What is different from a Delegate to a pointer?

    Let me explain.
    When you define a type Integer you say that you will need at least 4 bytes to store its value. By itself Integer is just a type - you cannot store actual values in it.

    But when you declare a variable:
    vb.net Code:
    1. Dim i As Integer
    - you say that you need a place in your app's memory heap to store the values for this variable.

    the same with delegates
    You declare that there is somewhere a method that accepts two integers and returns a string.
    So you declare a delegate:
    vb.net Code:
    1. Delegate Function MyDelegate(ByVal Arg1 As Integer, ByVal Arg2 As Integer) As String

    The part (ByVal Arg1 As Integer, ByVal Arg2 As Integer) As String is called a method's signature and it says that when you need to invoke something using this delegate you will have to provide exactly two integers and expect a string for return value.

    So now, I'm designing some class that uses a callback procedure. The user has to know what is the signature for that callback, so you declare the member like this:

    vb.net Code:
    1. Private Function MyWork(ByVal id As MyDelegate) As String
    2.         ' I do not know what will be done with these two integers, I simply invoke
    3.         ' the delegate provided, get the return value and return it back
    4.         Dim PassedInt1 As Integer = 1
    5.         Dim PassedInt2 As Integer = 2
    6.         Dim ret As String = id.Invoke(PassedInt1, PassedInt2)
    7.         Return ret
    8. End Function

    This can be helpful if you are working with third party components and you will have to redefine the behaviour of the algorithm from time to time.

    Of course we need a callback procedure that will be invoked by the MyWork, note that its 'signature' corresponds with the delegate signature:
    vb.net Code:
    1. Private Function MyCallBack1(ByVal i1 As Integer, ByVal i2 As Integer) As String
    2.         Return (i1 + i2).ToString
    3. End Function


    But what if we wanted to redefine the way our integers will be processed? Well, we provide another callback procedure for that. Note again that our second callback procedure has the identical 'signature':

    vb.net Code:
    1. Private Function MyCallBack2(ByVal i1 As Integer, ByVal i2 As Integer) As String
    2.         ' I can redefine the callback procedure
    3.         Return (i1 * i2).ToString
    4. End Function


    I call the MyWork function (it can be done even from a remote machine) and provide it with two pointers of the type MyDelegate:
    vb.net Code:
    1. Private Sub DoMyWork()
    2.         MsgBox(MyWork(AddressOf MyCallBack1))
    3.         MsgBox(MyWork(AddressOf MyCallBack2))
    4. End Sub

    Hope this helps. Sometimes using delegates is the only way out when you're in a multithreaded or distributed environment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width