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

Here, we are going to demonstrate the const and read-only data members in VB.Net.
Submitted by Nidhi, on December 23, 2020

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 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()
    End Sub
End Module


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


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




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.