Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

947 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

20 Experts available now in Live!

Get 1:1 Help Now