Solved

Can a FUNCTION return an array list?

Posted on 2013-01-28
8
228 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
Comment Utility
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
Comment Utility
Or otherwise you would have to assign a new Arraylist to is first, I'll check.
0
 
LVL 35

Expert Comment

by:Robert Schutt
Comment Utility
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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks, but this seems a strange choice of solution?
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now