Solved

Can a FUNCTION return an array list?

Posted on 2013-01-28
8
232 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vb.net winforms sizing/resolution? 4 55
VS 2017 18 102
PowerShell:  Convert parameter's friendly name to another name... hashtable perhaps? 2 46
Regarding swagger API 1 32
IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

763 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