If you want to learn how to code and test a Windows form application, look no further. This article, the second of three parts, helps you brush up your coding skills. It is excerpted from chapter three of the book
Murach's C# 2005, written by Joel Murach (Murach, 2005; ISBN: 9781890774370).
How to detect and correct syntax errors
As you enter code, Visual Studio checks the syntax of each statement. If a
syntax error, or
build error, is detected, Visual Studio displays a wavy line under the code in the Code Editor. In the Code Editor in figure 3-6, for example, you can see the lines under txtPercent and txtAmount.
If you place the mouse pointer over the code in error, a brief description of the error is displayed. In this case, the error message indicates that the name does not exist. That’s because the names entered in the Code Editor don’t match the names used by the Form Designer. If the names are correct in the Form Designer, you can easily correct these errors, by editing the names in the Code Editor. In this figure, for example, the names of the text boxes should be txtDiscountPercent and txtDiscountAmount.
If the Error List window is open as shown in this figure, any errors that Visual Studio detects will also be displayed in that window. If the Error List window isn’t open, you can display it by pointing to the Error List tab that’s displayed on the lower
edge of Visual Studio. Then, you can jump to the error in the Code Editor by double-clicking on it in the Error List window.
When you’re first getting started with C#, you will inevitably encounter a lot of errors. As a result, you may want to use the Auto Hide button that’s available from the Error List window to keep it open all the time. This makes it easy to see errors as soon as they occur. Then, once you get the hang of working with C#, you can conserve screen space by using the Auto Hide button so this window is only displayed when you point to the Error List tab.
By the way, Visual Studio isn’t able to detect all syntax errors as you enter code. Instead, some syntax errors aren’t detected until the project is built. You’ll learn more about building projects later in this chapter.
The Code Editor and Error List windows with syntax errors displayed
Description - Visual Studio checks the syntax of your C# code as you enter it. If a syntax error (or build error) is detected, it’s highlighted with a wavy underline in the Code Editor, and you can place the mouse pointer over it to display a description of the error.
- If the Error List window is open, all of the build errors are listed in that window. Then, you can double-click on any error in the list to take you to its location in the Code Editor. When you correct the error, it’s removed from the error list.
- If the Error List window isn’t open, you can display it by selecting the Error List command from the View menu. Then, you can click the Error List tab that’s displayed at the edge of the Visual Studio window. If you prefer, you can click its Auto Hide button to keep it displayed.
- Visual Studio doesn’t detect some syntax errors until the project is built. As a result, you may encounter more syntax errors when you build and run the project.
More coding skills
At this point, you should understand the mechanics of adding code to a form. To code effectively, however, you’ll need some additional skills. The topics that follow present some of the most useful coding skills.
http://www.aspfree.com/c/a/C-Sharp/C...m-Application/