Using Your Object

Using your own objects is a little different than using intrinsic Excel objects. With all of the Excel objects you've used, the object already existed before you used it. All you had to do was set a reference to the object using the Set keyword. When you did add a new Excel object such as a worksheet, you used the Add method of one of the various Collection objects (such as Worksheets). Collection objects provide methods for creating and adding a new object to the collection.

When you use your own object or, as you'll see later, an object from a class library other than Microsoft Excel, you need to use the New keyword to create or instantiate an object. You can instantiate an object in one of two ways.

First, you can use the New keyword in the declaration of the variable that you'll use to refer to the object. The second way is to declare the variable as you would any other variable and then, before using the variable, explicitly create the object and point the variable to it. Listing 11.2 shows an example of both methods. As you enter this listing, you'll witness another minor benefit of developing and using classes. Look at what you find in the Auto List Members dropdown list. Do you see it in Figure 11.3? That new object named SimpleLoan that looks like it is a native object in Excel?

0 0

Post a comment