The generic As Object declaration

It is also possible to declare any Excel object using the generic object data type Object, as in the following example:

Dim chrt As Object

While you may see this declaration from time to time, it is much less efficient than a specific object declaration, such as:

Dim chrt As Chart

This is because Excel cannot tell what type of object the variable chrt refers to until the program is running, so it must use some execution time to make this determination. This is referred to as late binding and can make programs run significantly more slowly. (For more on late versus early binding, see Appendix E.) Thus, generic object declarations should be avoided.

We will discuss object variables in some detail in Chapter 9. However, we should briefly discuss the Set statement now, since it will appear from time to time in upcoming code examples.

0 0

Post a comment