views:

430

answers:

1

I'd like to do something like this:

Private _myCollection As IList(Of T)
Public Property MyProperty(Of T)() as IList(Of T)
    Get
        Return Me._myCollection 
    End Get
    Set(ByVal value As String)
        Me._myCollection = value
    End Set
End Property

Basically, I want to have a collection of items that may be of any type. Then, I'll be able to do something like this:

Dim myPropertyValue as <the type of some value>
if (MyProperty.Contains(<some value>))
    myPropertyValue = CType(MyProperty(<some value>), <the type of some value>)

How can I do this? Or is there a better way than using a generic type?

+5  A: 

You may have to create a generic class to do this

Public Class MyClass(Of T)
    Private _myCollection As IList(Of T)
    Public Property MyProperty() as IList(Of T)
        Get
            Return Me._myCollection 
        End Get
        Set(ByVal value As String)
            Me._myCollection = value
        End Set
    End Property
End Class
Joel Potter
+1 This is the first thought that came to my mind, I was not sure if its the best way
Mahesh Velaga
Weird. I wouldn't have thought the class had to be decorated. But I have much to learn.
IAmAN00B
A property is nothing more than get and set methods to expose a variable within a class. The variable itself is created when the class is instantiated. If you ever write Java this will become clearer since there's no properties like VB or C# have.
Joel Potter