Export (0) Print
Expand All

Trace.Assert Method (Boolean, String, String)

Updated: July 2009

Checks for a condition; if the condition is false, outputs two specified messages and displays a message box that shows the call stack.

Namespace:  System.Diagnostics
Assembly:  System (in System.dll)

'Declaration
<ConditionalAttribute("TRACE")> _
Public Shared Sub Assert ( _
	condition As Boolean, _
	message As String, _
	detailMessage As String _
)
'Usage
Dim condition As Boolean 
Dim message As String 
Dim detailMessage As String 

Trace.Assert(condition, message, detailMessage)

Parameters

condition
Type: System.Boolean

The conditional expression to evaluate. If the condition is true, the specified messages are not sent and the message box is not displayed.

message
Type: System.String

The message to send to the Listeners collection.

detailMessage
Type: System.String

The detailed message to send to the Listeners collection.

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.

NoteNote:

The display of the message box depends on the presence of the DefaultTraceListener. If the DefaultTraceListener is not in the Listeners collection, the message box is not displayed. The DefaultTraceListener can be removed by the <clear> Element for <listeners> for <trace>, the <remove> Element for <listeners> for <trace>, or by calling the Clear method on the Listeners property (System.Diagnostics.Trace.Listeners.Clear()).

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>

The following example checks to see whether the type parameter is valid. If type is Nothing, Assert outputs a message.

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Date

History

Reason

July 2009

Added detailed information.

Information enhancement.

Community Additions

ADD
Show:
© 2014 Microsoft