Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Trace.Assert Method (Boolean, String)

Checks for a condition, and displays a message if the condition is false.

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

[ConditionalAttribute("TRACE")] 
public static void Assert (
	bool condition,
	string message
)
/** @attribute ConditionalAttribute("TRACE") */ 
public static void Assert (
	boolean condition, 
	String message
)
ConditionalAttribute("TRACE") 
public static function Assert (
	condition : boolean, 
	message : String
)

Parameters

condition

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

message

A message to display.

Typically, you use Assert to identify logic errors during program development. Assert will evaluate the condition parameter. 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 to output 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 that corresponds 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>
    <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 that the type parameter is valid. If the type passed in is a null reference (Nothing in Visual Basic), the Assert outputs a message.

public static void MyMethod(Type type, Type baseType) {
    Trace.Assert(type != null, "Type parameter is null");
 
   // Perform some processing.
 }


public static void MyMethod(Type type, Type baseType)
{
    Trace.Assert(type != null, "Type parameter is null");

    // Perform some processing.
} //MyMethod 

import System
import System.Diagnostics

var type  // not initialized
Trace.Assert(type != null, "type is null")


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.