Closer Look: Understanding Properties, Methods, and Events

All objects in the Visual Basic language have their own properties, methods, and events. This includes forms and controls. Properties can be thought of as the attributes of an object, methods as its actions, and events as its responses.

An everyday object such as a helium balloon also has properties, methods, and events. A balloon's properties include visible attributes such as its height, diameter, and color. Other properties describe its state (inflated or deflated), or attributes that are not visible, such as its age. All balloons have these properties, although the values of these properties may differ from one balloon to another.

A balloon also has known methods or actions that it can perform. It has an inflate method (filling it with helium), a deflate method (expelling its contents), and a rise method (letting go of it). Again, all balloons can perform these methods.

Balloons also have responses to certain external events. For example, a balloon responds to the event of being punctured by deflating, or to the event of being released by rising.

Properties, Methods, and Events

Balloons

A balloon has properties (Color, Height, and Diameter), responds to events (Puncture), and can perform methods (Deflate, MakeNoise).

Properties

If you could program a balloon, the Visual Basic code might resemble the following "code," which sets a balloon's properties.

Balloon.Color = Red

Balloon.Diameter = 10

Balloon.Inflated = True

Notice the order of the code—the object (Balloon), followed by the property (Color), followed by the assignment of the value (= Red). You could change the balloon's color by substituting a different value.

Methods

A balloon's methods are called as follows.

Balloon.Inflate

Balloon.Deflate

Balloon.Rise(5)

The order resembles that of a property—the object (a noun), followed by the method (a verb). In the third method, there is an additional item, called an argument, which specifies the distance the balloon will rise. Some methods will have one or more arguments to further describe the action to be performed.

Events

The balloon might respond to an event as follows.

Sub Balloon_Puncture()
    Balloon.MakeNoise("Bang")
    Balloon.Deflate
    Balloon.Inflated = False
End Sub

In this case, the code describes the balloon's behavior when a Puncture event occurs. When this event occurs, call the MakeNoise method with an argument of "Bang" (the type of noise to make), then call the Deflate method. Since the balloon is no longer inflated, the Inflated property is set to False.

While you can't actually program a balloon, you can program a Visual Basic form or control. As the programmer, you are in charge. You decide which properties should be changed, which methods should be invoked, or which events should be responded to in order to achieve the desired appearance and behavior.

Next Steps

In the next lesson, you will add a line of code to your program.

Next Lesson: Step 4: Add Visual Basic Code.

See Also

Other Resources

Visual Basic Programming Guide

Programming with Objects: Using Classes