The Basics of Working with Objects

See Also

Visual Basic objects support properties, methods, and events. In Visual Basic, an object's data (settings or attributes) are called properties, while the various procedures that can operate on the object are called its methods. An event is an action recognized by an object, such as clicking a mouse or pressing a key, and you can write code to respond to that event.

You can change an object's characteristics by changing its properties. Consider a radio: One property of a radio is its volume. In Visual Basic, you might say that a radio has a "Volume" property that you can adjust by changing its value. Assume you can set the volume of the radio from 0 to 10. If you could control a radio with Visual Basic, you might write code in a procedure that changes the value of the "Volume" property from 3 to 5 to make the radio play louder:

Radio.Volume = 5

In addition to properties, objects have methods. Methods are a part of objects just as properties are. Generally, methods are actions you want to perform, while properties are the attributes you set or retrieve. For example, you dial a telephone to make a call. You might say that telephones have a "Dial" method, and you could use this syntax to dial the seven-digit number 5551111:

Phone.Dial 5551111

Objects also have events. Events are triggered when some aspect of the object is changed. For example, a radio might have a "VolumeChange" event. A telephone might have a "Ring" event.

Controlling Objects with Their Properties

Individual properties vary as to when you can set or get their values. Some properties can be set at design time. You can use the Properties window to set the value of these properties without writing any code at all. Some properties are not available at design time; therefore, you must write code to set those properties at run time.

Properties that you can set and get at run time are called read-write properties. Properties you can only read at run time are called read-only properties.

Setting Property Values

You set the value of a property when you want to change the appearance or behavior of an object. For example, you change the Text property of a text box control to change the contents of the text box.

To set the value of a property, use the following syntax:

object.property = expression

The following statements demonstrate how you set properties:

Text1.Top = 200   ' Sets the Top property to 200 twips.
Text1.Visible = True            ' Displays the text box.
Text1.Text = "hello"   ' Displays 'hello' in the text 
                     ' box.

Getting Property Values

You get the value of a property when you want to find the state of an object before your code performs additional actions (such as assigning the value to another object). For example, you can return the Text property of a text box control to determine the contents of the text box before running code that might change the value.

In most cases, to get the value of a property, you use the following syntax:

variable = object.property

You can also get a property value as part of a more complex expression, without assigning the property to a variable. In the following code example, the Top property of the new member of a control array is calculated as the Top property of the previous member, plus 400:

Private Sub cmdAdd_Click()
   ' [statements]
   optButton(n).Top = optButton(n-1).Top + 400
   ' [statements]
End Sub

Tip   If you're going to use the value of a property more than once, your code will run faster if you store the value in a variable.

Performing Actions with Methods

Methods can affect the values of properties. For example, in the radio analogy, the SetVolume method changes the Volume property. Similarly, in Visual Basic, list boxes have a List property, which can be changed with the Clear and AddItem methods.

Using Methods in Code

When you use a method in code, how you write the statement depends on how many arguments the method requires, and whether the method returns a value. When a method doesn't take arguments, you write the code using the following syntax:

object.method

In this example, the Refresh method repaints the picture box:

Picture1.Refresh   ' Forces a repaint of the control.

Some methods, such as the Refresh method, don't have arguments and don't return values.

If the method takes more than one argument, you separate the arguments with a comma. For example, the Circle method uses arguments specifying the location, radius, and color of a circle on a form:

' Draw a blue circle with a 1200-twip radius.
Form1.Circle (1600, 1800), 1200, vbBlue

If you keep the return value of a method, you must enclose the arguments in parentheses. For example, the GetData method returns a picture from the Clipboard:

Picture = Clipboard.GetData (vbCFBitmap)

If there is no return value, the arguments appear without parentheses. For example, the AddItem method doesn't return a value:

List1.AddItem "yourname"      ' Adds the text 'yourname' 
                           ' to a list box.

For More Information   See the Language Reference for the syntax and arguments for all methods provided by Visual Basic.