Search code examples
vala

vala: convenient way of getting properties


I know that one can set a property of a GLib.Object-based class by name using the following simple syntax

obj["foo"] = bar;

Is there a way to get the property's value in the similar way? The following construction doesn't seem to work:

Bar bar = obj["foo"];

Vala returns error: invocation of void method not allowed as expression

I know it can be done as in the example below, but I'm looking for something conciser.

Bar bar;
obj.get("foo", out bar);

Solution

  • Bar bar = obj.foo;
    

    You should use similar code to set properties, too, instead of what you wrote above:

    obj.foo = bar;
    

    It's not usually a big deal, but that form tends to be a bit more efficient than going through GObject properties. And it's shorter. IMHO it looks better, too.