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.
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()) ' 750Constructors 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
1. What keyword is used to create a new instance of a class in VB.NET?
2. Which procedure type is used for a class member that performs an action but does not return a value?
3. What is the name of the special constructor procedure in a VB.NET class?
4. A member marked Shared belongs to:
5. What syntax lets you set property values immediately after New without writing a custom constructor?
Was this page helpful?
You May Also Like
Inheritance and Interfaces
Explore how VB.NET classes reuse and extend behavior through Inherits, Overridable/Overrides, and how Interfaces define implementation-free contracts.
Properties and Encapsulation
Understand how VB.NET Property blocks and Private fields work together to enforce encapsulation, validation, and controlled access to object state.
Overloading and Overriding
Distinguish compile-time method overloading from run-time method overriding in VB.NET, plus MustOverride and operator overloading.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics