Table of Contents

7.4.7 Array copy

The following section deals with comparative considerations of arrays under the aspects' Assigning an array' and' Copying an array'.

7.4.7.1 Assignment


Source code extract:

  Dim aArray_A As New Integer[3]
  Dim aArray_B As Integer[]
  Dim iCount As Integer

  aArray_A = [4, -3, 7] ' [4, -3, 7] is an inline array (without name)
  aArray_B = aArray_A ' aArray_B receives reference to aArray_A

  For iCount = 0 To aArray_B.Max ' Output Array_B
    Print aArray_B[iCount]
  Next

  aArray_A.Sort(gb.Descent)

  For iCount = 0 To aArray_A.Max ' Output Array_A
    Print aArray_A[iCount],
  Next
  Print
  For iCount = 0 To aArray_B.Max ' Output Array_B
    Print aArray_B[iCount]
  Next

Output in the IDE console:

4 -3  7   ' Content of aArray_B is content of aArray_A
7 4 -3    ' Content of aArray_A after sorting
7 4 -3    ' Content of aArray_B (without sorting!)

The displayed contents of array_A and array_B after sorting array_A are the same! The generalized explanation for Object_A and Object_B is simple: A reference to the data of Object_A is stored in the variable Object_B. This means: Object_A and Object_B point to the same object in memory. If you change the object_A - for example, to sort the contents of the object - then Object_B also points to this changed data.

7.4.7.2 Array copy

Native arrays have the Array.Copy()-method and therefore belong to the objects that can be copied easily. This method returns a new array object' Array_B' as a 1:1 copy of' Array_A' with the same content. However, the two objects Array_A and Array_B are completely independent of each other. Later changes to the original array_A, for example, have no effect on the copy array_B and vice versa.

  aArray_B = aArray_A.Copy() ' aArray_B as 1:1 copy of aArray_A

  aArray_A[1] = 55 ' Modification of aArray_A in the 2nd element

  For iCount = 0 To aArray_A.Max ' Output array_A
    Print aArray_A[iCount],
  Next

  Print
  For iCount = 0 To aArray_B.Max ' Output array_B
    Print aArray_B[iCount],
  Next

Output in the IDE console:

7  55  -3   ' Content of aArray_A
7  4   -3   ' Content of aArray_B

7.4.7.3 Example for using an array copy

To show alternately the effect of sorting array elements in a GridView with the _compare() method in a demonstration project, the original data array and the array of sorted data of the same array type are required. The complete project is described in chapter 7.4.9 Array - Sorting.


Figure 7.4.7.3.3.1: GridView data (original data vs. sorted data (–> field surname))

This code was first used in line 4 of the following sourcecode extract:

[4] aGArray = $2DGridArray

During the test - first the original and then sorting by surnames in the first field - the two pictures shown above were shown one after the other –> figure 7.4.7.3.1. That's how it should be!

Effect achieved? No, because each further click on one of the two buttons showed only a representation as in the second picture. The explanation for this behavior can be found in the upper section about assigning arrays and their special features.

Correct is the following section of the source code where you work with two different array objects, where the array aGArray is a 1:1 copy of array $2DGridArray (line 4). The sorting of the elements of aGArray in row 7 does not affect the array $2DGridArray in any way, so that the correct views are always generated in both procedures in rows 15 and 19:

[1] Public Sub GridViewShow(Optional bSorted As Boolean)
[2]   Dim aGArray As New String[]
[3]
[4]   aGArray = $2DGridArray.Copy()
[5]
[6]   If bSorted = True Then
[7]      aGArray.Sort
[8]      ArrayToGrid(aGArray)
[9]   Else
[10]     ArrayToGrid($2DGridArray)
[11]   Endif ' bSorted = True ?
[12]
[13] End ' GridViewShow(..)
[14]
[15] Public Sub btnGridViewShow_Click()
[16]   GridViewShow()
[17] End ' btnGridViewShow_Click()
[18]
[19] Public Sub btnGridViewShowS_Click()
[20]   GridViewShow(True)
[21] End ' btnGridViewShowS_Click()