Results 1 to 3 of 3

Thread: passing generic collection to a method

  1. #1

    Thread Starter
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950

    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,
    Don't anthropomorphize computers -- they hate it

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. public void SetSource<T>(baseFeedbackLookupsCollection<T> items)
    2. {
    3.     foreach(T item in items)
    4.     {
    5.         // do things with the item.
    6.     }
    7. }
    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.
    Last edited by jmcilhinney; Nov 12th, 2009 at 09:45 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950

    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.
    Don't anthropomorphize computers -- they hate it

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width