Solved

Can a FUNCTION return an array list?

Posted on 2013-01-28
8
229 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

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…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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 …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

911 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