How to enforce immutability to prevent the modification of values in VB.NET

6 Answers

0 votes
' Immutable variable using Const

Imports System

Module ImmutableConstVar

    ' Const must be declared at module/class level, not inside a method
    Const X As Integer = 10   ' immutable value

    Sub Main()
        Console.WriteLine("X = " & X)

        ' X = 20   ' ERROR: Constant expression is not a variable 
		           ' Constant cannot be the target of an assignment.
    End Sub

End Module


'
' run:
'
' x = 10
'

 



answered 14 hours ago by avibootz
0 votes
' Immutable fields using ReadOnly

Imports System

Public Class User
    Public ReadOnly Id As Integer        ' immutable field
    Public ReadOnly Name As String       ' immutable field

    Public Sub New(id As Integer, name As String)
        Me.Id = id            ' allowed only here
        Me.Name = name
    End Sub
End Class

Module Program
    Sub Main()
        Dim u As New User(42, "Mary")

        Console.WriteLine(u.Id)
        Console.WriteLine(u.Name)

        ' u.Id = 100    ' ERROR: ReadOnly field cannot be assigned
					    'ReadOnly' variable cannot be the target of an assignment.
		' u.Name = "Tim" ' ERROR: ReadOnly field cannot be assigned
		                 'ReadOnly' variable cannot be the target of an assignment.
    End Sub
End Module


'
' run:
'
' 42
' Mary
'

 



answered 13 hours ago by avibootz
0 votes
' Immutable properties (no setter)

Imports System

' Immutable User class
Public Class User
    ' These fields can only be assigned inside the constructor
    Private ReadOnly _id As Integer
    Private ReadOnly _name As String

    ' Read-only property exposes the ID but cannot be changed
    Public ReadOnly Property Id As Integer
        Get
            Return _id
        End Get
    End Property

    ' Read-only property exposes the Name but cannot be changed
    Public ReadOnly Property Name As String
        Get
            Return _name
        End Get
    End Property

    ' Constructor initializes the immutable fields
    Public Sub New(id As Integer, name As String)
        _id = id
        _name = name
    End Sub
End Class


' Main program demonstrating immutability
Module Program
    Sub Main()
        ' Create an immutable User object
        Dim u As New User(42, "Harper")

        Console.WriteLine("ID: " & u.Id)
        Console.WriteLine("Name: " & u.Name)

        ' The following lines would cause compile-time errors:
        ' u.Id = 100        ' ERROR: Property is ReadOnly
		' u.Name = "Tom"    ' ERROR: Property is ReadOnly

        ' The internal fields are also protected:
        ' u._id = 100       ' ERROR: _id is Private
		' u._name = "Tom"   ' ERROR: _name is Private
    End Sub
End Module


'
' run:
'
' ID: 42
' Name: Harper
'

 



answered 13 hours ago by avibootz
0 votes
' Immutable objects using Structure + ReadOnly

Imports System

Public Structure Point2D
    Public ReadOnly X As Integer
    Public ReadOnly Y As Integer

    Public Sub New(x As Integer, y As Integer)
        Me.X = x
        Me.Y = y
    End Sub
End Structure

Module Program
    Sub Main()
        Dim p As New Point2D(5, 7)

        Console.WriteLine(p.X & ", " & p.Y)

        ' p.X = 10   ' ERROR: ReadOnly field
    End Sub
End Module


'
' run:
'
' 5, 7
'

 



answered 13 hours ago by avibootz
0 votes
' Prevent modification of parameters (ByVal)

Imports System

Module Program

    ' This procedure receives x **ByVal**,
    ' meaning it gets a **copy** of the caller's value.
    ' Any modification inside this procedure does NOT affect the caller.
    Sub PrintValue(ByVal x As Integer)
        Console.WriteLine("Inside PrintValue, x = " & x)

        ' This modifies ONLY the local copy.
        x = 20   ' allowed (local copy), caller is not affected

        Console.WriteLine("Inside PrintValue after change, x = " & x)
    End Sub

    Sub Main()
        Dim original As Integer = 10

        Console.WriteLine("Before calling PrintValue, original = " & original)

        ' Pass original ByVal → PrintValue receives a copy
        PrintValue(original)

        ' original remains unchanged
        Console.WriteLine("After calling PrintValue, original = " & original)
    End Sub

End Module


'
' run:
'
' Before calling PrintValue, original = 10
' Inside PrintValue, x = 10
' Inside PrintValue after change, x = 20
' After calling PrintValue, original = 10
'

 



answered 13 hours ago by avibootz
0 votes
' Immutable class pattern 

Imports System

' Immutable User class
Public Class User
    ' ReadOnly auto-properties: can only be assigned in the constructor
    Public ReadOnly Property Id As Integer
    Public ReadOnly Property Name As String

    ' Constructor initializes the immutable properties
    Public Sub New(id As Integer, name As String)
        Me.Id = id
        Me.Name = name
    End Sub
End Class


' Main program demonstrating immutability
Module Program

    Sub Main()
        ' Create an immutable User object
        Dim u As New User(42, "Eithan")

        Console.WriteLine("ID: " & u.Id)
        Console.WriteLine("Name: " & u.Name)

        ' The following lines would cause compile-time errors:
        ' u.Id = 100        ' ERROR: Property is ReadOnly
        ' u.Name = "Bob"    ' ERROR: Property is ReadOnly
    End Sub

End Module


'
' run:
'
' ID: 42
' Name: Eithan
'

 



answered 13 hours ago by avibootz
...