Assigning
After a variable has been declared (and initialised with its default value) it can be assigned a different value.
The assignment operator "=" can be used to assign a value to a variable.
This operator writes the value of the constant or expression on the right hand side and assigns it to the variable on the left hand side.
All variables need to be declared before they can be assigned a value.
Value Data Types
When a variable has a Value data type the assignment operator is used.
Dim myLong As Long
myLong = 20
myLong = 30
Dim myString As String
myString = "some"
myString = "text"
Reference Data Types
When a variable has a Reference data type the assignment operator is used but you also need to use the Set keyword.
The Set statement must be used to assign an object reference to an object variable.
Dim colEmployees As Collection
Set colEmployees = New Collection
Dim oEmployee As class_Employee
Set oEmployee = New class_Employee
When an object is assigned to a reference data type this process is called binding.
The object is early bound (and uses Early Binding) when it is assigned to a variable declared to be of a specific data type.
The object is late bound (and uses Late Binding) when it is assigned to a variable declared to be of data type Object.
Dim oMyObject As Object
Set oMyObject = New Library.ClassName ' uses early binding
Set oMyObject = CreateObject("Library.ClassName") ' uses late binding
If you don't know the data type, you can use the Object data type.
Dim As New
Never declare a reference data type using the "New" keyword.
more details
Remember
It is more efficient to create an object variable if you are going to refer to an object more than once.
Since object variables are pointers it is possible for more than one object variable to refer to the same object.
If you are using object variables then it is important to keep track of what they are referencing.
It is not a good idea to have more than one object variable pointing to the same object.
© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext