' Gambas class file

Public aHeaderList As Variant[]

Public Sub Form_Open()  
  
  Dim i As Integer
  Dim aElement As Variant[]  '--->>> das muss noch einmal genau angesehen werden ...
  Dim sDate As String
  Dim bTF As Boolean
  Dim iSize As Integer
  Dim fValue As Float
  
  FMain.Center
  FMain.Resizable = False
  
  aHeaderList = New Variant[] 
  For i = 0 To 4
    
    sDate = Format(Now(), "dd. mm. yyyy hh:nn")
    bTF = 2 > Rand(0, 10)
    iSize = Rand(10, 99)
    fValue = Round(22 / Rand(1, 9), -4)
    aElement = New Variant[]
    aElement = [bTF, "Gambas", iSize, sDate, "MM", 55.66, True, "Gambas", "BBB", "Meier", fValue]
    aHeaderList.Add(aElement) 
  Next
  
End ' Form_Open()  

'Export
Public Sub Array2CSV(exArray As Variant[], ExportPath As String)
  
  Dim vElement As Variant[]
  Dim i, j As Integer
  Dim sZeile, sF, sT As String
  
  If Not exArray Then Return
  If Not ExportPath Then Return
  
  sF = ","     ' Feld-Trenn-Zeichen
  sT = "\""    ' Text-Trennzeichen
  
  For i = 0 To exArray.Max
    vElement = New Variant[]
    vElement = exArray[i]

    For j = 0 To vElement.Max
      If j < vElement.Max Then
         sZeile &= sT & vElement[j] & sT & sF
      Else
         sZeile &= sT & vElement[j] & sT ' Letztes Feld OHNE Feld-Trennzeichen
      Endif
    Next
    If i < exArray.Max Then sZeile &= gb.NewLine ' Letzte Zeile OHNE CLRF
  Next  
    
  File.Save(ExportPath, sZeile)
  If Error Then 
     Message.Error(Error.Text & Error.Where)
     Return
  Endif ' ERROR
 
End ' Array2CSV(...)
 
' Import
Public Function CSV2Array(ImportPath As String) As Variant[]
  
  Dim sText, sF, sT As String
  Dim aLines, aFields As String[]
  Dim imVariant As Variant[]  
  Dim sZeile As String
  
  sF = ","
  sT = "\""
  
  If Not ImportPath Then Return
 
  If Exist(ImportPath) Then
     sText = File.Load(ImportPath)
     aLines = Split(sText, gb.NewLine) 
  
     imVariant = New Variant[]
  
     For Each sZeile In aLines
       Print "Zeile = "; sZeile
       aFields = New Variant[]
       aFields = Split(sZeile, sF, sT)
       imVariant.Add(aFields)
     Next

     Return imVariant
     
  Endif
  
End ' CSV2Array(...)

Private Sub ShowArray(Array As Variant[])

  Dim vElement As String[]
  Dim i, j As Integer
  
  If Not Array Then Return
  
' Anzeige aller Datensätze (Konsole der IDE) 
  For i = 0 To Array.Max
    vElement = Array[i]
    For j = 0 To vElement.Max
      Print vElement[j],
    Next
    Print
  Next
  
End ' ShowArray(...)

Public Sub btnExport_Click()

  Try Array2CSV(aHeaderList, Application.Path &/ "vdata.dat")
  If Error Then 
     Message.Error(Error.Text & Error.Where)
     Return
  Endif ' ERROR

End ' btnExport_Click()

Public Sub btnImport_Click()

  aHeaderList = CSV2Array(Application.Path &/ "vdata.dat")
  ShowArray(aHeaderList)

End ' btnImport_Click()
