FREE BOOK

Chapter 3: How to code and test a Windows Forms application using C# 2008

Posted by Murach Free Book | Windows Forms December 10, 2008
In this chapter, you'll learn how to code and test a Windows Forms application. When you're done, you'll be able to develop simple applications of your own.

How to test a project

When you test a project, you run it and make sure the application works correctly. As you test your project, you should try every possible combination of input data and user actions to be certain that the project works correctly in every case. Figure 3-15 provides an overview of the testing process for C# applications.

To start, you should test the user interface. Make sure that each control is sized and positioned properly, that there are no spelling errors in any of the controls or in the form's title bar, and that the navigation features such as the tab order and access keys work properly.

Next, subject your application to a carefully thought-out sequence of valid test data. Make sure you test every combination of data that the project will handle. If, for example, the project calculates the discount at different values based on the value of the subtotal, use subtotals that fall within each range.

Finally, test the program to make sure that it properly handles invalid data entered by users. For example, type text information into text boxes that expect numeric data. Leave fields blank. Use negative numbers where they shouldn't be allowed. Remember that the goal of testing is to find all of the problems.

As you test your projects, you'll eventually encounter runtime errors. These errors, also known as exceptions, occur when C# encounters a problem that prevents a statement from being executed. If, for example, a user enters "ABC" into the Subtotal text box on the Invoice Total form, a runtime error will occur when the program tries to assign that value to a decimal variable.

When a runtime error occurs, Visual Studio breaks into the debugger and displays an Exception Assistant window like the one in this figure. Then, you can use the debugging tools that you'll be introduced to in the next figure to debug the error.

Runtime errors, though, should only occur when you're testing a program. Before an application is put into production, it should be coded and tested so all runtime errors are caught by the application and appropriate messages are displayed to the user. You'll learn how to do that in chapter 7 of this book.

The message that's displayed when a runtime error occurs



Figure 3-14 How to test a project

How to test a project

  1. Test the user interface. Visually check all the controls to make sure they are displayed properly with the correct text. Use the Tab key to make sure the tab order is set correctly, verify that the access keys work right, and make sure that the Enter and Esc keys work properly.

  2. Test valid input data. For example, enter data that you would expect a user to enter.

  3. Test invalid data or unexpected user actions. For example, leave required fields blank, enter text data into numeric input fields, and use negative numbers where they are not appropriate. Try everything you can think of to make the program fail.

Description

  • To test a project, you run the project to make sure it works properly no matter what combinations of valid or invalid data you enter or what sequence of controls you use.

  • If a statement in your application can't be executed, a runtime error, or exception, occurs. Then, if the exception isn't handled by your application, the statement that caused the exception is highlighted and an Exception Assistant window like the one above is displayed. At that point, you need to debug the application as explained in  the next figure.

How to debug runtime errors

When a runtime error occurs, Visual Studio enters break mode. In that mode, Visual Studio displays the Code Editor and highlights the statement that couldn't be executed, displays the Debug toolbar, and displays an Exception Assistant window box like the one shown in figure 3-15. This is designed to help you find the cause of the exception (the bug), and to debug the application by preventing the exception from occurring again or by handling the exception.

Often, you can figure out what caused the problem just by knowing what statement couldn't be executed, by reading the message displayed by the Exception Assistant, or by reading the troubleshooting tips displayed by the Exception Assistant. But sometimes, it helps to find out what the current values in some of the variables or properties in the program are.

To do that, you can place the mouse pointer over a variable or property in the code so a data tip is displayed as shown in figure 3-16. This tip displays the current value of the variable or property. You can do this with the Exception Assistant still open, or you can click on its Close button to close it. Either way, the application is still in break mode. In this figure, the data tip for the Text property of the txtSubtotal control is "$100", which shows that the user didn't enter valid numeric data.

Once you find the cause of a bug, you can correct it. Sometimes, you can do that in break mode and continue running the application. Often, though, you'll exit from break mode before fixing the code. To exit, you can click the Stop Debugging button in the Debug toolbar. Then, you can correct the code and test the application again.

For now, don't worry if you don't know how to correct the problem in this example. Instead, you can assume that the user will enter valid data. In chapter 7, though, you'll learn how to catch exceptions and validate all user entries for an application because that's what a professional application has to do. And in chapter 11, you'll learn a lot more about debugging.

How a project looks in break mode



Figure 3-15 How to debug runtime errors

Description

  • When an application encounters a runtime error, you need to fix the error. This is commonly referred to as debugging, and the error is commonly referred to as a bug.

  • When an application encounters a runtime error, it enters break mode. In break mode,  the Debug toolbar is displayed along with other windows that provide debugging features.

  • The information in the Exception Assistant window should give you an idea of what the error might be. You can also click on the links in the Troubleshooting Tips list to display more information in a Help window.

  • If you close the Exception Assistant window, the application remains in break mode. To display a data tip for a property or variable, move the mouse pointer over it in the C# code.

  • To exit break mode and end the application, click the Stop Debugging button in the Debug toolbar or press Shift+F5. Then, you can attempt to fix the error and run the application again.

  • You'll learn more about debugging and the Exception Assistant window in chapter 11

Total Pages : 10 678910

comments