Variable not persisting from one element to another

I am working on a quick practice test page for an upcoming exam, and am noticing some odd behavior. I created a variable $type and set it to 0. I then create a card, and within that card create a radio button group and a button. For the radiogroup -OnChange, I have a switch statement that sets the $type variable based on the $Body value. Within the radiogroup -OnChange, a UDToast shows what I would expect; the $type variable 1, 2, or 3 depending on the radio selected. However, if I do a button element directly after the radio button group (same card), the $type variable goes back to 0. I have declared the $type variable at the page level, as well as trying it at the card level, but it appears that not to be passing between elements. I read through the docs on variables, and don’t see anything regarding scoping that explains this. Am I missing something stupid simple?

New-UDRadioGroup -Id "radioGroup" -Label "radioGroup" -Content {
	New-UDRadio -Label "Show all answers" -Value 'all'
	New-UDRadio -Label "Show incorrect answers only" -Value 'incorrect'
	New-UDRadio -Label "Practice Exam, show no answers" -Value 'practice'
} -OnChange {
	Switch ($Body) {
		{$_ -match "all"} {$type = 1}
		{$_ -match "incorrect"} {$type = 2}
		{$_ -match "practice"} {$type = 3}
	}
	Show-UDToast -Message $type -Duration 2000
}

New-UDButton -Id "btnBegin" -Text "Begin" -OnClick {
	Show-UDToast -Message $type -Duration 2000
}

Edit: I also noticed some differences in documentation. The help docs show using $Body, whereas the API docs on github show the exact same example but using $EventData. Not sure if one is ‘better’ than the other, but I confirmed both elicit the same behavior.

I did find I could do this, as sort of a kludge, so I guess it is a workaround. Still do not understand why $type doesn’t pass through.

New-UDButton -Id "btnBegin" -Text "Begin" -OnClick {
   $Element = (Get-UDElement -Id "radioGroup")
   Show-UDToast -Message $Element.Value -Duration 2000
}
Product: PowerShell Universal
Version: 3.9.4
1 Like

I think I have it. Apparently, even on the same page, I need to pass the variable with either the $Session or $Page custom scope. So, as I plan to use this variable throughout the pages, initializing as $Session:type gives me the expected behavior.

Yep. That’s correct. Any standard variables are really only read-only in PSU event handler blocks. Session and Page scopes persist across event handler blocks.

$Variable1 = '123'

New-UDButton -OnClick {
    # This variable can be used but is really just scoped to OnClick
    Show-UDToast $Variable1
     
    # If you update it in the same block, then you will see the new value. 
    $Variable1 = 234
    Show-UDToast $Variable1

    # If you click this button again, it will show 123 and then 234 since the
    # value only persists for the current execution of the event handler
}

New-UDButton -OnClick {
    # Even if you clicked the first button and updated 
    # the variable, the value will still be 123 here
    Show-UDToast $Variable1
} 

1 Like