user489041
user489041

Reputation: 28294

How do I use generics in VB.NET?

I have the following code:

Public Shared Function GetListAsString(ByVal data As List(Of String)) As String
    Dim retVal As String = "| "
    For Each obj As String In data
        retVal = retVal + obj.ToString() + " |"
    Next
    Return retVal
End Function

It converts a List to a printable String. Right now it is set up to work with just Lists. I feel as if it should be able to work with any type of Collection. I am new to Collections and generics. When I attempt to do something like

Public Shared Function GetListAsString(ByVal data As Collection(T)) As String
    Dim retVal As String = "| "
    For Each obj As String In data.ToString()
        retVal = retVal + obj.ToString() + " |"
    Next
    Return retVal
End Function

I get an error. Can anyone point me in the right direction?

Upvotes: 3

Views: 1067

Answers (5)

MarkJ
MarkJ

Reputation: 30398

Shortened version of Hans's answer, with an extension method in place of the explicit For Each loop:

Public Function GetListAsString(Of T)(ByVal data As IEnumerable(Of T)) As String
    Return data.Aggregate("| ", Function(d, s) d & s.ToString & " |")
End Function

But have you tried Snowbear's suggestion from the comments?

"| " & String.Join(" | ", data) & " |"

This String.Join code does give slightly different behaviour from the code in your question, but it's so simple. Here are the differences

empty-sequence -> "| |" rather than "| "
1, 2 -> "| 1 | 2 |" rather than "| 1| 2 |" (was that really what you meant?)

Upvotes: 1

J0HN
J0HN

Reputation: 26921

Collection(T) should be IEnumerable(Of T)?

Update Btw, Btw, as soon as you want just ToString from your collection objects, and ToString is defined in class Object and all other classes inherit Object, you can just use

Public Shared Function GetListAsString(ByVal data As IEnumerable(Of Object)) As String

Upvotes: 3

Carter Medlin
Carter Medlin

Reputation: 12465

Same solution but with less code thanks to the magic of VB.Net inference:

Public Shared Function GetListAsString(Of T)(ByVal data As IEnumerable(Of T)) As String
    Dim retVal = "| "    
    For Each obj In data
        retVal &= obj.ToString + " |"
    Next
    Return retVal
End Function

EDIT

OR even simpler using String.Join...

Public Shared Function GetListAsString(Of T)(ByVal data As IEnumerable(Of T)) As String
    Return "| " & String.Join(" |", data) & " |"
End Function

Upvotes: 2

Hans Passant
Hans Passant

Reputation: 941257

You did not get a very helpful error message from the compiler. You forgot to make the function generic, it cannot produce a decent diagnostic. Let's focus on the bigger issue:

    For Each obj As String In data.ToString()
        retVal = retVal + obj.ToString() + " |"
    Next

The For loop is broken, starting with data.ToString(). Just use data. And the element type of data is T, not String. Also, since you are only enumerating the collection, you can make do with the lesser interface, IEnumerable<>, making it work on many more collection types. Thus:

Public Shared Function GetCollectionAsString(Of T)(ByVal data As IEnumerable(Of T)) As String
    Dim retVal As String = "| "
    For Each obj As T In data
        retVal = retVal + obj.ToString() + " |"
    Next
    Return retVal
End Function

Note the added (Of T) after the function name, that's the one that solves your original error message.

Upvotes: 4

Daniel A. White
Daniel A. White

Reputation: 190907

You need to specify the generic on the method signature.

Public Shared Function GetListAsString(Of T)(ByVal data As ICollection(Of T)) As String

edit I changed it.

Upvotes: 2

Related Questions