Yugal Jindle
Yugal Jindle

Reputation: 45656

How to make this simple function generic in VB.NET?

Aim to Achieve :

I want the Function to accept List(Of String), Array & String and similarly return List(Of String), Array & String respectively. The Function simply adds a string (month) to the input collection. I just want to use it for string, array and list with needing to think of conversions.

I have a simple function :

Private Function addMonth(ByVal stringList As List(Of String), ByVal month As String) As List(Of String)
    Dim names As New List(Of String)
    For Each name In stringList
        names.Add(name + " (" + month + ")")
    Next name
    Return names
End Function

How can I use generic type 'T' to achieve my aim ?

It would really save me a lot of tension.. !

I am new to VB.Net.. and don't know much about generic functions !

Upvotes: 0

Views: 1291

Answers (2)

Dan Tao
Dan Tao

Reputation: 128327

Update: If what you want is a function that can take either a String or a String() array, you can accomplish that with the ParamArray keyword. But it needs to be the last argument passed to the function; so you'd just need to reverse the order of the arguments:

Private Function AddMonth(ByVal month As String, ByVal ParamArray list() As String) As List(Of String)
    ' Same code as below
End Function

This would allow you to call it like this:

Dim withMonths = AddMonth("March", "Report")

And also like this:

Dim withMonths = AddMonth("March", "Report", "Memo")

And also like this:

Dim list = New String() { "Report", "Memo" }
Dim withMonths = AddMonth("March", list)

Note that while this appears to achieve what you're after, it's actually a bit less flexible as it requires you to pass in parameters either in the form above (each individually) or as an actual String() array, rather than just any IEnumerable(Of String) such as List(Of String).


Original answer:

Private Function AddMonth(Of T)(ByVal list As IEnumerable(Of T), ByVal month As String) As List(Of String)
    Dim names As New List(Of String)
    For Each name In list
        ' Using String.Concat avoids the possibility
        ' of a NullReferenceException
        names.Add(String.Concat(name) + " (" + month + ")")
    Next name
    Return names
End Function

Upvotes: 1

Joel Coehoorn
Joel Coehoorn

Reputation: 415735

You don't even need to write a new function for this. Just use .Select():

myStringList.Select(Function(s) s & " (" & month & ")")

But since you also want to accept a string input, you can overload the function like this:

Private Function AddMonth(ByVal list As IEnumerable(Of String), ByVal month As String) As IEnumerable(Of String)
    return list.Select(Function(s) s & " (" & month & ")")
End Function

Private Function AddMonth(ByVal list As String, ByVal month As String) As IEnumerable(Of String)
   Return New String() {list & " (" & month & ")"}
End Function

Note that all of these return IEnumerable(Of String), and this is just as it should be. It's easy to convert this to a string array or string list later, but most of the time you don't want to. It's much better for performance to keep treating an object as an IEnumerable for as long as possible.

Upvotes: 3

Related Questions