Shared field : Member Fields « Class Module « VB.Net Tutorial






Public Class Tester
    Public Shared Sub Main
        Dim Counter1 As New Counter
        Dim Counter2 As New Counter
        Console.WriteLine("Total count = " & Counter1.Count())
        Console.WriteLine("Total count = " & Counter2.Count())

        Console.WriteLine("Total count = " & Counter1.Count())
        Console.WriteLine("Total count = " & Counter2.Count())

        Console.WriteLine("Total count = " & Counter1.Count())
        Console.WriteLine("Total count = " & Counter2.Count())

        Console.WriteLine("Total count = " & Counter1.Count())
        Console.WriteLine("Total count = " & Counter2.Count())
    End Sub
End Class

Public Class Counter
    Shared Data As Integer = 0

    Public Function Count() As Integer
        Data += 1
        Return Data
    End Function
End Class
Total count = 1
Total count = 2
Total count = 3
Total count = 4
Total count = 5
Total count = 6
Total count = 7
Total count = 8








6.12.Member Fields
6.12.1.Shadow member variable in base class
6.12.2.Shadow fields
6.12.3.Shared field
6.12.4.Field initialization
6.12.5.Public Field