Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Generics and System.Type

Last post 07-06-2008 4:05 AM by jchunn. 0 replies.
Page 1 of 1 (1 items)
Sort Posts: Previous Next
  • 07-06-2008 4:05 AM

    • jchunn
    • Not Ranked
    • Joined on 07-06-2008
    • United States
    • New Member
    • Points 5

    Generics and System.Type

    In the code below, I am trying to create a collection of "SomeClass" objects, each one being created based on the generic type chosen by the caller of "CreateSomeList", like this:

    Dim c as Collection = CreateSomeList(GetType(Integer), GetType(Single), GetType(Double), GetType(Date)) 

    The problem is that the line "Dim sc as SomeClass(Of t)" fails to compile, saying "Type 't' is not defined.".

    Here is the complete code to reproduce:

    Public Function CreateSomeList(ByVal ParamArray Types() As System.Type) As Collection

    Dim somelist As New Collection For Each t As System.Type In Types
        Dim sc As SomeClass(Of t) ' this is the line that fails to compile
        somelist.Add(sc)
    Next

    Return somelist
    End Function

    Public Class SomeClass(Of T)
    Private mValue As T
    Public Property Value() As T
        Get
            Return mValue
        End Get
        Set(ByVal value As T)
            mValue = value
        End Set
    End Property

    End Class

    How can I make this work?

    • Post Points: 5
Page 1 of 1 (1 items)