Static class constructor in VB

Read documentation here. In you can do

Shared Sub New()
...
End Sub

And it will be invoked. From MSDN:

  1. Shared constructors are run before any instance of a class type is created.

  2. Shared constructors are run before any instance members of a structure type are accessed, or before any constructor of a structure type is explicitly called. Calling the implicit parameter less constructor created for structures will not cause the shared constructor to run.

  3. Shared constructors are run before any of the type's shared members are referenced.

  4. Shared constructors are run before any types that derive from the type are loaded.

  5. A shared constructor will not be run more than once during a single execution of a program.


Kind of looks like a normal constructor in VB.NET:

Shared Sub New()

End Sub

Have you tried:

Class someClass

    Public Shared somePublicMember As String

    Shared Sub New()
        messageBox.show("I just constructed a static class")
    End Sub
End Class

Tags:

C#

Vb.Net