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.
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
The topic "What’s in a Name?" earlier in this chapter, outlines the rules for naming classes, properties, and methods.
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.)
Related Class Creation Topics
The following topics provide detailed information about the features of classes in Visual Basic projects.
Instancing for Classes Provided by ActiveX Components The Instancing property of class modules determines whether an object can be created by clients, always starts a new instance of the component, is global, or (in some cases) is on a separate thread.
Coding Robust Initialize and Terminate Events What happens when an error occurs as an object is being created or destroyed? How you answer that question can affect the stability of programs that use your component.
Standard Modules vs. Class Modules Public variables in class modules and standard modules have different scope and visibility.
*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.