Home >>VB.Net Built-in Functions >VB.Net program to demonstrate the shared data member class

VB.Net program to demonstrate the shared data member class

Write a VB.Net program to demonstrate the shared data member class

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

Program :

The source code to demonstrate a class member's shared data is given below. The program given is compiled and successfully executed.

'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
Explanation:

We created a Module1 module in the program above that contains a Sample Class. We created a shared common member of the Sample class, which is initialized with 10. You may access a shared member of a class through shared or not shared methods. Here, we accessed the common value in class methods and also accessed it using the class name in the Main() method.


No Sidebar ads