[Vala] Read object's properties



Hi!

This is my first post on this list so I just want to say hello to
everybody. My name is Philipp Trommler, I'm from Dresden, Germany and
I'm programming Vala as a hobby. I really like Vala as I like the
syntax, the fact that it's performant and the good integration into the
gnome ecosystem. In the last days I wanted to dig a bit deeper into
serious Vala programming what leads me to my question:

What are common ways to access an object's attributes? I checked and
tried `GLib.ParamSpec` but receiving an empty array all the time. Is
there any other method?

The background is that I want to save an object's attributes into a
file. Alternatively I would save the whole object (that's what I tried
before, without luck, because I can't figure out how to access all of
the object's parts in memory), but accessing the attributes would be
enough for now.

Maybe I'm just doing something wrong, so here's my code:

```
public interface Saveable : GLib.Object {
        public virtual void save_object (string filename) {
                unowned GLib.ObjectClass object_class = this.get_class ();
                GLib.File file = GLib.File.new_for_path (filename);
                GLib.FileOutputStream file_output_stream = file.create
(GLib.FileCreateFlags.PRIVATE |
GLib.FileCreateFlags.REPLACE_DESTINATION);
                foreach (GLib.ParamSpec param_spec in object_class.list_properties ())
{
                        stdout.printf ("%s\n", param_spec.get_name ());
                }
        }
}

public class Test : GLib.Object, Saveable {
        private string name;
        private int zahl;
        private List<string> liste;
        
        public bool boolean;
        protected uint16 kleine;
        
        internal int32 integ;
        
        public Test (string name, int zahl, List<string> liste) {
                this.name = name;
                this.zahl = zahl;
                this.liste = liste.copy ();
        }
}
```

So, as you can see, the class `Test` has some attributes. Why can't I
access any of them?

Greetings,
Philipp.

Attachment: signature.asc
Description: This is a digitally signed message part



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]