Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

creating a new obj in called function and passing back to caller

Posted on 2008-10-22
3
Medium Priority
?
173 Views
Last Modified: 2012-05-05
I have a function in a class which you pass any number to it and it finds the factors of that number. It puts them as pairs in 1 or more orderedpair objects which are added to an arraylist. There is only 1 arraylist which holds this list of orderedpair objects in the class. I call a function in the class which returns the arraylist but when I do it 2x with 2 instances of the arraylist in the calling program, the 2nd one gets overwritten with the first. (see code listing)
I know there is a better way to do this - do I need to return by reference?
' calling subroutine
 Private Sub test_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles test.Click
        Dim myArray As ArrayList
        Dim a, b, c As Int32
        Dim afactarray, cfactarray As ArrayList
        a = avar
        b = bvar
        c = cvar
 
        afactarray = New ArrayList()
        cfactarray = New ArrayList()
 
' numfac is an instance declared in the constructor
 
        numfac.FindFactors(a)
        afactarray = numfac.GetFactorList()
        numfac.FindFactors(c)
        cfactarray = numfac.GetFactorList()
            ...
    End Sub
' called subroutine in class
   Public Function GetFactorList() As ArrayList
        Dim myArray As ArrayList = New ArrayList()
        myArray = finalfactorlist
        Return myArray
    End Function

Open in new window

0
Comment
Question by:zliminator
  • 2
3 Comments
 
LVL 13

Accepted Solution

by:
TechTiger007 earned 1000 total points
ID: 22775203
You can try
Return myArray.Clone()
0
 

Author Comment

by:zliminator
ID: 22775435
That was an easy 250 points
0
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22775485
:-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

824 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