Referring to members of an object

You can use the With statement as a quick way to access class members of a given object. You can also use the With statement to test expressions using an object's members.

The syntax is:

With objectRef


End With




An expression whose value is a reference to an object. For example, objectRef can be a function call that returns an object reference or a Variant that contains an object reference.


One or more statements.

The With statement itself may be nested up to 16 levels.

This example uses the With statement to refer to members of an object using a dot to represent the object name (startEmp).

Class Employee
   Public empName As String   
   Public newName As String
   '  Sub GetName prompts for and accepts input to newName.
   Sub GetName
      newName$ = InputBox$("Enter name:" , "New Name" )
   End Sub
End Class

Dim startEmp As New Employee
'  Sub SetEmp puts information into the new employee object. 
Sub SetEmp (E As Employee)
   With E
      Call .GetName  ' Prompts for input to startEmp.newName$.
      .empName$ = .newName$     
   End With
End Sub
Call SetEmp(startEmp)

Outside the With statement, you need to specify the entire reference. For example:

Employee.empName$ = .newName$