Presentation is loading. Please wait.

Presentation is loading. Please wait.

Decisions and Debugging Part06dbg --- if/else, switch, validating data, and enhanced MessageBoxes.

Similar presentations


Presentation on theme: "Decisions and Debugging Part06dbg --- if/else, switch, validating data, and enhanced MessageBoxes."— Presentation transcript:

1 Decisions and Debugging Part06dbg --- if/else, switch, validating data, and enhanced MessageBoxes

2 2 if Any decision must involve some sort of condition. In the simplest of situations, we ask if two entities are equivalent. If the entities are equivalent we do something; if not, we do nothing. We use the equivalence operator (==) to make the comparison.

3 3 if Enclose the condition in parentheses. Enclose the dependent instructions—those that will execute if the comparison is true—in braces. Braces are optional if there is only a single dependent instruction.  SimpleIf  TwoInstructionIf

4 4 More Relational Operators OperatorAlgebraicDefinition >>Greater than <<Less than >=≥Greater than or equal to <=≤Less than or equal to ===Equal to !=≠Not equal to

5 5 if Comparisons can be made with all of the relational operators. If the condition/comparison is true the dependent instructions will be executed.  RelationalIfs

6 6 Nested if When you place an if within the dependent instructions of the first if, you have a nested if.  NestedIfs

7 7 if with Logical AND We can test two comparisons within the same condition parentheses by separating them with the && operator. If both of the comparisons are true, the dependent instructions will be executed.  LogicalAnd

8 8 if with Logical Inclusive OR Use the symbol || to separate two comparisons within the condition parentheses. If one, the other, or both of the comparisons are true, the dependent instructions will be executed.  LogicalOr

9 9 if with Logical Exclusive OR Use the symbol ^ to separate two comparisons within the condition parentheses. If one, and only one, of the two comparisons is true, the dependent instructions will be executed.  ExclusiveOr

10 10 else In previous examples we did nothing if the condition(s) were not true. else provides a set of dependent instructions to be executed if the comparisons of an if are not true. Include the dependent instructions within braces as with if.  SimpleIfElse  BetterExclusiveOr

11 11 if Nested in else Ask another question if the first answer is false. Enclose a second entire if structure within the braces of the else.  IfNestedInElse

12 12 Changing Case When comparing strings, the case of the characters is considered. To remove this complication, you can convert a string to all upper case or all lower case characters. The String class has ToUpper() and ToLower() methods for this.  CaseConversion

13 13 switch Any conditional logic can be portrayed with if and if…else structures. If extensive nesting is necessary, the code may become difficult to follow. switch provides a multiple selection structure that results in simplified code for the same task.

14 14 switch switch replaces if/else chain code like this: if (grade ==‘A’) else if (grade ==‘B’) else if (grade == ‘C’) … etc. With code like this: switch (grade) { case ‘A’: case ‘B’: case ‘C’: … etc. }

15 15 if or switch? Mutually exclusive alternatives can be coded 2 ways. The switch code may be easier to read than equivalent if/else chain.

16 16 switch Limitations Exact values to be matched are listed in the cases; ranges of values expressed with relational operators such as >, =, <= are not allowed. switch can only be used with variable values that are integer, char, or string; no floating point values may be compared.  Switch

17 17 Validating Data Some user entries (in TextBoxes) are used in calculations and/or are displayed in other controls, such as Labels. If a user entry is required, then you must test to make sure that the user did enter some information. Since user entries are stored in the Text property of the TextBox, test for an empty string.

18 18 User Did Not Supply Required Data If the user did not supply required data, alert user to the problem by displaying a informational MessageBox.  Required Entry

19 19 Calling one Event Handler Function in another Event Handler Often one event handler function should execute the code of a different event handler. Ex. The btnClear_Click handler function clears all Textbox and Label contents. The btnClearAll_Click handler should 1) reset summary variable values and 2) clear all Textbox and Label contents. Rather than copy/paste the identical code into second event handler, you could call btnClear_Click handler from btnClearAll handler.

20 20 More MessageBoxes We have used MessageBoxes with a string message, a string caption, a single OK button, and various informational icons. MessageBoxes may have more than 1 button. You may choose from the following button combinations:

21 21 MessageBox Buttons Choose a button combination by typing the third parameter of the MessageBox as MessageBoxButtons. and Intellisense will list the available buttons. To make one of the buttons the default button (can be activated by pressing Enter), the 5 th parameter of the MessageBox will be MessageBoxDefaultButton and Intellisense will display Button1, Button 2, etc, corresponding to the Buttons you chose.

22 22 Capturing User Button Choice If you have chosen multiple buttons for your MessageBox, you will want to test to see which Button the user chose and then execute the appropriate code. To do this, you must 1) declare a variable of the DialogResult type and 2) assign the value returned by the MessageBox to this variable DialogResult response; //declare variable to store returned value response = MessageBox.Show(“message”, “caption”, MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); //default is Retry

23 23 Test User Response Then you test the value of the DialogResult variable if (response == DialogResult.Abort) //execute appropriate code else if (response == DialogResult.Retry) //execute appropriate code else //DialogResult.Cancel //execute appropriate code

24 24 Tracking Calculations with Debug The Debug object of the System.Diagnostics namespace has some useful methods for examining code while it is running. Running the WriteLine() method allows you to write information to the Output Window while the program is running.

25 25 Tracking Calculations with Debug This represents a very quick way to peek into a running program and view the actual contents of variables without having to show them with controls.  DebugWriteLine

26 26 Let’s Do a Problem Together Ch4ex4_6pg180


Download ppt "Decisions and Debugging Part06dbg --- if/else, switch, validating data, and enhanced MessageBoxes."

Similar presentations


Ads by Google