VB.Net program to count the total created objects using a shared member

Here, we are going to learn how to count the total created objects using a shared member in VB.Net?
Submitted by Nidhi, on December 28, 2020

Here, we will create a class Sample that contains a shared member to count the created objects.

Program/Source Code:

The source code to count the total created objects using shared members is given below. The given program is compiled and executed successfully.

' program to count the total created objects 
'using a shared member.

Module Module1
    Class Sample
        Shared count As Integer
        Sub New()
            count = count + 1
        End Sub
        Sub ObjectCount()
            Console.WriteLine("Total objects: {0}", count)

        End Sub
    End Class

    Sub Main()
        Dim obj1 As New Sample()
        Dim obj2 As New Sample()
        Dim obj3 As New Sample()

    End Sub
End Module


Total objects: 3
Press any key to continue . . .


In the above program, we created a module Module1 that contains a class Sample. In the Sample class, we created a shared member count. Here, we increased the value of count in the constructor by one. As we know that shared members are common for all objects. Here, we created the method ObjectCount() to print the total number of objects created in the class.

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.