The saving of derived arrays is characterized by some special features, because Gambas 3 can only save and read out native arrays in a file specific to gambas. You have to implement the serialization of the array elements of derived arrays during array export as well as the de-serialization during array import. In a suitable control structure, write the object properties of the objects stored in the elements of the derived array to the file stream one after the other, because object properties of native data type can serialize Gambas. If a property of an object is again an object, Gambas cannot serialize this property. You must then apply the procedure recursively until only native data types are available and can be saved.
When importing, you read the object properties from the export file and (re-)construct the array.
The project follows this plan:
The array used in the project is an array derived from a self-written class and the elements of the array are objects of this class. The class CDS is a (pure) data structure without its own methods and represents, for example, a record in a course list.
Source code of CDS. class:
' Gambas class file Public JGS As Integer Public BirthDate As Date Public FirstName As String Public LastName As String Public Course1 As String Public Course2 As String
The complete project can be found in the download area and here you will find the source code of FMain. class:
[1] ' Gambas class file [2] [3] Public arrayKL As New CDS[] ' Create object with the name arrayKL of the array class CDS[]. [4] Public hCDS As CDS ' Create variable of type CDS [5] [6] Public Sub Form_Open() [7] FMain.Center [8] FMain.Resizable = False [9] If Not Exist(Application.Path &/ "array.dat") Then [10] SetEnabled(True, False, False) [11] Else [12] SetEnabled(True, False, True) [13] Endif [14] End ' Form_Open() [15] [16] Public Sub btnSetClassArray_Click() [17] Dim iCount As Integer [18] Dim aNames As String[] = ["Eagle", "Bear", "Badger", "Fox", "Tit", "Eagle Owl"] [19] Dim aSurNames As String[] = ["Anna", "Bruno", "Doris", "Fred", "Maria", "Udo"] [20] Dim aCourses1 As String[] = ["Mathematics", "History", "Astronomy", "Biology", "Computer Science"] [21] Dim aCourses2 As String[] = ["German", "Physics", "English", "Art", "Music"] [22] [23] Randomize [24] [25] For iCount = 0 To 9 [26] hCDS = New CDS [27] hCDS.JGS = CInt(Rnd(11, 13)) ' 11..12 [28] ' Random date: Year: matching JGS, Month: 1..12, Day: 1..28 (this always matches...) [29] hCDS.BirthDate = Date(Int(Rnd(Year(Now())-18, Year(Now())-16)), Int(Rnd(1,13)), Int(Rnd(1,29))) [30] hCDS.FirstName = aSurNames[Int(Rnd(0, aSurNames.Count))] [31] hCDS.LastName = aNames[Int(Rnd(0, aNames.Count))] [32] hCDS.Course1 = aCourses1[Int(Rnd(0, aCourses1.Count))] [33] hCDS.Course2 = aCourses2[Int(Rnd(0, aCourses2.Count))] [34] arrayKL.Add(hCDS) [35] Next ' iCount [36] [37] ' Display of all data sets (console of the IDE) for control purposes [38] ShowCDSArrayElements(arrayKL) [39] SetEnabled(True, True, False) [40] [41] End ' btnClassArray_Click() [42] [43] Public Sub btnArrayDatenExport_Click() [44] Dim iCount As Integer [45] Dim hFile As File [46] [47] ' Save all elements of the derived array 'arrayKL' in a file. [48] Dialogue.Title = "Array export to a binary file". [49] Dialog.Filter = ["*.dat", "Data Files"] [50] Dialog.Path = Application.Path &/ "array.dat" [51] [52] If Dialog.SaveFile() Then Return [53] hFile = Open Dialog.Path For Write Create [54] [55] ' Serialisation of the object properties [56] For iCount = 0 To arrayKL.Max [57] With arrayKL[iCount] [58] Write #hFile, .JGS As Integer [59] Write #hFile, .BirthDate As Date [60] Write #hFile, .FirstName As String [61] Write #hFile, .LastName As String [62] Write #hFile, .Course1 As String [63] Write #hFile, .Course2 As String [64] End With [65] Next [66] [67] Close #hFile [68] SetEnabled(True, True, True) [69] [70] End ' btnDataExport_Click() [71] [72] Public Sub btnArrayDataImport_Click() [73] Dim iCount As Integer [74] Dim hFile As File [75] [76] ' Read in all elements of the derived array 'arrayKL' from a file. [77] Dialog.Title = "Array import from a binary file". [78] Dialog.Filter = ["*.dat", "Data Files"] [79] Dialog.Path = Application.Path &/ "array.dat" [80] [81] If Dialog.OpenFile() Then Return [82] hFile = Open Dialog.Path For Read [83] [84] arrayKL.Clear [85] ' De-serialisation [86] While Not Eof(hFile) [87] hCDS = New CDS [88] hCDS.JGS = Read #hFile As Integer [89] hCDS.BirthDate = Read #hFile As Date [90] hCDS.Firstname = Read #hFile As String [91] hCDS.Lastname = Read #hFile As String [92] hCDS.Course1 = Read #hFile As String [93] hCDS.Course2 = Read #hFile As String [94] arrayKL.Add(hCDS) [95] Wend [96] [97] ' Display of all data sets (IDE console)) [98] ShowCDSArrayElements(arrayKL) [99] Close #hFile [100] SetEnabled(True, True, True) [101] [102] End ' btnDatenImport_Click() [103] [104] Private Sub ShowCDSArrayElements(aArray As CDS[]) [105] ' Display of all data sets (IDE console)) [106] For Each hCDS In aArray [107] Print hCDS.JGS, [108] Print Format(hCDS.BirthDate, "dd.mm.yyyy"), [109] Print hCDS.Firstname, [110] Print hCDS.Lastname, [111] Print hCDS.Course1, [112] Print hCDS.Course2 [113] Next ' hCDS [114] Print [115] End ' ShowCDSArrayElements(aArray As CDS[]) [116] [117] Public Sub SetEnabled(bAR As Boolean, bEX As Boolean, bIM As Boolean) [118] btnSetClassArray.Enabled = bAR [119] btnArrayDatenExport.Enabled = bEX [120] btnArrayDatenImport.Enabled = bIM [121] End ' SetEnabled(..)
Comments:
Public arrayKL As New CDS[]
11 17.08.1997 Anna Bear Mathematics Music 11 23.11.1996 Maria Bear Astronomy Physics 12 19.02.1997 Bruno Bear Astronomy Art 12 03.12.1996 Anna Badger Astronomy Music 12 05.07.1997 Maria Bear History German 12 19.09.1996 Doris Owl History English 12 03.09.1996 Maria Bear Mathematics English 12 01.09.1996 Udo Tit Computer German 12 24.09.1996 Doris Owl Computer German 11 24.10.1997 Doris Tit Mathematics Physics