# GAMBAS BOOK 3.18.0

## Data types

### Chapters

28.12.2021
k7:k7.4:k7.4.7:start

## 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

• First an array_A and an array_B of the same type are generated and array_A is filled with data according to type.
• Then Array_A is assigned to an array_B and the content of array_B is output.
• Then array_A is sorted.
• Finally, the contents of array_B are reissued.

Source code extract:

```  Dim aArray_A As New Integer
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 = 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:

` 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:

``` Public Sub GridViewShow(Optional bSorted As Boolean)
   Dim aGArray As New String[]

   aGArray = \$2DGridArray.Copy()

   If bSorted = True Then
      aGArray.Sort
      ArrayToGrid(aGArray)
   Else
     ArrayToGrid(\$2DGridArray)
   Endif ' bSorted = True ?

 End ' GridViewShow(..)

 Public Sub btnGridViewShow_Click()
   GridViewShow()
 End ' btnGridViewShow_Click()

 Public Sub btnGridViewShowS_Click()
   GridViewShow(True)
 End ' btnGridViewShowS_Click()```
The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy. 