What Is a Class?

In this lesson, you will learn how to use classes to represent objects in your programs.

As you learned in an earlier lesson, Visual Basic programs are built with objects such as forms or controls. Objects can also be used to represent real-world things, such as a person, a computer, or even something more abstract, such as a bank account.

A class is simply a representation of a type of object; think of it as a blueprint that describes the object. Just as a single blueprint can be used to build multiple buildings, a class can be used to create multiple copies of an object.

Although you may not have realized it, you have already used classes. For example, the TextBox control is defined by a TextBox class, which defines its appearance and its capabilities. Each time you drag a TextBox control onto a form, you are actually creating a new instance of the TextBox class.

Each TextBox control is an exact yet distinct copy of the class that defines it, the TextBox class. Because each object is a separate "instance" of a class, the act of creating a class is called instantiation.

So far you have added TextBox controls to your form by dragging them from the Toolbox, but you can also instantiate a TextBox object in your code by declaring it with the New keyword.

Dim Textbox1 As New TextBox

You will learn more about creating and using classes in the next few lessons.

What's in a Class?

In an earlier lesson, Closer Look: Understanding Properties, Methods, and Events, you learned that all objects have properties that describe their attributes, methods that define their actions, and events that define their responses. Likewise, the class that defines an object has its own properties, methods, and events (sometimes called members) that are passed on to all instances of that class.

For example, a class that represents a bank account might have properties such as AccountNumber or AccountBalance, methods such as CalculateInterest, and events such as BalanceChanged. Once you instantiate a bank-account object, you can access its properties, methods and events just as you would with an object such as TextBox.

Some members of a class are private—they can only be accessed by code within the class. For example, a bank-account class might have a method to calculate a balance. You would allow a program to read that balance, but you would not want the program to be able to change the balance directly.

You can hide members in a class by declaring them as Private, or you can expose them by declaring them as Public. You can also allow access to a property but prevent the program from changing its value by declaring it as ReadOnly. The following code shows what a BankAccount class might look like.

Class BankAccount
    Private AccountNumber As String
    Private AccountBalance As Decimal
    Public Sub UpdateBalance()
        ' add code to recalculate balance.
    End Sub
    ReadOnly Property Balance() As Decimal
        Get
            Return AccountBalance
        End Get
    End Property
End Class

Next Steps

In this topic, you learned the basics of classes, as well as some new terminology. In the next lesson, you will learn how to create a class.

Next Lesson: Modeling a Real-World Object: Creating Your First Class.

See Also

Concepts

Closer Look: Understanding Properties, Methods, and Events

Other Resources

Programming with Objects: Using Classes

Classes: Blueprints for Objects