Freetutes.com

VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

VB .NET - Learn Visual Basic .NET

Systems Analysis - System analysis and Design tutorial for Software Engineering


You are here: Visual Basic > Advanced VB6 tutorial > Chapter 10

Using the New Keyword to Declare and Instantiate a Class Object from a COM Component

After you've set a reference to a COM component in your application, you can instantiate objects from the component's classes in your application.

You may declare an object variable for a component class that you want to use. You can then use this object variable to point to an instance of the corresponding class.

The New keyword is Microsoft's preferred technique for instantiating object variables from server classes. You can use the New keyword in one of two ways, discussed in the following sections:

  • Instantiate the object at the same time you declare it by using the As New keyword.

  • Declare the object and instantiate it later with set / New.

Many COM components don't support the New keyword. Refer to each component's documentation to see whether it supports New. If the component you want to use doesn't support New, you will need to use the CreateObject or GetObject function, as discussed in the section of this chapter entitled "Using the CreateObject and GetObject Functions to Instantiate Objects."

NOTE - VB Version Support for the New Keyword: Components created in VB4, VB5, or VB6 always support the New keyword. See Chapters 12, 13, and 14 for a discussion of how to create ActiveX components in VB6.

Using As New to Instantiate an Object Variable When You Declare It

If the component whose class you want to instantiate supports the New keyword, all you need to do is declare a variable of appropriate scope (usually Private or Public), and the object is ready to use in your application. For example, if you want to use a class called MyClass from a component application called MyComp, your variable declaration in a General Declarations section might look like this:

Private objMyClass As New MyComp.MyClass

You could then manipulate the object's methods and properties and react to its events through your own application's code.

Using New to Instantiate an Object Variable After You Declare It

In the previous section, an object variable was instantiated at the same time it was declared. If you wanted to manage your object more tightly, you could wait until you actually needed to use the variable before instantiating it.

It's possible to declare an object variable without instantiating it and then instantiate it as needed with a combination of the Set statement and the New keyword, as illustrated in Listing 10.1.

LISTING 10.1INSTANTIATING AN OBJECT VARIABLE WITH THE New KEYWORD AFTER YOU HAVE DECLARED IT

'General Declarations
Private objMyClass As MyComp.MyClass
.
.
.
'later in your code
Set objMyClass = New Mycomp.MyClass

This second method, as illustrated in Listing 10.1, is preferred over the method of the previous section (declaring the variable with As New). The As New declaration requires extra runtime checking of the variable type.


  

<< Previous | Contents | Next >>

Home | About Us | Privacy Policy | Contact Us

Copyright © Freetutes.com | All Rights Reserved