Checks for a condition; if the condition is false
, outputs two specified messages and displays a message box that shows the call stack.
public:
static void Assert(bool condition, System::String ^ message, System::String ^ detailMessage);
[System.Diagnostics.Conditional("TRACE")]
public static void Assert(bool condition, string? message, string? detailMessage);
[System.Diagnostics.Conditional("TRACE")]
public static void Assert(bool condition, string message, string detailMessage);
[<System.Diagnostics.Conditional("TRACE")>]
static member Assert : bool * string * string -> unit
Public Shared Sub Assert (condition As Boolean, message As String, detailMessage As String)
Parameters
The conditional expression to evaluate. If the condition is true
, the specified messages are not sent and the message box is not displayed.
The detailed message to send to the Listeners collection.
The following example checks to see that the type
parameter is valid. If the type
passed in is null
, the Assert outputs a message.
public static void MyMethod(Type type, Type baseType)
{
Trace.Assert(type != null, "Type parameter is null",
"Can't get object for null type");
// Perform some processing.
}
Public Shared Sub MyMethod(type As Type, baseType As Type)
Trace.Assert( Not (type Is Nothing), "Type parameter is null", _
"Can't get object for null type")
' Perform some processing.
End Sub
Remarks
Use the Trace.Assert method if you want to do assertions in release builds. The Debug.Assert method works only in debug builds. For more information, see Assertions in Managed Code.
Typically, the Assert(Boolean, String, String) method is used to identify logic errors during program development. Assert evaluates the condition. If the result is false
, it sends the specified diagnostic message and detailed message to the Listeners collection. You can customize this behavior by adding a TraceListener to, or removing one from, the Listeners collection.
When the application runs in user-interface mode, it displays a message box that shows the call stack with file and line numbers. The message box contains three buttons: Abort, Retry, and Ignore. Clicking the Abort button terminates the application. Clicking Retry sends you to the code in the debugger if your application is running in a debugger, or offers to open a debugger if it is not. Clicking Ignore continues with the next instruction in the code.
For .NET Framework apps, you can change the behavior of the DefaultTraceListener in the configuration file that corresponds to the name of your application. In this file, you can enable and disable the assert message box or set the DefaultTraceListener.LogFileName property. The configuration file should be formatted as follows:
<configuration>
<system.diagnostics>
<switches>
<add name="mySwitch" value="4"/>
</switches>
<trace autoflush="false" indentsize="4"/>
<assert assertuienabled="true" logfilename=".\TraceLog.txt"/>
</system.diagnostics>
</configuration>
See also
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4