Home »
VB.Net »
VB.Net Programs
VB.Net program to count the total created objects using a shared member
By Nidhi Last Updated : October 13, 2024
Counting the total created objects using a shared member in VB.Net
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.
VB.Net code to count the total created objects using a shared member
'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
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 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 »