Visual Basic ReadOnly Property

In visual basic, ReadOnly is a keyword and it is useful to define read-only fields in our applications.  The read-only field values need to be initialized either at the declaration or in the constructor of the same class unlike constant keyword in vb. If we use ReadOnly keyword with fields, those field values will be evaluated at the run time.

 

To define read-only fields in visual basic, we need to use ReadOnly keyword during the declaration of fields in our application and we can use ReadOnly modifier with the numbers, boolean values, strings or with null references.

 

In visual basic, if we use ReadOnly keyword to define the read-only field, that field value cannot be changed once the constructor execution has finished so we should not use ReadOnly keyword with the fields whose value will be changed at any time.

 

It’s mandatory to initialize the read-only field values either at the declaration or in a constructor otherwise we will get a compile-time error in our visual basic application.

Visual Basic Readonly Keyword Syntax

Following is the syntax of defining the read-only fields using ReadOnly keyword in a visual basic programming language.

 

ReadOnly field_name As data_type = "value"

If you observe the above syntax, we used ReadOnly keyword to declare the read-only variable in our application.

 

The following are the different ways of declaring and initializing the read-only fields in a visual basic programming language.

 

Class User

    ' Initialize Read-Only Fields

    Public ReadOnly name As String = "Suresh Dasari"

    Public ReadOnly location As String

    Public ReadOnly age As Integer

    Public Sub New()

        location = "Hyderabad"

        age = 32

    End Sub

    Public Sub SetDetails()

        ' Compile error if uncommented

        ' location = "Guntur";

        ' age = 30;

    End Sub

End Class

If you observe the above example, we created read-only fields with different data types and initializing the field values during the declaration and in a constructor.

 

In case, if we uncomment the commented code in SetDetails() method, we will get a compile error because, in visual basic, the read-only field values can be initialized either during declaration or in a constructor.

Visual Basic Readonly Property Example

Following is the example of defining and using the read-only fields in a visual basic programming language with ReadOnly keyword.

 

Module Module1

    Class User

        ' Initialize Read-Only Fields

        Public ReadOnly name As String = "Suresh Dasari"

        Public ReadOnly location As String

        Public ReadOnly age As Integer

        Public Sub New()

            location = "Hyderabad"

            age = 32

        End Sub

    End Class

    Sub Main()

        Dim u As User = New User()

        ' This will throw compile time error

        ' u.name = "Rohini Alavala";

        Console.WriteLine("Name: {0}", u.name)

        Console.WriteLine("Location: {0}", u.location)

        Console.WriteLine("Age: {0}", u.age)

        Console.WriteLine("Press Enter Key to Exit..")

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we created read only fields with different data types and if we uncomment the commented code, we will get a compile-time error because we are trying to change the value of read only fields.

 

As discussed, once the read-only field is declared and initialized, that field value must be the same throughout the application.

 

When we execute the above visual basic program, we will get the result as shown below.

 

Visual Basic Readonly Property Example Result

Visual Basic ReadOnly Property Features

The following are the important features of the read-only variables in a visual basic programming language.

 

  • Read-only fields in visual basic can be created by using ReadOnly keyword.
  • In visual basic, the read-only fields can be initialized either at the declaration or in a constructor.
  • The read-only field values will be evaluated during the run time in visual basic.
  • Once values assigned to the read-only fields, those values must be the same throughout the application.

Visual Basic Constant vs ReadOnly

The following are the differences between constant and read-only properties in a visual basic programming language.

 

  • In visual basic, the constant fields can be created by using Const keyword and the read-only fields can be created by using ReadOnly keyword.
  • In visual basic, the constant fields can only be initialized during the time of declaration but the read-only fields can be initialized either at the declaration or in a constructor.
  • Constant field values will be evaluated during the compile-time but the values of the read-only field will be evaluated at run time in visual basic.

This is how we can create and use read-only fields in a visual basic programming language with ReadOnly keyword based on our requirements.