Udostępnij za pośrednictwem


Object Members

Objects are the basic units of object-oriented programming. An object is an element of an application, representing an instance of a class. Fields, properties, methods, and events are the building blocks of objects and constitute their members.

Objects

An object represents an instance of a class, such as Form or Label. You must create an object before you can access its nonshared members. To do this, you use the New keyword to specify the class from which to create the object, and then assign the new object to an object variable.

Dim warningLabel As New System.Windows.Forms.Label

For more information, see How to: Create an Object.

Member Access

You access a member of an object by specifying, in order, the name of the object variable, a period (.), and the name of the member. The following example sets the Text property of a Label object.

warningLabel.Text = "Data not saved"

Fields and Properties

Fields and properties represent information stored in an object. You retrieve and set their values with assignment statements the same way you retrieve and set local variables in a procedure. The following example retrieves the Width property and sets the ForeColor property of a Label object.

Dim warningWidth As Integer = warningLabel.Width
warningLabel.ForeColor = System.Drawing.Color.Red

Note that a field is also called a member variable.

For more information, see Property Procedures vs. Fields.

Methods

A method is an action that an object can perform. For example, Add is a method of the ComboBox object that adds a new entry to a combo box.

The following example demonstrates the Start method of a Timer object.

Dim safetyTimer As New System.Windows.Forms.Timer
safetyTimer.Start()

Note that a method is simply a procedure that is exposed by an object.

For more information, see How to: Perform Actions with Methods.

Events

An event is an action recognized by an object, such as clicking the mouse or pressing a key, and for which you can write code to respond. Events can occur as a result of a user action or program code, or they can be caused by the system. Code that signals an event is said to raise the event, and code that responds to it is said to handle it.

You can also develop your own custom events to be raised by your objects and handled by other objects. For more information, see Events and Event Handlers.

Instance Members and Shared Members

When you create an object from a class, the result is an instance of that class. Members that are not declared with the Shared (Visual Basic) keyword are instance members, which belong strictly to that particular instance. An instance member in one instance is independent of the same member in another instance of the same class. An instance member variable, for example, can have different values in different instances.

Members declared with the Shared keyword are shared members, which belong to the class as a whole and not to any particular instance. A shared member exists only once, no matter how many instances of its class you create, or even if you create no instances. A shared member variable, for example, has only one value, which is available to all code that can access the class.

IntelliSense Listing of Members

IntelliSense lists members of a class when you invoke its List Members option, for example when you type a period (.) as a member-access operator. If you type the period following the name of a variable declared as an instance of that class, IntelliSense lists all the instance members and none of the shared members. If you type the period following the class name itself, IntelliSense lists all the shared members and none of the instance members. For more information, see Using IntelliSense.

See Also

Concepts

Relationships Among Objects

Other Resources

Objects in Visual Basic