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

Namespace StdGen
    Public Class Program
        ' **** demo classes ****
        Public Class P
            Public Sub MP()
            End Sub
        End Class
        Public Class C
            Inherits P
            Public Sub MC()
            End Sub
        End Class
        Public Class Container(Of T)
            Private o As T
            Public Sub Put(o As T)
                Me.o = o
            End Sub
            Public Function [Get]() As T
                Return o
            End Function
        End Class
        ' **** standard generic methods ****
        Public Shared Sub Test(Of T)(co As Container(Of T), o As T)
            'co.Put(new P()); // no required relation between T and P
            'co.Put(new C()); // no required relation between T and C
            co.Put(o)
            'P pcopy = co.Get(); // no required relation between T and P
            'pcopy.MP();
            'pcopy.MC();
            'C ccopy = co.Get(); // no required relation between T and C
            'ccopy.MP();
            'ccopy.MC();
            Dim tcopy As T = co.[Get]()
            'tcopy.MP();
            'tcopy.MC();
        End Sub
        Public Shared Sub TestP(Of T As P)(co As Container(Of T), o As T)
            'co.Put(new P()); // problem if T is C or a subclass of C
            'co.Put(new C()); // problem if T is a subclass of C
            co.Put(o)
            Dim pcopy As P = co.[Get]()
            pcopy.MP()
            'pcopy.MC();
            'C ccopy = co.Get(); // problem if T is P
            'ccopy.MP();
            'ccopy.MC();
            Dim tcopy As T = co.[Get]()
            tcopy.MP()
            'tcopy.MC();
        End Sub
        Public Shared Sub TestC(Of T As C)(co As Container(Of T), o As T)
            'co.Put(new P()); // problem if T is C or a subclass of C
            'co.Put(new C()); // problem if T is a subclass of C
            co.Put(o)
            Dim pcopy As P = co.[Get]()
            pcopy.MP()
            'pcopy.MC();
            Dim ccopy As C = co.[Get]()
            ccopy.MP()
            ccopy.MC()
            Dim tcopy As T = co.[Get]()
            tcopy.MP()
            tcopy.MC()
        End Sub
        ' ****
        Public Shared Sub Main(args As String())
            Test(New Container(Of P)(), New P())
            Test(New Container(Of C)(), New C())
            TestP(New Container(Of P)(), New P())
            TestP(New Container(Of C)(), New C())
            'TestC(new Container<P>(), new P()); // P does not match "T : C"
            TestC(New Container(Of C)(), New C())
        End Sub
    End Class
End Namespace
