summaryrefslogtreecommitdiffhomepage
path: root/vala.html.markdown
diff options
context:
space:
mode:
authorMilo Gilad <milogaccnts@gmail.com>2017-08-25 12:33:39 -0400
committerMilo Gilad <milogaccnts@gmail.com>2017-08-25 12:33:39 -0400
commitfe5001ca964e048ea92f0f7b15f486e57bfb5235 (patch)
tree19b6cbddaa2af60d8bf6587b1859efe960d9e9f2 /vala.html.markdown
parent81f58f4b0d101014fa1df81e2f48e123dd0dbcf2 (diff)
Added properties (get and set)
Diffstat (limited to 'vala.html.markdown')
-rwxr-xr-xvala.html.markdown20
1 files changed, 19 insertions, 1 deletions
diff --git a/vala.html.markdown b/vala.html.markdown
index 237d4a15..f0141eb4 100755
--- a/vala.html.markdown
+++ b/vala.html.markdown
@@ -240,7 +240,7 @@ enum HouseSize { // An example of an enum
class Message : GLib.Object { // Class Message extends GLib's Object
private string sender; // a private field
- public string text {get; set;} // a public property
+ public string text {get; set;} // a public property (more on that later)
protected bool is_digital = true; // protected (this class and subclasses)
internal bool sent = false; // internal (classes in same package)
@@ -302,6 +302,24 @@ public class SignalDemo : GLib.Object {
// You may use the connect() method and attach as many handlers as you'd like.
// They'll all run when the signal is emitted.
+// Properties (getters and setters)
+
+class Animal : GLib.Object {
+ private int _legs; // prefixed with underscore to prevents name clashes
+
+ public int legs {
+ get { return _legs; }
+ set { _legs = value; }
+ }
+
+ public int eyes { get; set; default = 5; } // Shorter way
+}
+
+rabbit = new Animal();
+rabbit.legs = 2;
+rabbit.legs += 2;
+rabbit.eyes = 2;
+
```
* More Vala documentation can be found [here](https://valadoc.org/).
* Read about building GUIs with GTK+ and Vala [here](http://archive.is/7C7bw).