No history yet

Modern OOP Principles

Beyond Scripts to Systems

In VBA, you write scripts that live inside a host application like Excel. Your code manipulates objects the application gives you. With VB.NET, you're in the driver's seat. You're not just scripting; you're building standalone software from the ground up. This requires a more structured approach called Object-Oriented Programming, or OOP.

OOP is a way of thinking that organizes complex programs into reusable blueprints called Classes. Think of a Class as a recipe for creating something. The things you create from that recipe are called Objects. A Car class could define what all cars have (properties like Color and Make) and what they can do (methods like StartEngine() and Accelerate()). Each individual car you create is an object, an instance of that class.

The four principles of object-oriented programming are encapsulation, abstraction, inheritance, and polymorphism.

These four pillars aren't just academic terms. They are practical tools that help you write code that is flexible, easy to maintain, and less prone to bugs. Let's break them down.

Encapsulation Your Digital Safe

Encapsulation is about bundling data and the methods that operate on that data into a single unit, the Class. More importantly, it's about controlling access to that data. You don't want other parts of your program to randomly change an object's state without permission.

In VB.NET, you achieve this using access modifiers like Public, Private, and Protected. A Public member is accessible from anywhere. A Private member is only accessible from within its own class. This prevents accidental changes and creates a clear, safe boundary.

To allow controlled access, we use Properties. Instead of making a variable Public, you make it Private and expose it through Public Get and Set accessors. This lets you add validation logic. For instance, you can ensure a car's Speed property never becomes negative.

Public Class Car
    ' This field is hidden from the outside world.
    Private _speed As Integer

    ' This Property provides controlled access.
    Public Property Speed As Integer
        Get
            Return _speed
        End Get
        Set(ByVal value As Integer)
            If value >= 0 Then
                _speed = value
            Else
                _speed = 0 ' Prevent negative speed
            End If
        End Set
    End Property
End Class

We also have Friend and Protected. A Friend member is visible to any code within the same project or assembly, which is perfect for building libraries where different components need to cooperate closely. Protected is used with inheritance, making a member available to its own class and any class that inherits from it.

Inheritance and Abstraction

Inheritance is the mechanism for creating a new class from an existing one. The new class, or derived class, inherits the properties and methods of the base class. This promotes code reuse. You could have a base Vehicle class, and then create more specific Car and Motorcycle classes that inherit from it.

Both Car and Motorcycle would automatically get the Speed property from Vehicle, but they could also have their own unique features. A Car might have a NumberOfDoors property, while a Motorcycle might have a HasSidecar property.

Sometimes you want to define a base class that can't be instantiated on its own. It only exists to be inherited from. This is an Abstract Class. It can define common functionality but leave some methods as MustOverride. These are methods without an implementation, forcing any derived class to provide one.

An Interface, on the other hand, is like a contract. It contains no implementation at all, only definitions for properties and methods. Any class that Implements an interface promises to provide its own code for every member defined in that contract. A single class can implement multiple interfaces, but it can only inherit from one base class. This makes interfaces a powerful tool for achieving without being restricted by single inheritance.

FeatureAbstract ClassInterface
ImplementationCan provide default codeCannot provide any code
InheritanceClass can only inherit oneClass can implement many
MembersCan have fields, constructorsOnly property/method signatures
UsageFor sharing common code in a hierarchyFor defining a capability or contract

Creating Objects

When you create an object from a class, a special method called a constructor is executed. In VB.NET, the constructor is always named Sub New. This is where you initialize the object's state, setting default values for its properties.

You can create multiple constructors with different parameters. This is called overloading. For example, you could have one Sub New() that creates a default Car and another Sub New(startColor As String) that sets the color upon creation.

Public Class Car
    Public Property Color As String

    ' Default constructor
    Public Sub New()
        Color = "Black"
    End Sub

    ' Overloaded constructor
    Public Sub New(ByVal startColor As String)
        Color = startColor
    End Sub
End Class

' Usage:
Dim defaultConfig As New Car() ' Color is "Black"
Dim customConfig As New Car("Red") ' Color is "Red"

The object's lifecycle begins when New is called and ends when no part of your program holds a reference to it. At that point, the .NET Framework's garbage collector automatically frees up the memory the object was using. This is a major difference from older systems where you had to manage memory manually.

With these principles, you're equipped to design software that is more than just a sequence of commands. You can create a logical, organized system of interacting objects that models the real world, making your code easier to build, test, and evolve.

Quiz Questions 1/6

What is the primary conceptual shift when moving from VBA to VB.NET, as described in the provided text?

Quiz Questions 2/6

In OOP, the principle of bundling data and the methods that operate on it into a single unit like a Class, and controlling access to that data, is known as what?