' Collection of code snippets by Arne Vajhøj 
' (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
Imports System
Imports System.Collections

Public Class S1
    Private list As ArrayList
    Private Shared instance As S1 = Nothing

    Private Sub New()
        list = New ArrayList 
    End Sub

    Public Shared Function GetInstance() As S1
        If instance Is Nothing Then
            instance = New S1 
        End If
        Return instance
    End Function

    Public Sub Add(ByVal o As Object)
        list.Add(o)
    End Sub

    Public Function GetList() As ArrayList
        Return list
    End Function
End Class 

Public Class S2
    Private list As ArrayList
    Private Shared instance As S2 = New S2 

    Private Sub New()
        list = New ArrayList 
    End Sub

    Public Shared Function GetInstance() As S2
        Return instance
    End Function

    Public Sub Add(ByVal o As Object)
        list.Add(o)
    End Sub

    Public Function GetList() As ArrayList
        Return list
    End Function
End Class 

Public Class S3
    Private list As ArrayList
    Private Shared _instance As S3 = Nothing

    Private Sub New()
        list = New ArrayList 
    End Sub

    Public Shared ReadOnly Property Instance() As S3
        Get
            If _instance Is Nothing Then
                _instance = New S3 
            End If
            Return _instance
        End Get
    End Property

    Public Sub Add(ByVal o As Object)
        list.Add(o)
    End Sub

    Public Function GetList() As ArrayList
        Return list
    End Function
End Class 

Public Class S4
    Private list As ArrayList
    Private Shared _instance As S4 = Nothing

    Private Sub New()
        list = New ArrayList 
    End Sub

    Private Shared mylock As Object = New Object 
    Public Shared ReadOnly Property Instance() As S4
        Get
            SyncLock mylock
                If _instance Is Nothing Then
                    _instance = New S4 
                End If
            End SyncLock
            Return _instance
        End Get
    End Property

    Public Sub Add(ByVal o As Object)
        list.Add(o)
    End Sub

    Public Function GetList() As ArrayList
        Return list
    End Function
End Class 

Public Class S5
    Private list As ArrayList
    Private Shared _instance As S5 = Nothing

    Private Sub New()
        list = New ArrayList 
    End Sub

    Private Shared mylock As Object = New Object 
    Public Shared ReadOnly Property Instance() As S5
        Get
            If _instance Is Nothing Then
                SyncLock mylock
                    If _instance Is Nothing Then
                        _instance = New S5 
                    End If
                End SyncLock
            End If
            Return instance
        End Get
    End Property

    Public Sub Add(ByVal o As Object)
        list.Add(o)
    End Sub

    Public Function GetList() As ArrayList
        Return list
    End Function
End Class 

Class MainClass
    Public Shared Sub Main(ByVal args As String())
        Dim a As S3 = S3.Instance
        a.Add("A")
        Dim b As S3 = S3.Instance
        b.Add("B")
        Dim c As S3 = S3.Instance
        Dim list As ArrayList = c.GetList
        Dim i As Integer
        For i = 0 To (list.Count - 1)
            Console.WriteLine(CType(list(i), String))
        Next
    End Sub
End Class 
