TESTING AND DEBUGGING in C#

Painting Data Matrix ECC200 in C# TESTING AND DEBUGGING

CHAPTER 38 TESTING AND DEBUGGING
Generate DataMatrix In C#
Using Barcode drawer for .NET Control to generate, create Data Matrix ECC200 image in VS .NET applications.
www.OnBarcode.com
Decode Data Matrix ECC200 In C#.NET
Using Barcode decoder for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
Making Assertions
Barcode Creator In C#
Using Barcode drawer for VS .NET Control to generate, create Barcode image in VS .NET applications.
www.OnBarcode.com
PDF-417 2d Barcode Generator In C#.NET
Using Barcode creation for .NET Control to generate, create PDF-417 2d barcode image in .NET applications.
www.OnBarcode.com
We can use the Debug class to test for conditions like these. Listing 38-2 shows the static Debug.Assert method applied to the CalculateDivision method. Listing 38-2. Applying the Debug.Assert Method to a Method using System.Diagnostics; class Calculator { public int CalculateSum(int x, int y) { return x + y; } public int CalculateProduct(int x, int y) { return x * y; } public int CalculateSubtraction(int x, int y) { return x - y; } public int CalculateDivision(int x, int y) { Debug.Assert(y != 0, "Second parameter is zero"); return x / y; } } This version of the Assert method takes a bool parameter and a string parameter. The bool is used to evaluate a condition that you require to be true for your program to work as designed or intended. In the listing, I have asserted that the parameter y should not be zero. If the condition evaluates to true, that is, the value of y is not zero, then the program continues as normal. If the condition evaluates to false, that is, y is zero, then execution of the program is stopped and a dialog box appears, displaying the string that was the second parameter to the Assert method, as shown in Figure 38-1.
Generate Matrix In Visual C#
Using Barcode drawer for Visual Studio .NET Control to generate, create 2D image in .NET applications.
www.OnBarcode.com
Generate Barcode In Visual C#.NET
Using Barcode printer for .NET framework Control to generate, create Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
CHAPTER 38 TESTING AND DEBUGGING
GS1 128 Generation In C#.NET
Using Barcode maker for .NET Control to generate, create GS1-128 image in VS .NET applications.
www.OnBarcode.com
USD-4 Drawer In Visual C#.NET
Using Barcode maker for VS .NET Control to generate, create ABC Codabar image in VS .NET applications.
www.OnBarcode.com
Figure 38-1. The dialog box shown by the Debug.Assert method The meaning of the three buttons is misleading. Clicking the Abort button will terminate your program. Clicking the Ignore button will continue the execution of your program. In the example, this means that a System.DivideByZero exception will be thrown. Clicking the Retry button will prompt you to choose a debugger to debug your program with, as shown in Figure 38-2.
Read Data Matrix 2d Barcode In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Data Matrix Generation In None
Using Barcode creation for Excel Control to generate, create ECC200 image in Excel applications.
www.OnBarcode.com
Figure 38-2. Selecting a debugger
Barcode Maker In None
Using Barcode generator for Software Control to generate, create Barcode image in Software applications.
www.OnBarcode.com
Code 3/9 Generator In Java
Using Barcode maker for BIRT Control to generate, create ANSI/AIM Code 39 image in BIRT applications.
www.OnBarcode.com
CHAPTER 38 TESTING AND DEBUGGING
Reading EAN / UCC - 14 In C#
Using Barcode recognizer for .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Barcode Decoder In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
If you already have Visual Studio open, you can start debugging, use that instance, or create a new instance debug with that one. Selecting the debugger you want to use and clicking the Yes button starts the debugging process.
Barcode Creator In None
Using Barcode generation for Office Word Control to generate, create Barcode image in Microsoft Word applications.
www.OnBarcode.com
Barcode Recognizer In VS .NET
Using Barcode decoder for .NET framework Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Disabling Debug Statements
QR Code JIS X 0510 Maker In None
Using Barcode encoder for Font Control to generate, create QR Code image in Font applications.
www.OnBarcode.com
EAN128 Printer In None
Using Barcode generator for Font Control to generate, create UCC-128 image in Font applications.
www.OnBarcode.com
Once we have added an Assert statement, we can test until we find where the zero value arises and then remove the Assert method call. It would be nice if we could leave the statement there. After all, if the zero value is a big deal, then we always want to know when it arises. But such a statement will provide a miniscule amount of drag on the performance of our program, and we don t want our users to see the dialog box that is displayed when a zero value does arise. Fortunately, we don t need to remove the Assert statement. It is removed for us when we compile our program for release. When you create a new project, Visual Studio configures it such that the Debug build mode is used. This includes all sorts of additional information that is useful for debugging your program. All that additional information is striped out when you switch to the Release mode, and so are the calls to the Debug class. To switch to the Release mode, select the Configuration Manager item from the Build menu. This will show the Configuration Manager dialog box, as shown by Figure 38-3. Click the item in the Configuration column for the project you want to change, and select Release from the drop-down list.
Printing DataMatrix In Java
Using Barcode creation for Java Control to generate, create Data Matrix ECC200 image in Java applications.
www.OnBarcode.com
Barcode Printer In None
Using Barcode creator for Microsoft Excel Control to generate, create Barcode image in Excel applications.
www.OnBarcode.com
Figure 38-3. Changing the build configuration The Assert call in the example is omitted from the program during compilation, even though the statement is still in the code file. This is similar to the conditional compilation we saw in 26 but doesn t require as much forethought. We still get the DivideByZero exception when we run the program changing the build setting simply removes the Debug calls and, sadly, doesn t fix the bugs. To change back to the Debug build mode, open the Configuration Manager dialog box again, change the setting for your project back to Debug, and rebuild your project. Changing the build setting also changes where your compiled program resides on the disk. In Debug mode, you can find the output of your project in the bin\Debug directory within your project folder. In Release mode, you will find the output in the bin\Release directory. When relying on the build mode to change the behavior of the program, by suppressing the Assert dialog boxes, for example, it is very important to make sure you ship the contents of the correct directory to your users.
Copyright © OnBarcode.com . All rights reserved.