Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Can a FUNCTION return an array list?

Posted on 2013-01-28
8
Medium Priority
?
235 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 86

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 2000 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

What is a Denial of Service (DoS)?

A DoS is a malicious attempt to prevent the normal operation of a computer system. You may frequently see the terms 'DDoS' (Distributed Denial of Service) and 'DoS' used interchangeably, but there are some subtle differences.

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

722 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