Testing and Debugging a Windows Forms Application
(Page 1 of 6 )
This article shows you how to revise, test, and debug a Windows form application. The third of three parts, it is excerpted from chapter three of the book
Murach's C# 2005, written by Joel Murach (Murach, 2005; ISBN: 9781890774370).
How to refactor code
As you work on the code for an application, you will often find that you want to revise your code. For example, you may want to change a name that you’ve used to identify a variable in your code to make the name more meaningful and readable. However, if you change the name in one place, you need to change it throughout your code. This is known as refactoring, and Visual Studio 2005 includes new features that make it much easier to refactor your code.
Figure 3-11 shows how you can use Visual Studio to quickly and easily change the names that you use within your code. In this figure, for example, the first screen shows the Code Editor after the name of the subtotal variable has been changed from subtotal to total. Here, a bar appears under the last letter of the newly renamed total variable. Then, the second screen shows the smart tag menu that can be displayed by pointing at the bar and clicking on the drop-down arrow that becomes available.
At this point, you can select the first command to rename the variable throughout the application. Or, you can select the second command to preview the changes. If you select the second command, a Preview Changes dialog box will be displayed. Then, you can preview the changes and deselect any changes that you don’t want to make.
Although this figure just shows how to change a name that’s used by the code, you can also use Visual Studio’s refactoring features to modify the structure of your code by extracting methods, encapsulating fields, and so on. To do that, you often begin by selecting a block of code. Then, you can right-click on the code and select the appropriate refactoring command. Or, you can select the appropriate command from the Refactor menu.
If you already have experience with another object-oriented language, these refactoring features should make sense to you. If not, don’t worry. You’ll learn more about these features as you progress through this book.
The bar that appears under a renamed variable
decimal total = Convert.ToDecimal (txtSubtotal.Text);
The menu that’s available from the bar

The Preview Changes dialog box

Figure 3-11. How to refactor code
Description
- The process of revising and restructuring existing code is known as refactoring. Visual Studio 2005 provides many new features that make it easier to refactor your code.
- When you change a name that’s used in your code, Visual Studio displays a bar beneath the modified name. Then, you can display a smart tag menu by moving the mouse pointer over the bar, and you can click on the drop-down list to display a menu that contains the appropriate refactoring commands.
- You can also use Visual Studio’s refactoring features to modify the structure of your code by extracting methods, encapsulating fields, and so on. To do that, you often begin by selecting a block of code. Then, you can right-click on the code and select the appropriate command, or you can select the command from the Refactor menu.
- Some refactoring commands display a dialog box that lets you preview the changes before you make them. Then, you can deselect any changes that you don’t want to make.
Next: How to get help information >>
More C# Articles
More By Murach Publishing
|
This article is excerpted from chapter three of the book Murach's C# 2005, written by Joel Murach (Murach, 2005; ISBN: 9781890774370). Check it out today at your favorite bookstore. Buy this book now.
|
|