cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Super User
Super User

Mixing Set() and UpdateContext() on same variable name.

I would like to know if there is a fundamental issue using Set() and UpdateContext() on the same variable name.  I have an app where I realized I have mixed the two.  It seems to have let me do it, but now I'm getting some strange type errors that were not there before.  It started out as Set(), then I used it as UpdateContext() later.  It was working... not sure if this is one of those "expect unexpected results" situations.

 

Thanks!

2 ACCEPTED SOLUTIONS

Accepted Solutions
Power Apps Staff CarlosFigueira
Power Apps Staff

Re: Mixing Set() and UpdateContext() on same variable name.

You can use the same variable names with Set and UpdateContext, but they are referring to different variables. If you have this expression in a button's OnSelect, for example:

Set(a, 123); UpdateContext({ a: 444 })

This is setting the value of two distinct variables, (global) 'a' and (context, local to the screen) 'a'. If you need to refer simply to 'a' on that screen, then you'll be referring to the local variable - this on a label's Text property will show 'a = 444':

"a = " & a

If you want to refer to the value of the global variable, you need to use the disambiguation operator:

"a (global) = " & [@a]

In general, to prevent this kind of confusion, I'd advise against have both global and context variables with the same name. If you can avoid it, then there are less chances that you'll be mixing them up.

The attached app shows this difference. To open it, save it locally, then go to https://create.powerapps.com, select Open, then Browse, then select the file that you saved before.

Hope this helps!

View solution in original post

Super User
Super User

Re: Mixing Set() and UpdateContext() on same variable name.

I found the variable browser in File->Variables.

 

image.png

View solution in original post

3 REPLIES 3
Power Apps Staff CarlosFigueira
Power Apps Staff

Re: Mixing Set() and UpdateContext() on same variable name.

You can use the same variable names with Set and UpdateContext, but they are referring to different variables. If you have this expression in a button's OnSelect, for example:

Set(a, 123); UpdateContext({ a: 444 })

This is setting the value of two distinct variables, (global) 'a' and (context, local to the screen) 'a'. If you need to refer simply to 'a' on that screen, then you'll be referring to the local variable - this on a label's Text property will show 'a = 444':

"a = " & a

If you want to refer to the value of the global variable, you need to use the disambiguation operator:

"a (global) = " & [@a]

In general, to prevent this kind of confusion, I'd advise against have both global and context variables with the same name. If you can avoid it, then there are less chances that you'll be mixing them up.

The attached app shows this difference. To open it, save it locally, then go to https://create.powerapps.com, select Open, then Browse, then select the file that you saved before.

Hope this helps!

View solution in original post

Super User
Super User

Re: Mixing Set() and UpdateContext() on same variable name.

My mixing of set() and updatecontext() was completely inadvertant.  Its good to know that they are treated differently.  I am trying to get rid of the mixing... however, the problem is I cannot find all of the instances where I used updatecontext.  There are far too many controls, screens, and objects where these statements are hidden.  How the heck do I find where this exists in my app?  I still think I have the issue.  I am having a situation where my app is working, then out of nowhere, it decides that my variable is the wrong type.  I dont get this, but it cant be coincidence that it was the variable where I mixed the statements.  I currently have no errors, but something will happen, the I get red "x" all over until it flips back to being "ok" for unknown reasons.  How can I find these other statements?

Super User
Super User

Re: Mixing Set() and UpdateContext() on same variable name.

I found the variable browser in File->Variables.

 

image.png

View solution in original post

Helpful resources

Announcements
thirdimage

Power Apps Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

sixthImage

Power Platform World Tour

Find out where you can attend!

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

SecondImage

Difinity Conference

The largest Power BI, Power Platform, and Data conference in New Zealand

Top Solution Authors
Top Kudoed Authors (Last 30 Days)
Users online (4,959)