Skip to content

Instantly share code, notes, and snippets.

@chebizarro
Last active January 13, 2016 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chebizarro/8063b9214431622efdec to your computer and use it in GitHub Desktop.
Save chebizarro/8063b9214431622efdec to your computer and use it in GitHub Desktop.
How to ignore a property if it is the same
public class PropertyDemo : Object {
/* Property-backing fields */
private string _name;
private string _read_only;
/* Properties */
public string automatic { get; set; }
[CCode(notify = "false")]
public string name {
get { return _name; }
set {
if (value != _name) {
_name = value;
notify_property("name");
}
}
}
public string read_only {
get { return _read_only; }
}
public PropertyDemo (string name) {
this.automatic = "InitialAutomatic";
_name = name;
_read_only = "InitialReadOnly";
}
}
void main () {
var demo = new PropertyDemo ("InitialName");
// Every class derived from 'Object' has a 'notify' signal that gets
// emitted every time a property changes
demo.notify.connect ((s, p) => {
stdout.printf ("property '%s' has changed!\n", p.name);
});
demo.automatic = "TheNewAutomatic";
// This will not fire the signal
demo.name = "InitialName";
// This will
demo.name = "NewName";
stdout.printf ("automatic: %s\n", demo.automatic);
stdout.printf ("name: %s\n", demo.name);
stdout.printf ("read_only: %s\n", demo.read_only);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment