Saturday, July 20, 2013

Delegates vb.net

Delegates are function pointers that store function address.

There are 3 steps to understand Delegates:
  1. Defining Delegate
  2. Instantiating Delegate
  3. Assigning Address of function to Delegate

Nothing fancy; just memorize the above steps to absorb the whole idea.

'How to define?
Public Delegate Sub testfile(Byval filename As String)

'instantiating the delegate
Dim mytestfile As testfile

Private Sub DoWork()
    'now assign the address of function to the delegate
    mytestfile = AddressOf FileTester

    'it can be called as
    mytestfile("test.txt"
    'or
    mytestfile.Invoke("test.txt")
End Sub
Public Sub FileTester(ByRef filename As String)
     MsgBox(filename)
End Sub


Now FileTester is really the function will be called which just shows the filename

Happy coding!

No comments: