Hour 15. Debugging Your Code


What You'll Learn in This Hour:

  • Adding comments to your code

  • Identifying the two basic types of errors

  • Using the Command window

  • Using the Output window

  • Creating a structured error handler

No one writes perfect code. You're most certainly familiar with those problems that prevent code from executing properlythey're called bugs. Being new to Visual C#, your code will probably contain a fair number of bugs. As you gain proficiency, the number of bugs in your code will decrease but bugs will never disappear entirely. This book can't teach you how to debug every possible build or runtime error you might encounter; debugging is a skill and an art. In this hour, however, you will learn the basic skills necessary to trace and correct most bugs in your code.

Before proceeding, create a new Windows Application project named Debugging Example. Next, follow these steps to build the project:

1.

Right-click Form1.cs in the Solution Explorer, choose Rename, and then change the name of the form to frmDebuggingExample.cs. Next, set the form's Text property to Debugging Example (you will need to click the form once to access its design properties).

2.

Add a new text box to the form by double-clicking the TextBox item in the toolbox. Set the text box's properties as follows:

Property

Value

Name

txtInput

Location

88,112

Size

120,20


3.

Add a new button to the form by double-clicking the Button item in the tool-box and set its properties as follows:

Property

Value

Name

btnPerformDivision

Location

96,144

Size

104,23

Text

Perform Division


Your form should now look like the one shown in Figure 15.1.

Figure 15.1. This simple interface will help you learn debugging techniques.


This little project is going to divide 100 by whatever is entered into the text box. As you write the code to accomplish this, various bugs will be introduced on purpose, and you'll learn to correct them. Save your project now by clicking the Save All button on the toolbar.




Sams Teach Yourself Microsoft Visual C# 2005 in 24 Hours, Complete Starter Kit
Sams Teach Yourself Visual C# 2005 in 24 Hours, Complete Starter Kit
ISBN: 0672327406
EAN: 2147483647
Year: N/A
Pages: 248
Authors: James Foxall

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net