Solved

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

Posted on 2013-10-29
13
1,005 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
ID: 39609945
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
ID: 39610293
Zone monitor.  When you remove a zone please tell me WHY.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39610604
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
ID: 39610704
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
ID: 39611604
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
ID: 39613444
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 39613823
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
ID: 39615388
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
ID: 39615398
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
ID: 39615465
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
ID: 39615577
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
ID: 39615752
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

910 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

23 Experts available now in Live!

Get 1:1 Help Now