×

VB.Net Programs

VB.Net program to demonstrate the const and read-only data members

By Nidhi Last Updated : November 11, 2024

Const and read-only data members in VB.Net

Here, we will create a Sample class that contains constant, read-only data members. The read-only member can only be initialized inside the constructor.

Program/Source Code:

The source code to demonstrate the const and read-only data members is given below. The given program is compiled and executed successfully.

VB.Net code to demonstrate the example of const and read-only data members

'VB.Net program to demonstrate the const 
'and read-only data members.

Class Sample
    Private Const val1 As Integer = 100
    Private ReadOnly val2 As Integer

    Sub New()
        val2 = 200
    End Sub

    Sub Print()
        Console.WriteLine("Val1 : {0}", val1)
        Console.WriteLine("Val2 : {0}", val2)
    End Sub
End Class

Module Module1
    Sub Main()
        Dim S As New Sample()
        S.Print()
    End Sub
End Module

Output:

Val1 : 100
Val2 : 200
Press any key to continue . . .

Explanation:

In the above program, we created a module Module1 that contains the Main() function. Here, we created a Sample class that contains a const member val1 and a read-only member val2. And, we initialized the read-only member inside the constructor. We create a member function Print() that will print the values of data members on the console screen.

VB.Net Basic Programs »


Comments and Discussions!

Load comments ↻





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