Canvas Apps Expressions

Variables – What happens when global & context var have the same name?

Global vs local variables in Power Apps
Global vs local variables in Power Apps

Introduction

Variables are very useful in any programming language. There are two types - global and local/context. Global variables hold values that can be accessed anywhere from the app. Context variables can be referenced from only one screen but can be passed to another screen using the Navigate function.

Tip - Accessing global variables

I recently ran into a behavior which I had not encountered before. There was a global variable in my app. But accessing that variable in a screen was resulting in a completely different value. After investigating it, I found that the screen had a context variable with the same name!

Personally, I will not suggest naming global and local variables the same. If you do, then accessing the variable, in say a label to display its value, will result in the value of the context variable being displayed. So, if global and context variables have the same name, the context variable takes precedence. But if you do, you can access the values of those 2 variables in the following ways:

  1. To access the context variable's value, simply use the variable name e.g. testVar
  2. To access the global variable that has the same name, use the disambiguation operator e.g. [@testVar]

To read the official documentation, click here.

Stay tuned for the remaining 4 tips!

Recent articles

  1. A-Z: 26 tips! Learn how to improve your Power Apps!
  2. Add Picture vs Camera Control – Who is the winner?
  3. Background images for Power Apps screens – the right way!
  4. Combo box or Dropdown? - Who is the winner?
  5. Delegation in Power Apps – How to identify and test!
  6. Edit vs Display – Which is the better form?
  7. Formulas – How to learn the 170+ Power Apps formulas?
  8. Galleries vs Data Tables – How to pick between the two in Power Apps?
  9. Hyperlinks – How to launch web pages & apps in Power Apps!
  10. Icons – How to increase performance!
  11. Jump start – How to speed up Power Apps dev with components!
  12. Keyboard – How to control the keyboard type on mobile
  13. LastSubmit – How to fetch the last submitted record in Power Apps
  14. Media controls in Power Apps – How to improve user experience!
  15. New screens – How to speed up Power Apps dev using templates!
  16. OnStart – How to optimize app loading experience!
  17. Phone camera – How to switch cameras on Power Apps mobile!
  18. Quotes – Should you use single or double in Power Apps?
  19. Reset – How to reset a Power Apps gallery?
  20. SaveData – How to persist your Power Apps sessions!
  21. Tables – How to create tables in Power Apps
  22. User – How to fetch current user’s details in Power Apps

4 thoughts on “Variables – What happens when global & context var have the same name?”

Leave a Reply