Guida programmazione orientata agli oggetti » Torna alla lezione

Esempio di singleton [VB .NET]

Imports system
Class Singleton
    Public Shared istanza As Singleton
    Private Sub new()
    End Sub
     Public Shared Property Instance as Singleton
        Get
            If (istanza = Nothing) Then
                istanza = new Singleton()
            End If
            return istanza
        End Get
    End Property
    Public Sub helloWorld()
        Console.WriteLine("Hello World")
    End Sub
End Class
Module UsaSingleton
    Sub Main()
        Singleton.Instance.helloWorld()
    End Sub
End Module