Home >>VB.Net Built-in Functions >VB.Net program to count total created objects using shared member

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

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

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

Program :

Below is the source code for counting the total created objects using shared members. The program given is compiled and successfully executed.

'VB.net 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()
        obj3.ObjectCount()
    End Sub
End Module
	
Output:
Total objects: 3
Explanation:

We created a Module1 module in the program above that contains a Sample Class. We created a shared member count in the Sample class. Here, we've increased the value of the constructor's count by one. As we understand, for all objects, shared members are common. We built the ObjectCount() method here to print the total number of objects that were created in the class.


No Sidebar ads