Home >>VB.Net Built-in Functions >VB.Net Program to Implement ReadOnly Property

VB.Net Program to Implement ReadOnly Property

Write a VB.Net Program to Implement ReadOnly Property

Here, to get the values of data members, we can create a class and implement Get properties using the ReadOnly keyword.

Program :

Below is the source code for implementing the ReadOnly property. The program given is compiled and successfully executed.

'VB.net program to implement ReadOnly property.


Class Student
    Dim Id As Integer
    Dim Name As String
    Sub New(ByVal i As Integer, ByVal n As String)
        Id = i
        Name = n
    End Sub
    Public ReadOnly Property StudentID As Integer
        Get
            Return Id
        End Get
    End Property

    Public ReadOnly Property StudentName As String
        Get
            Return Name
        End Get
    End Property
End Class
Module Module1
    Sub Main()
        Dim Stu As New Student(110, "Narang")
        Console.WriteLine("Student Id  : {0}", Stu.StudentID)
        Console.WriteLine("Student Name: {0}", Stu.StudentName)
    End Sub
End Module
	
Output:
Student Id : 110
Student Name: Narang
Explanation:

We built a Student class in the above program that contains two Id members of the data, Name. Here, we introduced StudentId and StudentName properties to get data member values, and implemented a constructor to set data member values.

After that, we built a Module1 module that contains the Main() method, and the application entry point is the Main() method. And, we created a Student Class object and then set the values of the members of the data using the constructor and then print the student details on the console screen using the properties.


No Sidebar ads