## How to save and load files with selfdefined datatypes

The program safes a binary file in the homedirectory.

    car AS CCar
    auto AS CCar
    arCar AS Object[]

    PUBLIC SUB _new()
      myFile AS File
      car = NEW CCar("ford", 150, 340.56)
      auto = NEW CCar
      arCar = NEW Object[]
      arCar.Add(car)
      arCar.Add(auto)
      IF Dialog.SaveFile() THEN RETURN
      OPEN Dialog.Path FOR CREATE AS myFile
      car.WriteTo(myFile)
            'Gambas can not save this object as one thing to a file
            'so the class CCar has to have a method which writes each variable
            'on its own to the file
      CLOSE myFile
    END

    PUBLIC SUB Button2_Click()
      myFile AS File
      IF Dialog.OpenFile() THEN RETURN
      OPEN Dialog.Path FOR READ AS myFile
      auto.ReadFrom(myFile)
      CLOSE myFile

      TextLabel1.Text = auto.getBrand() & "<br>" &
      Str(auto.getPS_Power()) & "<br>" &
      Str(auto.getKW_Power()) & "<br>" &
      Str(auto.getPrice()) & "<br>"

    END

See the Source of the Ccar-Class here: OopCcar

-- JochenGeorges - 28 Dec 2004