Skip to main content
30 April 2024
Follow Us

Types of variables in Power Apps

In today's article we're going to look at the different types of variables that exist in Power Apps, how to create, add and remove them!

Power Apps has three types of variables:

Type of variable

 

Scope

Description

 

Functions that establish

Global variables

Application

Simpler to use. Contains a number, text string, boolean, record, table, etc., which can be referenced from anywhere in the application.

Set

Context variables

Screen

Ideal for transmitting values to screens, in a similar way to parameters transmitted to procedures in other languages. They can only be referenced from a screen.

UpdateContext
Navigate

Collections

Application

They contain a table that can be referenced from anywhere in the application. They allow the contents of a table to be modified instead of being defined as a whole. Can be saved on the local device for later use.

Collect
ClearCollect

Creating and removing variables

 

All variables are created implicitly when they appear in a Set, UpdateContext, Navigate, Collect or ClearCollect function. To declare a variable and its type, all you have to do is include it in any of these functions anywhere in the application. None of these functions create variables; they only fill variables with values. They never declare variables explicitly, as you might do in other programming tools, and all typing is implicit in their use.

For example, you could have a Button control with an OnSelect formula equal to Set( X, 1 ). This formula sets X as a variable with a number type. You can use X in formulas as a number and that variable has a blank value after you open the application, but before you select the button. When you select the button, you assign X the value 1.

If you have added another button and set the OnSelect property to Set( X, "Hello" ), an error will occur because the type (text string) does not match the type of the previous Set (number). All implicit variable definitions must agree on the type. Again, all this happened because you mentioned X in formulas, not because any of those formulas were actually executed.

You can remove a variable by removing all Set, UpdateContext, Navigate, Collect or ClearCollect functions that implicitly set the variable. Without these functions, the variable doesn't exist. You must also remove any references to the variable because it will cause an error.

Rui Couto

Assine a nossa newsletter e receba o nosso conteúdo diretamente no seu email