Objects and Classes in Visual Basic
Updated: July 20, 2015
For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.
An object is a combination of code and data that can be treated as a unit. An object can be a piece of an application, like a control or a form. An entire application can also be an object.
When you create an application in Visual Basic, you constantly work with objects. You can use objects provided by Visual Basic, such as controls, forms, and data access objects. You can also use objects from other applications within your Visual Basic application. You can even create your own objects and define additional properties and methods for them. Objects act like prefabricated building blocks for programs — they let you write a piece of code once and reuse it over and over.
This topic discusses objects in detail.
Each object in Visual Basic is defined by a class. A class describes the variables, properties, procedures, and events of an object. Objects are instances of classes; you can create as many objects you need once you have defined a class.
To understand the relationship between an object and its class, think of cookie cutters and cookies. The cookie cutter is the class. It defines the characteristics of each cookie, for example size and shape. The class is used to create objects. The objects are the cookies.
You must create an object before you can access its members.
Determine from which class you want to create an object.
Write a Dim Statement to create a variable to which you can assign a class instance. The variable should be of the type of the desired class.
Dim nextCustomer As customer
Add the New Operator keyword to initialize the variable to a new instance of the class.
Dim nextCustomer As New customer
You can now access the members of the class through the object variable.
nextCustomer.accountNumber = lastAccountNumber + 1
Whenever possible, you should declare the variable to be of the class type you intend to assign to it. This is called early binding. If you do not know the class type at compile time, you can invoke late binding by declaring the variable to be of the Object Data Type. However, late binding can make performance slower and limit access to the run-time object's members. For more information, see Object Variable Declaration.
Objects newly created from a class are often identical to each other. Once they exist as individual objects, however, their variables and properties can be changed independently of the other instances. For example, if you add three check boxes to a form, each check box object is an instance of the CheckBox class. The individual CheckBox objects share a common set of characteristics and capabilities (properties, variables, procedures, and events) defined by the class. However, each has its own name, can be separately enabled and disabled, and can be placed in a different location on the form.
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.
warningLabel.Text = "Data not saved"
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.
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.
Use property procedures when:
You need to control when and how a value is set or retrieved.
The property has a well-defined set of values that need to be validated.
Setting the value causes some perceptible change in the object's state, such as an
Setting the property causes changes to other internal variables or to the values of other properties.
A set of steps must be performed before the property can be set or retrieved.
Use fields when:
The value is of a self-validating type. For example, an error or automatic data conversion occurs if a value other than
Falseis assigned to a
Any value in the range supported by the data type is valid. This is true of many properties of type
The property is a
Stringdata type, and there is no constraint on the size or value of the string.
For more information, see Property Procedures.
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 Procedures.
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.
When you create an object from a class, the result is an instance of that class. Members that are not declared with the Shared 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.
Make sure the object has been created from its class and assigned to an object variable.
Dim secondForm As New System.Windows.Forms.Form
In the statement that accesses the member, follow the object variable name with the member-access operator (
.) and then the member name.
Follow the class name with the member-access operator (
.) and then the member name. You should always access a
Sharedmember of the object directly through the class name.
MsgBox("This computer is called " & Environment.MachineName)
If you have already created an object from the class, you can alternatively access a
Sharedmember through the object's variable.
The main difference between classes and modules is that classes can be instantiated as objects while standard modules cannot. Because there is only one copy of a standard module's data, when one part of your program changes a public variable in a standard module, any other part of the program gets the same value if it then reads that variable. In contrast, object data exists separately for each instantiated object. Another difference is that unlike standard modules, classes can implement interfaces.
Classes and modules also use different scopes for their members. Members defined within a class are scoped within a specific instance of the class and exist only for the lifetime of the object. To access class members from outside a class, you must use fully qualified names in the format of Object.Member.
On the other hand, members declared within a module are publicly accessible by default, and can be accessed by any code that can access the module. This means that variables in a standard module are effectively global variables because they are visible from anywhere in your project, and they exist for the life of the program.
Objects let you declare variables and procedures once and then reuse them whenever needed. For example, if you want to add a spelling checker to an application you could define all the variables and support functions to provide spell-checking functionality. If you create your spelling checker as a class, you can then reuse it in other applications by adding a reference to the compiled assembly. Better yet, you may be able to save yourself some work by using a spelling checker class that someone else has already developed.
The .NET Framework provides many examples of components that are available for use. The following example uses the TimeZone class in the System namespace. TimeZone provides members that allow you to retrieve information about the time zone of the current computer system.
Public Sub examineTimeZone() Dim tz As System.TimeZone = System.TimeZone.CurrentTimeZone Dim s As String = "Current time zone is " s &= CStr(tz.GetUtcOffset(Now).Hours) & " hours and " s &= CStr(tz.GetUtcOffset(Now).Minutes) & " minutes " s &= "different from UTC (coordinated universal time)" s &= vbCrLf & "and is currently " If tz.IsDaylightSavingTime(Now) = False Then s &= "not " s &= "on ""summer time""." MsgBox(s) End Sub
Objects can be related to each other in several ways. The principal kinds of relationship are hierarchical and containment.
When classes are derived from more fundamental classes, they are said to have a hierarchical relationship. Class hierarchies are useful when describing items that are a subtype of a more general class.
Use a Class Statement to define a class from which to create the object you need.
Public Class reversibleButton
Be sure an
End Classstatement follows the last line of code in your class. By default, the integrated development environment (IDE) automatically generates an
End Classwhen you enter a
Classstatement immediately with an Inherits Statement. Specify the class from which your new class derives.
Your new class inherits all the members defined by the base class.
Add the code for the additional members your derived class exposes. For example, you might add a
reverseColorsmethod, and your derived class might look as follows:
Public Class reversibleButton Inherits System.Windows.Forms.Button Public Sub reverseColors() Dim saveColor As System.Drawing.Color = Me.BackColor Me.BackColor = Me.ForeColor Me.ForeColor = saveColor End Sub End Class
If you create an object from the
reversibleButtonclass, it can access all the members of the Button class, as well as the
reverseColorsmethod and any other new members you define on
Derived classes inherit members from the class they are based on, allowing you to add complexity as you progress in a class hierarchy. For more information, see Inheritance Basics.
Be sure the compiler can access the class from which you intend to derive your new class. This might mean fully qualifying its name, as in the preceding example, or identifying its namespace in an Imports Statement (.NET Namespace and Type). If the class is in a different project, you might need to add a reference to that project. For more information, see Managing references in a project.
Another way that objects can be related is a containment relationship. Container objects logically encapsulate other objects. For example, the OperatingSystem object logically contains a Version object, which it returns through its Version property. Note that the container object does not physically contain any other object.
One particular type of object containment is represented by collections. Collections are groups of similar objects that can be enumerated. Visual Basic supports a specific syntax in the For Each...Next Statement that allows you to iterate through the items of a collection. Additionally, collections often allow you to use an Item to retrieve elements by their index or by associating them with a unique string. Collections can be easier to use than arrays because they allow you to add or remove items without using indexes. Because of their ease of use, collections are often used to store forms and controls.
Walkthrough: Defining Classes
Provides a step-by-step description of how to create a class.
Overloaded Properties and Methods
Overloaded Properties and Methods
Covers inheritance modifiers, overriding methods and properties, MyClass, and MyBase.
Object Lifetime: How Objects Are Created and Destroyed
Discusses creating and disposing of class instances.
Describes how to create and use anonymous types, which allow you to create objects without writing a class definition for the data type.
Object Initializers: Named and Anonymous Types
Discusses object initializers, which are used to create instances of named and anonymous types by using a single expression.
How to: Infer Property Names and Types in Anonymous Type Declarations
Explains how to infer property names and types in anonymous type declarations. Provides examples of successful and unsuccessful inference.