Solved

can a vb.net function parameter be a generic collection type?

Posted on 2013-10-29
13
979 Views
Last Modified: 2013-10-31
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
Comment
Question by:ugeb
  • 6
  • 4
  • 2
13 Comments
 
LVL 18

Expert Comment

by:vasto
Comment Utility
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
 
LVL 11

Author Comment

by:ugeb
Comment Utility
Zone monitor.  When you remove a zone please tell me WHY.
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
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
 
LVL 11

Author Comment

by:ugeb
Comment Utility
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
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
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
 
LVL 11

Author Comment

by:ugeb
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
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
 
LVL 11

Author Comment

by:ugeb
Comment Utility
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
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
I am struggling to understand what you are looking for...I don't understand the need for ArrayList...
0
 
LVL 11

Author Comment

by:ugeb
Comment Utility
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
 
LVL 18

Accepted Solution

by:
vasto earned 500 total points
Comment Utility
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
 
LVL 11

Author Closing Comment

by:ugeb
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Response back from stored procedure 6 47
sorting efficency of sorting algorithm 30 77
Vb.Net Menu 1 20
Copy/Clone an object. 9 7
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now