ugeb
asked on
can a vb.net function parameter be a generic collection type?
Hi,
Can I pass an argument to a function that is EITHER a List or ArrayList?
I have a function that reads csv files, and some files have only one column. For files that have more than one column, I need to put that data into a List where each element is a List of type String. If there is only one column, then the data type would be just a simple List of type String. I want to pass by reference, but it's not a necessity. Here's an example of how the declaration looks now.
I found a reference that showed how I could do the following, which compiles fine but won't run.
Is there an elegant way to do this?
Thanks!
P.S. I'm stuck using .Net 2
Can I pass an argument to a function that is EITHER a List or ArrayList?
I have a function that reads csv files, and some files have only one column. For files that have more than one column, I need to put that data into a List where each element is a List of type String. If there is only one column, then the data type would be just a simple List of type String. I want to pass by reference, but it's not a necessity. Here's an example of how the declaration looks now.
Private Sub readTextFile(ByVal file As String, ByRef al_txtFileContents As ArrayList)
I would like the ArrayList to be switched to a List, but I can't pass a generic List (as far as I know), because I always need the Of Type modifier, and there are two variations on the theme. Additionally, I'd like to be able to handle ArrayLists as well. All of these are collections, right? Is there a way to treat a parameter as a generic list?I found a reference that showed how I could do the following, which compiles fine but won't run.
Call readTextFile(Of ArrayList)(file, al_OptionStrikeCodes)
Call readTextFile(Of List(Of List(Of String)))(file, al_OptionStrikeCodes)
to call a function with this declaration:
Private Sub readTextFile(Of T)(ByVal file As String, ByRef inputList As ICollection(Of T))
which keeps giving an invalid cast on the function call, even though the variable is declared as the same type as shown in the function call.Is there an elegant way to do this?
Thanks!
P.S. I'm stuck using .Net 2
ASKER
Zone monitor. When you remove a zone please tell me WHY.
I don't really understand the nature of your question. Also, why a ByRef argument, and not a function that returns a List(Of String)?
ASKER
I am indifferent to the ByRef vs. ByVal in this case since the array will not be populated prior calling the function. In the above code it's because I copied it from another forum question (not EE). When I have a populated list I prefer ByRef parameters to save the overhead of copying. Other methods may require modifying an existing ArrayList or List.
Essentially this is what I am trying to do. I have ArrayLists and Lists. These can be populated with either straight strings or the same sub lists of Strings.
For example, the range of data structures I need to support are:
ArrayList of String
ArrayList(ArrayList of String)
List(Of String)
List(Of (List(Of String))
The methods should take any of the above iterable structures and treat ArrayLists and Lists the same way. I'd rather not have two separate methods, one for lists and one for ArrayLists. I can often get the above code (and variations) to compile, but it always fails on the call.
Essentially this is what I am trying to do. I have ArrayLists and Lists. These can be populated with either straight strings or the same sub lists of Strings.
For example, the range of data structures I need to support are:
ArrayList of String
ArrayList(ArrayList of String)
List(Of String)
List(Of (List(Of String))
The methods should take any of the above iterable structures and treat ArrayLists and Lists the same way. I'd rather not have two separate methods, one for lists and one for ArrayLists. I can often get the above code (and variations) to compile, but it always fails on the call.
If you were going to create a generics method (Of T), both structures would need to inherit from the same base class, or implement the same interface.
Here are the declarations for both (common interfaces in bold):
Public Class ArrayList _
Implements IList, ICollection, IEnumerable, ICloneable
Public Class List(Of T) _
Implements IList(Of T), ICollection(Of T), _
IList, ICollection, IReadOnlyList(Of T), IReadOnlyCollection(Of T), _
IEnumerable(Of T), IEnumerable
You would have to use one of the interfaces in bold, and not ICollection(Of T), since ArrayList does not implement that interface.
Here are the declarations for both (common interfaces in bold):
Public Class ArrayList _
Implements IList, ICollection, IEnumerable, ICloneable
Public Class List(Of T) _
Implements IList(Of T), ICollection(Of T), _
IList, ICollection, IReadOnlyList(Of T), IReadOnlyCollection(Of T), _
IEnumerable(Of T), IEnumerable
You would have to use one of the interfaces in bold, and not ICollection(Of T), since ArrayList does not implement that interface.
ASKER
Okay, I understand. can you give an example of the function definition and function call? I've been struggling with the syntax and I need some direction after too many failed attempts.
Private Function readTextFile(ByVal file As String) As ICollection
or
Private Function readTextFile(ByVal file As String) As IList
I can't remember the last time I used ArrayList, since it takes an array of objects, and if you add value types, then it requires box and unboxing to a reference type.
or
Private Function readTextFile(ByVal file As String) As IList
I can't remember the last time I used ArrayList, since it takes an array of objects, and if you add value types, then it requires box and unboxing to a reference type.
ASKER
That's more of a trivial example which I've never had problems with and only works when the function returns the collection. Actually I need the call when the ArrayList or List is passed as an argument and how to call that function.
I really need an example of a declaration and call for when I pass either a List(Of String) or List(Of List(Of String)). It's the correct implementation of my code above:
Thanks.
I really need an example of a declaration and call for when I pass either a List(Of String) or List(Of List(Of String)). It's the correct implementation of my code above:
Private Sub readTextFile(ByVal file As String, ByRef al_txtFileContents As ArrayList)
Call readTextFile(Of List(Of List(Of String)))(file, al_OptionStrikeCodes)
Can you give an example of these? That's what I need.Thanks.
I am struggling to understand what you are looking for...I don't understand the need for ArrayList...
ASKER
I don't understand what's confusing you. I have several collection types and I want to be able to pass them all to the same function.
In the above example, the Of T's are throwing me off. I don't even understand what the first (Of T) after the function name is for. So, what's the syntax for both the function calls and declaration that would make this work?
lst1 = New List(Of List(Of String))
lst2 = New List(Of String)
arrlst = New ArrayList
...
processList(lst1)
processList(lst2)
processList(arrlst)
...
Public Sub processList(Of T)(ByVal collection As ICollection(Of T), ByVal elt As T)
...do work
I declare some collection types, list and arraylist, and then call the function processList. The first parameter of processList has to be able to accept any of the collection types which you said it could do if they all implement ICollection. The mos important ones are lst1 and lst2. In the above example, the Of T's are throwing me off. I don't even understand what the first (Of T) after the function name is for. So, what's the syntax for both the function calls and declaration that would make this work?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you both for the comments. In the end it was vasto's definition that worked as I needed (I'm sorry, I must have overlooked it!)
you can also try
Private Sub readTextFile(ByVal file As String, ByRef al_txtFileContents As object)
but then you need to convert the object to the right type