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