VBA TIPS
What is a Class? – Basic understanding of a Class Module – Chapter2

A Class and an Instance

What is an Instance

The instance is an object that became a condition that can be used by making the class content. When you want to use the specified object in the class module, it is necessary to prepare a variable. This special variable is called an Object variable. Let's think about this. Giving an example is the best way to explain what it means.

For instance, PHONE cannot be operated directly something like

PHONE.NUMBER="1234"

even if there is a Class named PHONE.

Because the PHONE is just a name of a Class, and any contents doesn't exist in the class. So it's the turn of an Object Variable.

First, you need to declare an Object Variable MyPhone as an object of the Phone Class.

Dim MyPhone As PHONE

So, you can handle this declared object like this.

MyPhone.NUMBER="1234"

However, the error "The object variable is not set" occurs when you execute above code it as it is. To tell the truth, an object variable is only a container for the content of the object, it will not become an object only by being declared. Then, you need an extra work that generates a "content of the object" and puts that in this container.


an Object Variable

an Instance

an Object Variable with an Instance

a Class

An Object Variable

An Instance

An Object Variable with an instance

A Class

An object variable is just like a container A content that is entered into an object variable An Object variable with an Instance it's just a blueprint


You can generate a new instance of the class as follows.

Set MyPhone = New PHONE 'Syntax Set object = New class

The content of the object is called Instance.

Set is a necessary key word when you substitute the object instance. You cannot omit this key word when you handle an object variable. In fact, for the normal variables the keyword Let exists but normally we can omit this.
New is a key word that generates a new object instance.

So the bottom line of how to handle an object variable of a Class is as follows. Finally you can handle a variable MyPhone as an instance of the PHONE Class.

Dim MyPhone As PHONE

Set MyPhone = New PHONE
Let MyPhone.Number = "1234"



To be continued to the next chapter.

| HOME |
Copyright © cellmasters.net - colo's junk room All Right Reserved
ABOUT
WORKS
THE CELL MASTERS
CONTACT
LINKS
Tips and Information about Microsoft Excel|Masaru Kaji aka Colo