VB.NET Const and read-only member
A constant member is defined at compile time and cannot be changed at run time. A read only member is not known at compile time. Read-only member initialized at run time.
Constant and Read-Only both perform a similar function on data members but there is few difference in both. A constant member is defined at compile time and cannot be changed at run time. A read only member is not known at compile time. Read-only member initialized at run time. Constants use const keyword for declaration. Constants must be be initialized when declared but read-only does not need initialized when declared. Constant cannot be modified once initialized. Read-only member can have different values depend by code in the constructor. const keyword means the value cannot mutate. Read-only keyword means value can be mutated. Constant member assumed to be a static member.
Code
Imports System.Runtime.CompilerServices
Module Module1
Sub Main()
'pass the radius to the constructor
Dim circle As New MyClass1(5)
'Convert integer to string
Dim radius As String = Convert.ToString(circle.Radius)
Console.WriteLine("Radius = " & radius)
Console.WriteLine("Circumference = " & String.Format("{0:N3}", circle.Radius * 2 * MyClass1.Cirumference))
Console.ReadLine()
End Sub
Public Class MyClass1
'Const member declared and initialized
'Const must be initialized when declared
Public Const Cirumference As Double = 2.85415
'read-only member declare
Public ReadOnly Radius As Integer
Public Sub New(Value As Integer)
Radius = Value
End Sub
End Class
End Module
Output
