views:

432

answers:

3

I have a utility class that takes a generic list as a parameter.

Code looks like:

Function DoStuff(collection as Object, elt as Object)
   ...
   collection.Add(elt)
   ...
End Function

This is called with:

DoStuff( List(Of Foo), new Foo() )
DoStuff( List(Of Bar), new Bar() )

There are about a dozen different types.

Currently, passing as Object results in a Late bound resolution warning, although it runs fine.

I've tried different ways to pass in collection and elt (Foo and Bar both extend a base class) but can't seem to figure out the "proper" way to do it.

Ideas?

+3  A: 

I think you're looking for something like this.

Public Sub DoStuff(Of T)(collection As List(Of T), elt As T)
    ...
    collection.Add(elt)
    ...
End Function
womp
But what is T in this case? The return value isn't relevant - in fact, it's actually a sub.
chris
T is the type of the variable you pass to the method. So if you pass an integer, T will be an integer and if you pass a List(Of Whatever) T will be such list
Qua
Generics is built for exactly what you're trying to do, which is reuse the same function for a large number of different types. Whenever you call DoStuff(Of Integer), then all the "T" arguments become Integer types. If you call DoStuff(Of MyCrazyClass), then all the T arguments are of type MyCrazyClass. Behind the scenes, the compiler makes one function for each DoStuff(Of xxx) that you have in your code - but you only had to write it once.
womp
But the problem is that I have 12 different types. I want to avoid having 12 different methods.This sub is never re-used outside the app, so I'm not worried about supporting ICollection as you mention below.
chris
Re-read my previous comment. The code above solves your problem. You write that one method, and use it with all your 12 types.
womp
What's the downvote for?
womp
I think I pissed someone off...a bunch of questions I've posted were all downvoted in the last day or two.
chris
+1  A: 

You should always strive to built/use strongly typed code. Imagine what would happen if an integer was passed as the collection object - You would have to manually check for the type of the variable. Even worse though, the user of your method would have to look up in the documentation what kind of object your method required as it's first parameter.

You should go by the approach suggested by womp, or even better you should make your parameter require an object that extends the generic collection interface, ICollection(Of T):

Public Function DoStuff(Of T)(collection As ICollection(Of T), elt As T) As stuff
...
collection.Add(elt)
...
End Function

since this would allow the user of the method to pass not only lists, but also all other classes that inheritage the ICollection(Of T) interface (synchronizedCollection, HashSet, LinkedList, or custom classes ect.) which is really the power of OO programming.

Qua
+1  A: 

The answers by womp and Qua are I think the correct ones, however if your 12 types all inherit from some common base class (as your original question suggests) you can write the method such that it works with your types, and only your types as such:

Public Sub DoStuff(Of T As YourBaseClass)(collection As List(Of T), elt As T)
    ...
    collection.Add(elt)
    ...
End Sub

This way the method works only for types which inherit from YourBaseClass.

chyne