This documentation is archived and is not being maintained.

Objects, Properties, Methods, and Events

Objects, properties, methods, and events are the basic units of object-oriented programming. An object is an element of an application, representing an instance of a class. 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, Control, or Component. In Visual Basic code, you must instantiate an object before you can apply one of the object's methods or change the value of one of its properties. Instantiation is the process by which an instance of a class is created and assigned to an object variable. An example is shown below:

Dim x As New MyClass()

In this example, the variable x is assigned to refer to a new instance of the class MyClass.

Properties

A property is an attribute of an object that defines one of the object's characteristics, such as size, color, or screen location, or an aspect of its behavior, such as whether it is enabled or visible. To change the characteristics of an object, you change the values of its corresponding properties.

To set the value of a property, affix the reference to an object with a period, the property name, an equal sign (=), and the new property value. For example, the following procedure changes the caption of a Visual Basic Windows Form by setting the Text property:

Public Sub ChangeName(newTitle)
   myForm.Text = newTitle
End Sub

You can't set some properties. The Help topic for each property indicates whether you can set that property (read/write), only read the property (read-only), or only write the property (write-only).

You can retrieve information about an object by returning the value of one of its properties. The following procedure uses a message box to display the title that appears at the top of the currently active form.

Public Sub GetFormName()
   Dim formName As String
   formName = myForm.Text
   MsgBox(formName)
End Sub

Methods

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

The following example demonstrates the Start method of a Timer component:

' Instantiates a Timer object.
Dim myTimer As New System.Windows.Forms.Timer()   
' Invokes the Start method of myTimer.
MyTimer.Start

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 triggered by the system. You can also develop your own custom events to be raised by your objects and handled by other objects. For further information, see Events and Delegates.

See Also

Objects | Setting and Retrieving Properties | Performing Actions with Methods | Events and Delegates

Show: