VB.Net program to demonstrate the shared data member of the class

Here, we are going to demonstrate the shared data member of the class in VB.Net.
Submitted by Nidhi, on December 28, 2020 [Last updated : March 06, 2023]

Shared data member of the class in VB.Net

Here, we will create a class Sample that contains a shared member that will access the class method as well as outside the class without creating the object.

Program/Source Code:

The source code to demonstrate the shared data member of the class is given below. The given program is compiled and executed successfully.

VB.Net code to demonstrate the example of shared data member of the class

'VB.net program to demonstrate the 
'Shared data member of the class.

Module Module1
    Class Sample
        Public Shared common As Integer = 10
        Sub Fun1()
            Console.WriteLine("Val : {0}", common)
        End Sub
        Sub Fun2()
            Console.WriteLine("Val : {0}", common)
        End Sub
    End Class

    Sub Main()
        Dim obj As New Sample()
        obj.Fun1()
        obj.Fun2()

        Console.WriteLine("Val : {0}", Sample.common)
    End Sub
End Module

Output:

Val : 10
Val : 10
Val : 10
Press any key to continue . . .

Explanation:

In the above program, we created a module Module1 that contains a class Sample. In the Sample class, we created a shared member common, which is initialized with 10. The shared member of the class can be accessed by shared or not shared methods. Here, we accessed the value of common in class methods and also accessed in Main() method using the class name.

VB.Net Basic Programs »





Comments and Discussions!

Load comments ↻





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