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

VB.Net Program to Implement WriteOnly Property

Write a VB.Net Program to Implement WriteOnly Property

Here, using the WriteOnly keyword, we can create a class and implement the Set properties to set the values for the data members.

Program :

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

'VB.net program to implement WriteOnly property


Class Student
    Dim Id As Integer
    Dim Name As String
    Public WriteOnly Property StudentID As Integer
        Set(value As Integer)
            Id = value
        End Set
    End Property
    Public WriteOnly Property StudentName As String
        Set(value As String)
            Name = value
        End Set
    End Property
    Sub PrintInfo()
        Console.WriteLine("Student Id  : {0}", Id)
        Console.WriteLine("Student Name: {0}", Name)
    End Sub
End Class
Module Module1
    Sub Main()
        Dim Stu As New Student()
        Stu.StudentID = 110
        Stu.StudentName = "Mohit"
        Stu.PrintInfo()
    End Sub
End Module
	
Output:
Student Id : 110
Student Name: Mohit
Explanation:

We built a Student class in the above program that contains two Id members of the data, Name. Here, we have implemented StudentId and StudentName properties to set data member values, and have implemented a PrintInfo() method to print data member values on the console screen.

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 data members using Properties, and then print the student details on the console screen using the PrintInfo() method.