Solved

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

Posted on 2008-10-22
3
163 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 250 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

786 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