generic method to handle collection

Last post 11-06-2009 9:15 AM by atconway. 2 replies.

Sort Posts:

  • generic method to handle collection

    11-02-2009, 10:36 PM

    Requirement:

    I need to create a method to which user will pass:

    1) a collection (Can be ArrayList or HashTable or DataTable or Generic List or Array)
    2) Items to be removed from this collection
    3) [Optional] PropertyName or ColumnName representing the value in 2) above. I think it is required for custom objects and data tables only.

    In order to resolve this I am following approach mentioned below. Is it a good approach or is there a better way to do this?


    Pseudocode

    1) Create a function taking above inputs (Object, string, string)
    2) Create overloaded 3 functions with same parameters as above but having collection argument as IList (to handle arrays etc.), IDictionary (to handle HashTables) and ComoponentData.IListSource (to handle datatables)
    3) From method in 1) invoke the method in 2) and automatically the right overloaded method will get called


    Code - Approach Followed

    A similar example having a method to which any type of collection is passed and it returns count of collection.

    'Method called with parameter as Object
    Public Function GetCount(ByVal x As Object)
            If (GetType(IList)).IsAssignableFrom(x.GetType) Then
                Return GetCount1(CType(x, IList))
            ElseIf (GetType(IDictionary)).IsAssignableFrom(x.GetType) Then
                Return GetCount1(CType(x, IDictionary))
            Else
                Return GetCount1(CType(x, ComponentModel.IListSource))
            End If
        End Function
    
    
    'Three implementations of count
        Public Function GetCount1(ByVal list As IList) As Integer
            Return list.Count
        End Function
    
    
        Public Function GetCount1(ByVal list As IDictionary) As Integer
            Return list.Count
        End Function
    
        Public Function GetCount1(ByVal list As ComponentModel.IListSource) As Integer
            Return list.GetList().Count
        End Function
    







    I compete with myself to motivate me!!

    Do not forget to mark posts, that help you, as "Answer".
  • Re: generic method to handle collection

    11-04-2009, 8:21 AM
    Answer

    You may be able to do this with extension methods if all of your collections implement the IEnumerable interface. Something along the lines of....

        public static class CollectionExtensionMethods
        {
            public static void RemoveCollection<T>(this IEnumerable<T> collection, IEnumerable<T> itemsToRemove)
            {
                collection.RemoveCollection(itemsToRemove);
            }
        }

    Then you could use it like so:

                List<String> strings = new List<String>();
                strings.Add("aaaa");
                strings.Add("bbbb");
                strings.Add("cccc");
    
                List<String> stringsToRemove = new List<String>();            
                strings.Add("bbbb");
    
                strings.RemoveCollection<String>(stringsToRemove);
    
    
                HashSet<String> stringsHash = new HashSet<String>();
                stringsHash.Add("aaaa");
                stringsHash.Add("bbbb");
                stringsHash.Add("cccc");
    
                stringsHash.RemoveCollection<String>(stringsToRemove);


    I haven't tested this but it might be worth a look.

    Let me know if you need any more help

    Scott

     

    My Books:

    Professional Enterprise .NET
    Check out my book on learning all about enterprise programming, including TDD, Mocking, DDD, Dependecy Injection, Inversion of Control, Dependency Inversion, NHibernate, MVC & MVP. Check out the code on the projects codeplex site.

    NHibernate with ASP.net Problem-Design-Solution
    Learn all about NHibernate with ASP.net.
  • Re: generic method to handle collection

    11-06-2009, 9:15 AM
    • Contributor
      5,914 point Contributor
    • atconway
    • Member since 09-24-2007, 5:20 PM
    • Florida U.S.A
    • Posts 1,239

    Based on the code example you provided, the only main suggestion I have is to change the (3) GetCount1 methods to be marked 'Private' instead of Public.  That way you can direct all traffic to the main GetCount method and control how the return and logic is processed without having someone sidestep your main entry point.

    This to me seems a bit analgous to a Factory pattern where you can pass some parameters to a Factory method, and based on the parameters you get the appropriate object type returned from the Factory.  In your case you are not returning objects from a Factory method, but you are determining what is returned by calling the appropriate method based on the object type.

     

    Thank you,   >[Blog]<

    "The best thing about a boolean is even if you are wrong, you are only off by a bit." :D
    -anonymous

Page 1 of 1 (3 items)