ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

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

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 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 »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

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

© https://www.includehelp.com some rights reserved.