MSDN Documentation

Objects and Classes in VB.NET

Object-Oriented Programming (OOP) is a fundamental paradigm in modern software development, and VB.NET provides robust support for creating and manipulating objects. Objects are instances of classes, which serve as blueprints defining the data (properties) and behavior (methods) that an object will have.

Understanding Classes

A class is a user-defined type that acts as a template for creating objects. It encapsulates data members (fields or properties) and methods (functions or subroutines) that operate on that data.

Defining a Class: The `Car` Example

Let's define a simple `Car` class:


Public Class Car
    ' Properties
    Public Property Make As String
    Public Property Model As String
    Public Property Year As Integer
    Public Property IsEngineRunning As Boolean

    ' Constructor
    Public Sub New(make As String, model As String, year As Integer)
        Me.Make = make
        Me.Model = model
        Me.Year = year
        Me.IsEngineRunning = False ' Default state
    End Sub

    ' Methods
    Public Sub StartEngine()
        If Not IsEngineRunning Then
            IsEngineRunning = True
            Console.WriteLine("The engine has started.")
        Else
            Console.WriteLine("The engine is already running.")
        End If
    End Sub

    Public Sub StopEngine()
        If IsEngineRunning Then
            IsEngineRunning = False
            Console.WriteLine("The engine has stopped.")
        Else
            Console.WriteLine("The engine is already stopped.")
        End If
    End Sub

    Public Sub DisplayCarInfo()
        Console.WriteLine($"Car: {Year} {Make} {Model}")
        Console.WriteLine($"Engine running: {IsEngineRunning}")
    End Sub
End Class
                

Creating Objects (Instantiating Classes)

Once a class is defined, you can create objects (instances) of that class using the New keyword.

Instantiating the `Car` Class


' In another part of your code (e.g., a Form or Module)
Dim myCar As New Car("Toyota", "Camry", 2022)

' Accessing properties
Console.WriteLine($"My car's make: {myCar.Make}")
myCar.Make = "Honda" ' Modifying a property

' Calling methods
myCar.StartEngine()
myCar.DisplayCarInfo()
myCar.StopEngine()
                

Properties

Properties are members of a class that provide a flexible mechanism to read, write, or compute the value of a private field. They allow you to control access to your data and can include validation logic.

Auto-Implemented Property


Public Class Person
    ' Auto-implemented property
    Public Property Name As String
End Class
                

Methods

Methods define the actions or behaviors that an object can perform. They are essentially functions or subroutines associated with a class.

Constructors

Constructors are special methods that are automatically called when an object is created. They are used to initialize the object's state. In VB.NET, constructors are named New. A class can have multiple constructors (constructor overloading) to allow for different ways of creating objects.

Default Constructor

If you don't define any constructors in your class, VB.NET provides a default parameterless constructor.

Key Object-Oriented Concepts in VB.NET

Mastering objects and classes is crucial for building scalable, maintainable, and robust applications in VB.NET. Explore concepts like inheritance, interfaces, and abstract classes to further leverage object-oriented principles.