User Tools

Site Tools


k7:k7.4:k7.4.8:k7.4.8.2:start

7.4.8.2 Export and import of derived arrays

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:

  • First, a derived array is created and filled with random data.
  • The array elements are then displayed in the IDE console for control.
  • Then you can export the created array (without receipt in case of success)? file save dialog (binary file) or
  • import an array after a file open dialog from the export file.
  • Finally, the array elements of the imported array are displayed in the IDE console.

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:

  • The class CDS[] does not actually exist. However, since it ends with “[]” and there is a class CDS, the interpreter automatically creates on-the-fly a class CDS[] derived from CDS, which is an array of 'CDS' objects. For example, a new object of this array class is created in line 3:
Public arrayKL As New CDS[]
  • In lines 16 to 41, the array of 'CDS' objects - arrayKL - is filled with 10 data records of type CDS. First, the For-Next control structure successively creates space for a new array element in array arrayKL (line 27), then a new, empty data record of type CDS is created and immediately filled with random values. Finally (line 34), the current data record is inserted as an element in the array arrayKL. In line 38, all ten records are output as contents of array array arrayKL formatted for control purposes and displayed in the console of the Gambas IDE.
  • The export of the derived array array arrayKL takes place in lines 43 to 70. If the file save dialog is successfully closed, a new file with the given path is created and opened for writing.
  • For all ten elements of the array arrayKL of object type CDS, the 6 object properties are written one after the other into the file stream. The file is then closed.
  • In line 84, all elements of the existing array array arrayKL are deleted using the Clear method.
  • When importing in lines 72 to 102, the path to the file with the exported array is available after the successful file open dialog and the file is opened for reading.
  • As long as the end of the file has not yet been reached, the elements of the array arrayKL are generated as CDS objects serially from the 6 values of the object properties and inserted into the array arrayKL (lines 86 to 95).
  • Finally, in line 104 all ten records are output as contents of the (imported) array array array arrayKL formatted and displayed in the console of the Gambas IDE:
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

Download

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.
k7/k7.4/k7.4.8/k7.4.8.2/start.txt · Last modified: 05.02.2022 (external edit)

Page Tools