Visual Basic Concepts

Adding Classes to Components

Only one thing distinguishes a component from other applications you author using Visual Basic: A component project has at least one public class from which client applications can create objects.

Like any other Visual Basic application, your component may have numerous class modules that encapsulate its internal functionality. When you allow clients to create instances of a class, objects created from that class can be manipulated by clients, and your application becomes a component.

Creating New Classes

From the Project menu, you can choose Add Class Module, Add User Control, or Add User Document to define a new public class. Other choices on the Project menu allow you to add objects that can be used within your application, but only UserControls, UserDocuments, and class modules can define public classes.

Each public class you add will be the blueprint for one kind of public object in your object model. You can provide a class name, define interfaces for the class, and set the Instancing property (or the Public property, in some cases) to determine how objects will be created from the class.

Name Property

Choose class names carefully. They should be short but descriptive, and formed from whole words with individual words capitalized — for example, BusinessRule.

The class name is combined with the name of the component to produce a fully qualified class name, also referred to as a programmatic ID or ProgID. For example, the fully qualified class name of a BusinessRule class provided by the Finance component, is Finance.BusinessRule.

The topic "What’s in a Name?" earlier in this chapter, outlines the rules for naming classes, properties, and methods.

Defining Interfaces

The default interface for a class is composed of the properties and methods you define for it, as discussed in "Adding Properties and Methods to Classes," later in this chapter.

The default interface of a class is an incoming interface, as explained in "Polymorphism, Interfaces, Type Libraries, and GUIDs." You can also add outgoing interfaces, or events, as described in "Adding Events to Classes."

Visual Basic includes information about the class module’s default interface and outgoing interfaces in the type library it creates when your component is compiled.

*For More Information* You can implement additional incoming interfaces on a class, as described in "Providing Polymorphism by Implementing Interfaces."

Public or Instancing Property

UserControl classes have a Public property that determines whether the class is public or private. UserDocument classes are always public. This is discussed in the in-depth chapters on ActiveX controls and documents, "Building ActiveX Controls" and "Building ActiveX Documents."

Class modules have a more complex public life, controlled by the Instancing property. For each class your component will provide to other applications, set the Instancing property of the class module to any value except Private, as discussed in the related topic "Instancing for Classes Provided by ActiveX Components."

You don’t have to make all your classes public; if there are objects you want to use only within your component, set the Instancing properties of the class modules that define them to Private. (For a UserControl, set the Public property to False.)

The following topics provide detailed information about the features of classes in Visual Basic projects.

*For More Information* Class modules in Visual Basic are introduced in "Programming with Objects," in the Visual Basic Programmer’s Guide. Topics specific to classes defined in class modules, ActiveX controls, and ActiveX documents are discussed in depth in "Building Code Components," "Building ActiveX Controls," and "Building ActiveX Documents." Object models are discussed in "Organizing Objects: The Object Model," later in this chapter.