locked
How to keep root array value as remove another array value that is assigned by root array RRS feed

  • Question

  • I have short code like this :

    Dim arr1 As New List(Of Integer)
            For i As Integer = 1 To 100
                arr1.Add(i)
            Next
            Dim arr2 As New List(Of Integer)
            arr2 = arr1 'now value arr1,arr2 are the same (contain list value from 1 to 100)
            arr2.RemoveAt(1) 'i will remove a item inside arr2, but i want value of arr1 not change

    My problem is : after RemoveAt command is value of array 2 is change this is true, but why it effect to array 1 , i don't want array 1 value change , so Please help me.

    Thanks !

     

    • Moved by litdev Thursday, September 8, 2011 5:45 PM (From:Small Basic)
    Thursday, September 8, 2011 3:14 AM

Answers

  • As Armin stated in his post your assignment statement is just making reference to the same data structure on the heap. In order to get a new list you can also do this.

    arr2 = arr1.ToList()

    Which will create a new list with the same values from the first list.

     

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, September 8, 2011 6:51 PM
  • A List(Of Integer) is a class. Classes are reference types. This means that variable arr1 contains a reference to the object. If you assign "arr2 = arr1" you are copying the reference into variable arr2. Now both variables reference the same list. If you want a second list containing a copy of the items of the first list, you can write:

        arr2 = New List(Of Integer)(arr1)

    This way you are passing arr1 to the constructor of the 2nd list. The constructor copies the items.


    Armin
    Thursday, September 8, 2011 6:19 PM

All replies

  • this is the small basic forum. Use the VB forum instead.
    ~~AirWaves!!~~
    Thursday, September 8, 2011 3:50 AM
  • A List(Of Integer) is a class. Classes are reference types. This means that variable arr1 contains a reference to the object. If you assign "arr2 = arr1" you are copying the reference into variable arr2. Now both variables reference the same list. If you want a second list containing a copy of the items of the first list, you can write:

        arr2 = New List(Of Integer)(arr1)

    This way you are passing arr1 to the constructor of the 2nd list. The constructor copies the items.


    Armin
    Thursday, September 8, 2011 6:19 PM
  • As Armin stated in his post your assignment statement is just making reference to the same data structure on the heap. In order to get a new list you can also do this.

    arr2 = arr1.ToList()

    Which will create a new list with the same values from the first list.

     

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, September 8, 2011 6:51 PM
  • What is mentioned above is totally correct. Just wanted to add that 

    List(Of Integer)

    is not an array it is a List and lists are reference types.

    You can create an array using this code for example:

    Dim arrNumbers() As Integer 'Declares the array of integers
    arrNumbers = New Integer() {0,1,2,3,4} 'Initialises the array to five members & sets their values


    Ali Hamdar (alihamdar.com - www.ids.com.lb)
    Thursday, September 8, 2011 7:07 PM