1 out of 2 rated this helpful - Rate this topic

Debug.Assert Method (Boolean)

.NET Framework 1.1

Checks for a condition and outputs the call stack if the condition is false.

[Visual Basic]
<Conditional("DEBUG")>
Overloads Public Shared Sub Assert( _
   ByVal condition As Boolean _
)
[C#]
[Conditional("DEBUG")]
public static void Assert(
 bool condition
);
[C++]
[Conditional("DEBUG")]
public: static void Assert(
 bool condition
);
[JScript]
public
   Conditional("DEBUG")
static function Assert(
 condition : Boolean
);

Parameters

condition
true to prevent a message being displayed; otherwise, false.

Remarks

Assert outputs the call stack with file and line numbers for each line in the call stack.

Typically, Assert is used to identify logic errors during program development. Assert evaluates the condition. If the result is false, it sends diagnostic messages to the Listeners.

The default behavior displays a message box when the application runs in user-interface mode, and outputs the message to the default trace output. You can customize this behavior by adding a TraceListener to, or removing one from, the Listeners collection.

To set an assert, you can also edit the configuration file corresponding to the name of your application. Within this file, you can enable and disable the assert or set the name of its log file. The configuration file should be formatted like the following example:

<configuration>
    <system.diagnostics>
       <assert assertuienabled="true" logfilename="c:\\myFile.log" />
    </system.diagnostics>
 </configuration>

Example

The following example creates an index for an array. Then some action is performed that sets the value of the index. Next the code calls Assert to confirm the index value is valid. If it is not valid, Assert outputs the call stack.

[Visual Basic] 
' Create an index for an array.
Dim index As Integer

' Perform some action that sets the index.
' Test that the index value is valid. 
Debug.Assert((index > - 1))

[C#] 
// Create an index for an array.
 int index;
 
 // Perform some action that sets the index.
 
 // Test that the index value is valid. 
 Debug.Assert(index > -1);   

[C++] 
#using <mscorlib.dll>
#using <System.dll>

using namespace System;
using namespace System::Diagnostics;

int main(void)
{
    // Create a local value
    int index;

    // Perform some action that sets the local.
    index = -40;

    // Test that the local value is valid. 
    Debug::Assert(index > -1);   

    return 0;
}

[JScript] 
// Create an index for an array.
 var index : int;
 
 // Perform some action that sets the index.
 index = 10;
 
 // Test that the index value is valid. 
 Debug.Assert(index > -1);   

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

Debug Class | Debug Members | System.Diagnostics Namespace | Debug.Assert Overload List | Debug | Trace | BooleanSwitch | TraceSwitch | TraceListener | DefaultTraceListener | ConditionalAttribute | ConditionalAttribute | ConditionalAttribute

Did you find this helpful?
(1500 characters remaining)