[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1175
  • Last Modified:

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.
Private Sub readTextFile(ByVal file As String, ByRef al_txtFileContents As ArrayList)

Open in new window

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)

Open in new window

to call a function with this declaration:
Private Sub readTextFile(Of T)(ByVal file As String, ByRef inputList As ICollection(Of T))

Open in new window

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
0
ugeb
Asked:
ugeb
  • 6
  • 4
  • 2
1 Solution
 
vastoCommented:
Private Sub readTextFile(ByVal file As String, ByRef al_txtFileContents As IList)

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
0
 
ugebAuthor Commented:
Zone monitor.  When you remove a zone please tell me WHY.
0
 
Bob LearnedCommented:
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)?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
ugebAuthor Commented:
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.
0
 
Bob LearnedCommented:
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.
0
 
ugebAuthor Commented:
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.
0
 
Bob LearnedCommented:
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.
0
 
ugebAuthor Commented:
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:
Private Sub readTextFile(ByVal file As String, ByRef al_txtFileContents As ArrayList)
Call readTextFile(Of List(Of List(Of String)))(file, al_OptionStrikeCodes)

Open in new window

Can you give an example of these? That's what I need.
Thanks.
0
 
Bob LearnedCommented:
I am struggling to understand what you are looking for...I don't understand the need for ArrayList...
0
 
ugebAuthor Commented:
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.
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

Open in new window

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?
0
 
vastoCommented:
It was in my first post :

Private Sub readTextFile(ByVal file As String, ByRef al_txtFileContents As IList)

the call will be
readTextFile(file, arrayList)
0
 
ugebAuthor Commented:
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!)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now