[Gambas-user] Gambas2-Writing properties of classes to file.

Richard richard.j.walker at ...247...
Fri Jun 11 03:31:43 CEST 2010


On Thursday 10 June 2010 21:02:08 Richard wrote:
> Greetings to the list on a fine warm sunny evening in Belfast,
>
> But here I am, indoors, trying to find an "easy" way to make some data more
> persistent.
>
> In a small app I am writing for my brother I have a number of classes
> designed to hold information about video and audio clips. Some of the data
> is "expensive" to gather - it takes time to process MBytes-GBytes of video
> to find out, for example, how many frames have been captured and how many
> dropped.
>
> In C I might have used an array of structures, or somesuch. In Gambas I
> have implemented the data store as a collection of class instances.
>
> I am trying to find a way to write the property names and values to a file
> and I cannot seem to find anywhere in the documentation, or in my archive
> of this list, a reference to the method used by the IDE when it prints a
> table to the screen of a highlighted class instance name (in break mode of
> course).
>
> The only practical alternative I can see at the moment is to write a long
> procedure to write these names and values to a file but there is so much to
> copy it is quite daunting, and likely to be typing-error-prone. Since in
> debug mode Gambas already knows how to create tables of the data I want to
> save, can I not discover this and do it the same way? Perhaps some of you
> already know how. Here's Hoping!
>
> Richard
>
> ---------------------------------------------------------------------------
>--- ThinkGeek and WIRED's GeekDad team up for the Ultimate
> GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the
> lucky parental unit.  See the prize list and enter to win:
> http://p.sf.net/sfu/thinkgeek-promo
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user

Never underestimate the power of the act of asking for help in concentrating 
the mind on a problem.

I am embarrassed to admit that I have struggled with this one for months and 
now that I have discovered the answer I am even more embarrassed to admit I 
had not been reading the manual with enough care.

In partial defence I have to say that the syntax examples for Class[] and 
Classes[] confused me. First off the illustration for Class (Class[]) doesn't 
work in my test program:

DIM hClass AS Class
DIM hSymbol AS .Symbol

hSymbol = hClass [ Name AS String ]

The declaration of hSymbol doesn't pass the parser's inspection; complaints 
about an unexpected "." and a failure to recognise .Symbol as a valid 
variable type mean that I had to change it to:

DIM hSymbol AS Variant

Secondly I repeatedly failed to grasp the difference between the uses 
of "[Name AS String]" in this and in the example for Classes[]:

DIM hClass AS Class

hClass = Classes [ Name AS String ]

I now know that in the first case (Class[]) the Name refers to the name of a 
class symbol (property, method, event, etc) and in the second (Classes[]) it 
must be the quoted name of a Class.

So, the solution to my problem goes something like this:

PUBLIC SomeInstance AS NEW ClipAudio("Clip_Name.wav")

PUBLIC SUB Action_Click()
DIM fp AS stream

    fp = OPEN User.home &/ "myclassdata.test" FOR OUTPUT CREATE 
    write_properties(fp, SomeInstance)
    CLOSE fp

END

PRIVATE PROCEDURE write_properties(ofile AS stream, instance AS Object)
DIM hClass AS Class
DIM hSymbol AS Variant
DIM symbol_name AS String
DIM prop_val AS String

    hClass = Object.Class(instance)
    FOR EACH symbol_name IN hClass.Symbols
        hSymbol = hClass[symbol_name]
        IF Class.Property = hClass[symbol_name].Kind THEN 
            prop_val = Str(Object.GetProperty(instance, symbol_name))
            PRINT #ofile, symbol_name & "=" & prop_val
        ENDIF 
    NEXT 
  
END

With a bit more hacking I will have section names like [SomeInstance] followed 
by property=value lines and I should be able to use gb.Settings to read the 
data back into the program on the next run. 

And so to bed...

Richard








More information about the User mailing list