passing generic collection to a method
Hello,
I have a base collection class which implements some things.
public abstract class baseDataCollection <T>
: System.Collections.ObjectModel.KeyedCollection<int, T>,
IDisposable,
IBindingList,
IComponent
where T: baseData
BaseData is an abstract class that deals with reading/writing from the db.
I have another abstract class in a project which adds some more functionality:
public abstract class baseFeedbackLookupsCollection<T> : BaseCore.baseDataCollection<T>
where T : baseLookup
baseLookup inherits baseData.
In my project, I have several collection classes which inherit from baseFeedbackLookupsCollection<T>, and T is replaced with the appropriate type, which also inherits baseData.
I want to have a method that will accept a parameter of type baseFeedbackLookupsCollection<T> and i can pass one of the several collections that inherit this class. That way I can work with the items knowing they all inherit from baseLookup and have certain properties.
Examples.
I have several collection classes that inherit baseFeedbackLookupsCollection:
Departments,
ContactTypes,
ActionTypes
etc....
I want a method
public void SetSource(baseFeedbackLookupsCollection<t> items)
{
foreach(baselookup item in items)
{
do things with the item.
}
}
to call i would use the following:
SetSource(departments)
or
SetSource(contactTypes)
Can someone point me in the right direction?
Thanks,
Re: passing generic collection to a method
It's almost exactly as you wrote it, but if you don't want to specify the generic type of the method parameter when you declare the method then you need to make the method generic too. Also, I think T is already constrained by virtue of being a generic type parameter of your collection so you can probably just use T in the method body too:
csharp Code:
public void SetSource<T>(baseFeedbackLookupsCollection<T> items)
{
foreach(T item in items)
{
// do things with the item.
}
}
Notice how I cleverly wrapped my code snippet in tags to make it more readable? I'm surprised you haven't seen that done before in your nearly 2000 posts. ;)
Re: passing generic collection to a method
Thank You and I apologize for not enclosing the code portion of my post in tags.
I'll make sure to do so from now on.