
.NET 1.1+Debugging Using Assertions (2)
Often, when debugging software, a problem manifested at one location in the code is caused much earlier, potentially making it difficult to isolate. By adding assertions to the code, assumptions can be checked and warnings issues when they are incorrect.
Adding Assertion Messages
You may have noticed that there is a blank area to the right error icon in the dialog box above. This area is reserved for additional messages that you can define within the assertion command. To add a single message, simply add a string parameter to the Assert method call. For example, try modifying the assertion in the ShowTimePlusMinutes method as follows:
Debug.Assert(minutes >= 0, "Minutes must be zero or more");
On triggering the assertion, this message will appear to the right of the icon, above the stack trace. You can add a second, more detailed message if you wish by using another string parameter. This appears below the first message. For example, try:
Debug.Assert(minutes >= 0, "Minutes must be zero or more"
, "The number of minutes was " + minutes);
5 October 2008