A Simple Example
The following example uses our class above. Notice for the static member, we do not use a variable, instead we use the class name directly. That's why static members are sometimes referred to as class members. Static members belong to the class and are referenced by the class name.
Note: The public member fields CyborgName and CyborgAge are used here for demonstration only. You normally want to make them private and access them via a member property.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Read static member field BEFORE we create our object.
'Notice the use of the class name, not an object name.
MessageBox.Show("We will now build a series " & Cyborg.SeriesID & " robot.")
Dim MyRobot As New Cyborg
MyRobot.CyborgName = "John"
MyRobot.CyborgAge = 34
MessageBox.Show("We created a " & MyRobot.CyborgAge & " year old robot named " & MyRobot.CyborgName & ".")
'You should not refer to static members using an instance.
'You will get a compiler warning (in C# you get a compiler error).
'MessageBox.Show("A series " & MyRobot.SeriesID & " robot.")
'Use a type name instead.
MessageBox.Show("A series " & Cyborg.SeriesID & " robot.")
End Sub
End Class
Public Class Cyborg
Private FSerialNumber As String = "A100"
Public CyborgName As String
Public CyborgAge As Integer
Public Shared ReadOnly SeriesID As Integer = 100
End Class