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 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-7, 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 using the View Error List command. 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 keep the Error List window 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 at the lower edge of the screen.

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

The Code Editor and Error List windows with syntax errors displayed



Figure 3-7 How to detect and correct syntax errors


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, if you want to hide this window, you can click its Auto Hide button.

  • 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.

Total Pages : 10 45678

comments