100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Classes and Objects in VB.NET

Learn how VB.NET classes act as blueprints and objects as their instances, covering fields, methods, constructors, and Shared members.

OOP in VB.NETBeginner8 min readJul 10, 2026
Analogies

Understanding Classes and Objects

A Class in VB.NET is a blueprint that describes what data an object will hold and what actions it can perform, written between Class and End Class. An Object is a concrete instance of that blueprint, created in memory the moment you use the New keyword, such as Dim car As New Car(). Every object created from the same class shares the same structure but keeps its own independent copy of the data defined by that class.

🏏

Cricket analogy: A cricket team's playing eleven template describing roles like opener and all-rounder is like a Class -- when Virat Kohli or Rohit Sharma actually walks onto the field, that specific player is the Object created with New.

Defining a Class with Fields and Methods

Inside a Class block, fields declared with Dim or Private hold the object's state, while Sub and Function procedures define its behavior. A Sub performs an action and returns nothing, whereas a Function computes and returns a value via its declared return type. Because fields and the methods that manipulate them live inside the same Class, VB.NET keeps related data and logic bundled together rather than scattered across the program.

🏏

Cricket analogy: A batsman's scorecard fields -- runsScored, ballsFaced -- are like a Class's private fields, and a Function like CalculateStrikeRate() that reads those fields and returns a number mirrors how MS Dhoni's strike rate is computed straight from his innings data.

vbnet
Public Class BankAccount
    Private accountHolder As String
    Private balance As Decimal

    Public Sub New(holder As String, openingBalance As Decimal)
        accountHolder = holder
        balance = openingBalance
    End Sub

    Public Sub Deposit(amount As Decimal)
        balance += amount
    End Sub

    Public Function GetBalance() As Decimal
        Return balance
    End Function
End Class

Dim acct As New BankAccount("Priya Nair", 500D)
acct.Deposit(250D)
Console.WriteLine(acct.GetBalance())  ' 750

Constructors and Object Initialization

The Sub New procedure is a class's constructor, running automatically whenever New is used to create an instance. A parameterized Sub New requires callers to supply specific values at the moment of creation, guaranteeing an object can never exist without its essential data. VB.NET also supports the With { } object initializer syntax to set several property values in one concise expression immediately after construction.

🏏

Cricket analogy: Selecting an opening pair for a Test match is like calling a parameterized Sub New(strikerName As String, standIn As String) -- you must supply both openers' names the moment the pairing is created, just as Rohit Sharma and Shubman Gill are named together.

If you define any Sub New with parameters, VB.NET stops auto-generating the parameterless constructor. Add a separate Public Sub New() explicitly if you still need New ClassName() with no arguments to keep working.

Instance vs Shared Members

Members marked Shared belong to the class itself rather than to any individual instance, so every object of that class sees the same value or calls the same method without needing its own copy. Instance members, by contrast, hold and operate on data that is unique to each object, meaning two objects of the same class can carry completely different instance data at the same time.

🏏

Cricket analogy: The ICC's official match-ball specification used across every fixture is like a Shared field TotalMatchesPlayed, counted once for the whole class, while each player's own individual runsScored is an instance field unique to them.

A common mistake is declaring a field or method Shared when it actually depends on per-object state. This causes every instance to unintentionally read and write the same shared value instead of keeping its own independent copy, producing hard-to-trace bugs.

  • A Class is a blueprint; an Object is a specific instance created with the New keyword.
  • Sub performs an action with no return value; Function computes and returns a value.
  • Sub New is the constructor, invoked automatically each time New creates an instance.
  • Parameterized constructors force required data to be supplied at creation time.
  • With { } object initializer syntax sets property values right after construction.
  • Shared members belong to the class itself, not to any single instance.
  • Instance members hold and act on data unique to each individual object.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#VBNETStudyNotes#ClassesAndObjectsInVBNET#Classes#Objects#NET#Defining#OOP#StudyNotes#SkillVeris