Properties: Getters and Setters

To use a component property as a variable, we need to be able to get the current value and set a new value. This is done with blocks called, logically, getters and setters. They are available in the flyout menu for each component (see Figure 4.2).

Image

Figure 4.2 The getter and setter blocks for TheButton’s Text property.

Here’s the idea: Every time the button is clicked, we will get the current value that’s in the text property, change it to be 1 greater, and then set that value back into the property. It will update the property to have the new number in it, which will appear on the screen.

That idea translates ...

Get Learning MIT App Inventor: A Hands-On Guide to Building Your Own Android Apps now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.