Export (0) Print
Expand All

Debug.Assert Method (Boolean, String, String)

Checks for a condition and displays both specified messages if the condition is false.

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

[ConditionalAttribute("DEBUG")] 
public static void Assert (
	bool condition,
	string message,
	string detailMessage
)
/** @attribute ConditionalAttribute("DEBUG") */ 
public static void Assert (
	boolean condition, 
	String message, 
	String detailMessage
)
ConditionalAttribute("DEBUG") 
public static function Assert (
	condition : boolean, 
	message : String, 
	detailMessage : String
)
Not applicable.

Parameters

condition

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

message

A message to display.

detailMessage

A detailed message to display.

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.

NoteNote:

The display of the message box is dependent 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()).

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>

The following example checks whether 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) {
    Debug.Assert(type != null, "Type parameter is null", 
       "Can't get object for null type");
 
   // Perform some processing.
 }


public static void MyMethod(Type type, Type baseType)
{
    Debug.Assert(type != null, "Type parameter is null", 
        "Can't get object for null type");
    // Perform some processing.
} //MyMethod

public static function MyMethod(type : Type, baseType : Type)
{
    @if(@DEBUG)
    Debug.Assert(type != null, "Type parameter is null", 
       "Can't get object for null type");
    @end
 
    // Perform some processing.
}

Windows 98, Windows Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2015 Microsoft