Home > Language Reference > Functions > Objects

AddressOf Function

Returns the memory address of an object.

Syntax

Public Function AddressOf(ByRef object As Object) As Long

ParameterDescription
objectReference to the object.

Remarks

The value returned by the function AddressOf must be stored in a variable of type Long. Actually only variables of this type are scanned by the garbage collector as well as variables explicitly declared as objects. If the last reference on a given instance of an object is stored for example in a type Double, the garbage collector will not be able to detect it and it will be released early.

The inverse operation, to retrieve a reference on an object from it's memory address is achieved using the function ObjectAt. Note that the real type of the object is lost, and that an explicit type cast may be necessary. It means that your application should be structured in a way to know from the compilation the real type of the object to which you apply both the functions ObjectAt / AddressOf.

A typical use of this function is to store object references in the ItemData fields of a List or Popup controls. The following example illustrates this by allowing the user to choose an image from a list. To use this example, you should add two images called img1 and img2 to your project :

Private Sub Form_Load()
  List1.AddItem "Image #1", AddressOf(img1)
  List1.AddItem "Image #2", AddressOf(img2)
End Sub

Private Sub List1_Change()
  Dim k as Integer, bm as Bitmap

  k = List1.ListIndex
  If k>=0 Then
    Set bm = Bitmap(ObjectAt(List1.ItemData(k)))
    Set Picture1.Image = bm
  End If
End Sub

You cannot use the AddressOf function to retrieve the address of a procedure. This operation is carried out by the Callback function.

System requirements

SystemMinimal versionRemarks
Palm OSPalm OS 3.0N/A