Dela via


Relationships Among Objects

Objects can be related to each other in several ways. The principal kinds of relationship are hierarchical and containment.

Hierarchical Relationship

When classes are derived from more fundamental classes, they are said to have a hierarchical relationship. Class hierarchies are useful when describing items that are a subtype of a more general class. For example, in the System.Windows.Forms namespace, the Label and TextBox classes both derive from the Control class. Derived classes inherit members from the class they are based on, allowing you to add complexity as you progress in a class hierarchy.

Containment Relationship

Another way that objects can be related is a containment relationship. Container objects logically encapsulate other objects. For example, the OperatingSystem object logically contains a Version object, which it returns through its Version property. Note that the container object does not physically contain any other object.

Collections

One particular type of object containment is represented by collections. Collections are groups of similar objects that can be enumerated. Visual Basic supports a specific syntax in the For Each...Next Statement (Visual Basic) that allows you to iterate through the items of a collection. Additionally, collections often allow you to use an Item Property (Collection Object) to retrieve elements by their index or by associating them with a unique string. Collections can be easier to use than arrays because they allow you to add or remove items without using indexes. Because of their ease of use, collections are often used to store forms and controls.

See Also

Concepts

Object Members

Collections in Visual Basic

Objects from Visual Basic and Other Sources

Other Resources

Designing an Inheritance Hierarchy

Creating and Using Objects