0

In Visual Basic.net, If I have a List of T, where the Type is an object that always has a .Name property, how I can write a function that returns the .Name value?

Here is my code:

Public Function GetListOfListItemNames(Of T)(ListOfItems As List(Of T)) As List(Of String)
    Dim ListToReturn As New List(Of String)
    For x = 0 To ListOfItems.Count - 1
        ListToReturn.Add(ListOfItems.Item(x).Name)
    Next
    Return ListToReturn
End Function

I am getting the following error:

Name' is not a member of 'T'

Simon
  • 351
  • 3
  • 8
  • 16

1 Answers1

0

You need to specify a constraint on your generic method. Specifically, your GetListOfListItemNames(Of T) should be defined as GetListOfListItemNames(Of T As BaseType) where BaseType is the type that you expect to be contained in the list.

I'm not familiar with VB.Net to any great extent, so my syntax may be incorrect. But the need for a generic constraint is not:

See: http://msdn.microsoft.com/en-us/library/w256ka79.aspx for specifics.

William
  • 1,867
  • 11
  • 10