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

Properties and Encapsulation

Understand how VB.NET Property blocks and Private fields work together to enforce encapsulation, validation, and controlled access to object state.

OOP in VB.NETIntermediate8 min readJul 10, 2026
Analogies

Why Encapsulation Matters

Encapsulation means keeping a class's internal data Private and exposing controlled access through Public members, so external code can never directly corrupt an object's state. In VB.NET, this is achieved by declaring a backing field Private and wrapping it in a Property block with Get and Set accessors, letting the class enforce rules every time the value is read or written.

🏏

Cricket analogy: A team's dressing-room strategy discussions stay private to players and coaching staff, while only the agreed batting order is exposed publicly on the scoreboard -- mirroring how a Private field stays hidden while a Public Property controls what's revealed.

Property Get and Set Accessors

A full Property declaration uses Property Name As Type followed by a Get block that returns the backing field's value and a Set(value As Type) block that assigns it, letting you insert validation or transformation logic exactly at the point of read or write. This is more verbose than an auto-property but necessary whenever the setter must reject invalid input or the getter must compute a derived value.

🏏

Cricket analogy: A stadium's turnstile that checks ticket validity every time a fan enters, the Set, and displays remaining capacity every time someone checks, the Get, mirrors how a Property's Get and Set blocks run logic on every single access, not just once.

vbnet
Public Class Temperature
    Private celsiusValue As Double

    Public Property Celsius As Double
        Get
            Return celsiusValue
        End Get
        Set(value As Double)
            If value < -273.15 Then
                Throw New ArgumentException("Temperature cannot be below absolute zero.")
            End If
            celsiusValue = value
        End Set
    End Property
End Class

Dim t As New Temperature()
t.Celsius = 25.0
Console.WriteLine(t.Celsius)  ' 25

Auto-Implemented Properties

When no custom validation is needed, VB.NET's auto-implemented property syntax -- Public Property Name As Type -- lets the compiler generate the private backing field and default Get/Set logic automatically, dramatically reducing boilerplate for simple data-holding classes.

🏏

Cricket analogy: A simple club-level scoreboard that just displays whatever number is entered, with no validation, mirrors an auto-implemented property -- no custom logic, just straightforward storage, unlike an international match's rigorous DRS-backed accessors.

Auto-implemented properties can still specify a default value inline, for example Public Property Age As Integer = 0, giving the backing field a starting value without writing a constructor.

Read-Only, Write-Only, and Validation Logic

Marking a property ReadOnly means it exposes only a Get accessor -- typically used for computed or constructor-assigned values that should never change after creation -- while WriteOnly exposes only Set, useful for things like a password field that should be settable but never readable back out. Validation logic placed inside a Set block, such as throwing an ArgumentException for an out-of-range value, is the primary mechanism for keeping an object in a consistent, valid state.

🏏

Cricket analogy: A player's DateOfBirth field, fixed once entered into ICC records and never editable afterward, mirrors a ReadOnly property -- only a Get accessor exists, just as birth year can't change after a player's international debut.

Skipping validation inside a Set block is a common source of bugs -- always throw a descriptive exception like ArgumentException for out-of-range or malformed values rather than silently accepting bad data that leaves the object in an inconsistent state.

  • Encapsulation keeps fields Private and exposes controlled access through Public properties.
  • A full Property uses Get to return a value and Set(value As T) to assign it, allowing validation.
  • Auto-implemented properties let the compiler generate a backing field automatically for simple cases.
  • ReadOnly properties expose only Get, typically for values fixed at construction.
  • WriteOnly properties expose only Set, useful for values like passwords that should never be read back.
  • Validation logic inside Set is the primary way to keep an object's state consistent and valid.
  • Get accessors can return computed or derived values rather than a raw stored field.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#VBNETStudyNotes#PropertiesAndEncapsulation#Properties#Encapsulation#Matters#Property#OOP#StudyNotes#SkillVeris