Visual Basic Concepts

Implementing Properties in Components

See Also

Adding Properties to a Class," in "Programming with Objects," in the Visual Basic Programmer’s Guide discusses in detail the many kinds of properties you can add to your classes, including simple data values, read-only properties, and property arrays.

"Adding Properties to a Class" also describes the two ways you can declare properties: as public variables, or as property procedures.

In general, properties of objects provided by components should be implemented as property procedures. Property procedures are more robust than data members. A property whose type is an enumeration, for example, cannot be validated unless implemented as a Property Get and Property Let.

The only exception to this rule is a simple numeric or string property which requires no validation and which, when changed, does not immediately affect other properties of the object.

An object property — that is, any property that contains an object reference instead of an ordinary data type — should almost always be implemented with property procedures. An object property implemented as a public object variable can be set to Nothing accidentally, possibly destroying the object. This is discussed in "Organizing Objects: The Object Model," later in this chapter.

Note   Internally, Visual Basic generates a pair of property procedures for every public variable you declare. For this reason, declaring public variables doesn’t provide any size or performance benefits.

For More Information   See "Adding Properties to a Class," in "Programming with Objects," in the Programmer’s Guide.