×

VB.Net Programs

VB.Net program to implement ReadOnly property

By Nidhi Last Updated : November 15, 2024

How to implement ReadOnly property in VB.Net?

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

Program/Source Code:

The source code to implement the ReadOnly property is given below. The given program is compiled and executed successfully.

VB.Net code to implement ReadOnly property

'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(101, "Shivang Yadav")

        Console.WriteLine("Student Id  : {0}", Stu.StudentID)
        Console.WriteLine("Student Name: {0}", Stu.StudentName)
    End Sub
End Module

Output:

Student Id  : 101
Student Name: Shivang Yadav
Press any key to continue . . .

Explanation:

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

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

VB.Net Basic Programs »


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.