[Gambas-user] save and load an array of MydataType

Jochen Georges jochengeorges at ...467...
Thu Jan 6 12:59:33 CET 2005


Hello gambanauts,

I do not find a way on my own :(
I have an array wich is filled with objects of the class CCar.
I would like to save and load this array.
I found a way to do that if i know how much elements the array contains.
But I do not find a way, if I do not know how much elements the array 
contains.

------------------------------------------------------------
(The form has got two buttons (load and save) and a TextLabel)
arCars AS Object[]
cnt AS Integer

PUBLIC SUB _new()
    'make the array arCar with 3 objects of CCar in it
    i AS Integer
    DIM tmpcar AS CCar
    cnt = 3
    arCars = NEW Object[]
    FOR i = 0 TO cnt
        tmpcar = NEW CCar(Str(i))
        arCars.add(tmpcar)
    NEXT
END

PUBLIC SUB Button1_Click()
 'save it to an file
 ' I think i only save the CCar objects and not the array itself, do I?
    myFile AS File
    i AS Integer
    IF Dialog.SaveFile() THEN RETURN
        TRY OPEN Dialog.Path FOR CREATE AS myFile
            FOR i = 0 TO cnt
                arCars[0].writeTo(myFile)
            NEXT
        CLOSE myFile
END

PUBLIC SUB Button2_Click()
    i AS Integer
    myFile AS File
    newCars AS Object[]
    tmpcar AS CCar
    newCars = NEW Object[]
    FOR i = 0 TO cnt
        tmpcar = NEW CCar
        newCars.add(tmpcar)
    NEXT
    IF Dialog.OpenFile() THEN RETURN
        TRY OPEN dialog.Path FOR READ AS myFile
 'guess cnt is not constant and I do not know how much objcts of car are in
 ' the file:
 'how can I know at this point how much objects of CCar are in the file?
            FOR i = 0 TO cnt
                newCars[i].ReadFrom(myFile)
            NEXT
        CLOSE myFile
        TLb1.Text = Str(arCars[0].getBrand())  & 
  Str(arCars[1].getBrand()) &
  Str(arCars[2].getBrand()) 
END

I assume, that i have to write a class 
"MyOwnArrayWhichIsFilledWithCCarObjects"
this class needs a Method WriteTo.
And a set- and get Method for each set- and getMethod of CCar.
That does not seem to be elegant to me.

So, how would you do that?

Thanks for any hint.

Jochen





More information about the User mailing list