Thursday, February 15, 2018

How to write a SWITCH statement in a UWP project?

SWITCH is a selection statement that selects a switch section among a number of switch sections based on a pattern match with the switch expression.


Switch.png

The default need not be the last case section, as it is evaluated last, no matter where it is found.
In the case constant (case sensitive).
  • constant is the value to test for, and it can be any of the following:
  • A bool literal, either true or false.
  • Any integral constant, such as an int, a long, or a byte. 
  • The name of a declared const variable.
  • An enumeration constant.
  • A char literal.
  • A string literal.
The following is a complete example in a UWP project called JSwitch:

In this example, the variable anim is set to 'bird'. It is case sensitive. Bird is not the same as bird. When the switch statement is run and it finds 'bird' in any of the case sections, it displays the corresponding case constant in the text box. Change 'anim' and run the app again, and the textbox displays the match.

The XAML page has just one TEXTBOX and a BUTTON. The button click runs the program MainPage.xaml.cs.

Here is the MainPage.xaml for the JSwitch project.


Here is the MainPage.xaml.cs code:



No comments: