The following section deals with comparative considerations of arrays under the aspects' Assigning an array' and' Copying an array'.
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] ist ein Inline-Array (ohne Namen) aArray_B = aArray_A ' aArray_B erhält Referenz auf aArray_A For iCount = 0 To aArray_B.Max ' Ausgabe Array_B Print aArray_B[iCount] Next aArray_A.Sort(gb.Descent) For iCount = 0 To aArray_A.Max ' Ausgabe Array_A Print aArray_A[iCount], Next Print For iCount = 0 To aArray_B.Max ' Ausgabe Array_B Print aArray_B[iCount] Next
Output in the IDE console:
4 -3 7 ' Inhalt von aArray_B ist Inhalt von aArray_A 7 4 -3 ' Inhalt von aArray_A nach dem Sortieren 7 4 -3 ' Inhalt von aArray_B (ohne Sortierung!)
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.
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
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()
Im folgenden Abschnitt geht es um vergleichende Betrachtungen zu Arrays unter den Aspekten 'Zuweisung eines Arrays' und 'Kopie eines Arrays'.
Quelltext-Ausschnitt:
Dim aArray_A As New Integer[3] Dim aArray_B As Integer[] Dim iCount As Integer aArray_A = [4, -3, 7] ' [4, -3, 7] ist ein Inline-Array (ohne Namen) aArray_B = aArray_A ' aArray_B erhält Referenz auf aArray_A For iCount = 0 To aArray_B.Max ' Ausgabe Array_B Print aArray_B[iCount] Next aArray_A.Sort(gb.Descent) For iCount = 0 To aArray_A.Max ' Ausgabe Array_A Print aArray_A[iCount], Next Print For iCount = 0 To aArray_B.Max ' Ausgabe Array_B Print aArray_B[iCount] Next
Ausgabe in der IDE-Konsole:
4 -3 7 ' Inhalt von aArray_B ist Inhalt von aArray_A 7 4 -3 ' Inhalt von aArray_A nach dem Sortieren 7 4 -3 ' Inhalt von aArray_B (ohne Sortierung!)
Die angezeigten Inhalte von Array_A und Array_B nach dem Sortieren von Array_A sind gleich! Die verallgemeinerte Erklärung bezogen auf Objekt_A und Objekt_B ist einfach: Es wird in der Variable Objekt_B eine Referenz auf die Daten von Objekt_A hinterlegt. Das bedeutet: Objekt_A und Objekt_B zeigen auf das gleiche Objekt im Speicher. Wenn Sie das Objekt_A ändern – beispielsweise den Inhalt des Objekts sortieren – so zeigt auch Objekt_B auf diese geänderten Daten.
Native Arrays besitzen die Array.Copy()-Methode und gehören daher zu den Objekten, die sich problemlos kopieren lassen. Diese Methode gibt Ihnen ein neues Array-Objekt 'Array_B' als 1:1-Kopie von 'Array_A' mit dem gleichen Inhalt zurück. Die beiden Objekte Array_A und Array_B sind jedoch völlig unabhängig von einander. Spätere Änderungen am Original Array_A haben zum Beispiel keine Auswirkung auf die Kopie Array_B und umgekehrt.
aArray_B = aArray_A.Copy() ' aArray_B als 1:1-Kopie von aArray_A aArray_A[1] = 55 ' Änderung von aArray_A im 2. Element For iCount = 0 To aArray_A.Max ' Ausgabe Array_A Print aArray_A[iCount], Next Print For iCount = 0 To aArray_B.Max ' Ausgabe Array_B Print aArray_B[iCount], Next
Ausgabe in der IDE-Konsole:
7 55 -3 ' Inhalt von aArray_A 7 4 -3 ' Inhalt von aArray_B
Um in einem Demonstrationsprojekt den Effekt der Sortierung von Array-Elementen in einer GridView mit der _compare()-Methode abwechselnd zu zeigen, werden das originale Daten-Array und das Array der sortierten Daten vom gleichen Array-Typ benötigt. Das vollständig beschriebene Projekt finden Sie im → Kapitel 7.4.9 Array – Sortierung.
Abbildung 7.4.7.3.1: GridView-Daten (Originale Daten vs. sortierte Daten (→ Feld Nachname))
In der Zeile 4 des folgenden Quelltext-Auszugs wurde zuerst dieser Code verwendet:
[4] aGArray = $2DGridArray
Beim Test ergaben sich – erst Original und dann Sortierung nach den Nachnamen im ersten Feld – nacheinander die beiden oben abgebildeten Bilder → Abbildung 7.4.7.3.1. So sollte es sein!
Effekt erreicht? Nein, denn jeder weitere Klick auf einen der beiden Button zeigte nur noch eine Darstellung wie im 2. Bild. Die Erklärung für dieses Verhalten finden Sie im oberen Abschnitt über die Zuweisung von Arrays und deren Besonderheiten.
Korrekt ist der folgende Quelltext-Ausschnitt, in dem mit zwei verschiedenen Array-Objekten gearbeitet wird, wobei das Array aGArray eine 1:1-Kopie von Array $2DGridArray ist (Zeile 4). Die Sortierung der Elemente von aGArray in der Zeile 7 beeinflusst das Array $2DGridArray in keiner Weise, so dass in den beiden Prozeduren in Zeile 15 und 19 stets die richtigen Ansichten generiert werden:
[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()