Solved

Can a FUNCTION return an array list?

Posted on 2013-01-28
8
231 Views
Last Modified: 2013-01-28
I am having difficulty writing a function that returns an ArrayList.
Below is the code:
Call the function (seems to work fine)
Dim AList as new ArrayList
AList = FillList()

Open in new window

The function looks like.
Private Function FillList() as ArrayList
   FillList.Add(1)    << This causes an Object reference not set error.
End Function

Open in new window


Could someone please offer an explanation and solution?

Thanks,
pat
0
Comment
Question by:mpdillon
8 Comments
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 38828857
Try this:
Private Function FillList() as ArrayList
   Dim ret as New ArrayList()
   ret.Add(1)
   Return ret
End Function

Open in new window

I'm not sure but I think the 'old' way of assigning to the function name is only available for 'simple' types.
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 38828862
Or otherwise you would have to assign a new Arraylist to is first, I'll check.
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 38828872
Yes, just checked it, this works as well:
    Private Function FillList() As ArrayList
        FillList = New ArrayList()
        FillList.Add(1)
    End Function

Open in new window

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 38829009
I never liked the old style...  =)

*In your original snippet, you can get rid of the "new" keyword since the function creates the arraylist:

    Dim AList As ArrayList
    AList = FillList()

Or just:

    Dim AList As ArrayList = FillList()

**Also, ArrayList() is rarely the correct structure to use.  Instead create a strongly typed List().  For instance, if you wanted a "list of strings":

    Dim AList As List(Of String) = FillList()

With:

    Private Function FillList() As List(Of String)
        Dim ret as New List(Of String)
        ret.Add("Hello World!")
       Return ret
    End Function
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38829100
I think Idle_Mind alluded to this, but I would strongly suggest abandoning the syntax robert_schutt in http:#a38828872 . Sure, for a seasoned VB person, this may be intuitive, but assigning to the function name never made much sense to me even when I was working with VB. (I'm sure it's probably some carry-over from a previous incarnation of the language, or inherited from some previous language.) It's a bit clearer to see you create an instance of some class that you return from your function using the Return keyword (what robert_schutt did in http:#a38828857). It will also aid those coming behind you who have experience in other languages besides VB. Many languages use some variant of a return keyword. Maintaining that style will make your code more readable to a wider audience.
0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 38829152
Absolutely, I concur. The second example was just to show that the posted code was actually not far off a working function, not a show of preference. But for someone coming from those older environments (mainly Classic ASP for me) this can be very tempting to 'ease in'.
0
 

Author Closing Comment

by:mpdillon
ID: 38829291
Thanks guys. I will change to the return syntax. I know the ArrayList is slower than a straight Array but I wanted to get experience with it anyway. I will look into Lists next time I have a need.
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 38829306
Thanks, but this seems a strange choice of solution?
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

773 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