Freigeben über


Visual Basic Concepts

Adding Properties and Methods to a Class

The properties and methods of a class make up its default interface. The default interface is the most common way of manipulating an object.

In general, properties represent data about an object, while methods represent actions an object can take. To put it another way, properties provide the description of an object, while methods are its behavior.

The following topics describe the mechanisms for adding properties and methods to your classes, and address some of the issues you'll meet.

  • Adding Properties to a Class   Discusses the two ways of adding properties to your classes — the easy way and the right way.

  • Property Procedures vs. Public Variables   Although property procedures are the best way to implement most properties, public variables have their uses.

  • Putting Property Procedures to Work for You   Provides full details of the way property procedures work, and describes the implementation of common property types — for example, read-only, read-write, object, and Variant.

  • Adding Methods to a Class   Methods — the behavior of an object — are just public procedures you add to your class module.

  • Is It a Property or a Method?   Sometimes it's not obvious whether the functionality you envision is best implemented as a property or a method. This topic provides some guidelines.

  • Making a Property or Method the Default   You can use the Procedure Attributes dialog to give your class a default member.

  • Friend Properties and Methods   Friend procedures are midway between Public and Private. Like Private, they aren't members of a class's interface, but like Public they're visible throughout your project.

    Note   Events aren't part of the default interface. Events are outgoing interfaces (that is, interfaces that reach out and touch other objects), while properties and methods belong to incoming interfaces (that is, interfaces whose members are invoked by other objects). The default interface of a Visual Basic object is an incoming interface.

Important   The following names cannot be used as property or method names, because they belong to the underlying IUnknown and IDispatch interfaces: QueryInterface, AddRef, Release, GetTypeInfoCount, GetTypeInfo, GetIDsOfNames, and Invoke. These names will cause a compilation error.

For More Information   Events are discussed in "Adding Events to a Class" later in this chapter.