VBA An Object's instance
Here is a question on VBA
I just cannot grasp clearly what is an instance of an object i've searched my VBA books and i have not found a clear answer. Phrases like:
- An instance of Word
- An instance of the class module
- The procedure declares an object Employee as a new instance of clsEmployee.
For me an object i mean if a Range("A1") is Set then what can possibly change and how we can tell for example if we have 10 loop iterations that change the value of Range A1 object an instance from another instance?
I' ve even heard instance of a variable!
Thank you for watching my question
"An instance is a specific realization of any class..." Instance (computer science) on Wikipedia
Sub Example() Dim Word1 As Word.Application Dim Word2 As Word.Application Dim Word3 As Word.Application 'Instantiate the first instance of a Word application Set Word1 = New Word.Application 'Instantiate the second instance of a Word application Set Word2 = New Word.Application 'Point our third Word object variable to the existing instance created above Set Word3 = Word1 End Sub
The above code contains three object variables (Word1, Word2, Word3) derived from a single class (Word.Application) representing two distinct instances (Word1 and Word3 point to the same instance and Word2 points to the second instance).
It is important to note that the line Set Word3 = Word1 does not copy the contents of the Word1 object instance to Word3. Word1 really only contains a pointer to a location in memory where the object resides.
The line Set Word3 = Word1 is saying copy the memory pointer from variable Word1 into variable Word3. So any change you make to the Word1 object instance will be reflected in the Word3 object instance...because they are both pointing to the same instance.
In example form:
Set Word3 = Word1 Word1.Visible = True Debug.Print Word1.Visible 'outputs True Debug.Print Word3.Visible 'outputs True Word1.Visible = False Debug.Print Word1.Visible 'outputs False Debug.Print Word3.Visible 'outputs False
Every time you mention any object in your code, either an object or a Variable, you have to instantiation it.
This means you have to create an instance (an existence of the object of variable).
for every time in your could you call Dim x as something you have not creating what something is, you are creating a new instance of a something, a whole new entity that takes on the form of whatever you specify it to be.
So by saying Dim strSample As String you are creating a new instance of an object or variable that takes on the properties and methods of being a String
In other works an instance is an existance of an object or variable, every time you have an object or a variable it exists, and in order to exist it has it's own instance.
You are creating a new object. or instantiation
You are an instance of a person, you are not an instance of a bird. You are not the only person ever made though, there are other instances of a person.