Export (0) Print
Expand All
2 out of 6 rated this helpful - Rate this topic

AddressOf Operator

Creates a procedure delegate instance that references the specific procedure.

AddressOf procedurename

The required procedurename specifies the procedure that will be referenced by the newly created procedure delegate.

Remarks

The AddressOf operator creates a function delegate that points to the function specified by procedurename. When the specified procedure is an instance method then the function delegate refers to both the instance and the method so that when the function delegate is invoked the specified method of the specified instance is called.

The AddressOf operator can be used as the operand of a delegate constructor or it can be used in a context in which the type of the delegate can be determined by the compiler.

Example

This example uses the AddressOf operator to designate a delegate to handle the Click event of a button.

Public Sub ButtonClickHandler(ByVal sender As Object, e As _
                              System.EventArgs)
' Implementation code omitted.
End Sub

' Add the following line to Sub Form1_Load().
   AddHandler Button1.Click, AddressOf ButtonClickHandler

'The following example uses the AddressOf operator to designate the startup function for a thread.

Imports System.Threading   ' Access threading namespace.

Public Sub CountSheep()
   Dim i As Integer = 1 ' Sheep don't count from 0.
   Do While (True) ' Endless loop.
      Console.WriteLine("Sheep " & i & " Baah")
      i = i + 1
      Thread.Sleep(1000) 'Wait 1 second.
   Loop
End Sub

Sub UseThread()
   Dim t As New System.Threading.Thread(AddressOf CountSheep)
   t.Start()
End Sub

See Also

Declare Statement | Function Statement | Sub Statement | Delegates and the AddressOf operator

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.