Export (0) Print
Expand All

Debug.Assert Method (Boolean, String, String)

.NET Framework 1.1

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

[Visual Basic]
Overloads Public Shared Sub Assert( _
   ByVal condition As Boolean, _
   ByVal message As String, _
   ByVal detailMessage As String _
public static void Assert(
 bool condition,
 string message,
 string detailMessage
public: static void Assert(
 bool condition,
 String* message,
 String* detailMessage
static function Assert(
 condition : Boolean,
 message : String,
 detailMessage : String


true to prevent a message being displayed; otherwise, false.
A message to display.
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.

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:

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


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.

[Visual Basic] 
Public Shared Sub MyMethod(type As Type, baseType As Type)
    Debug.Assert( Not (type Is Nothing), "Type parameter is null", "Can't get object for null type")
    ' Perform some processing.
End Sub 'MyMethod

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.

#using <mscorlib.dll>
#using <System.dll>

using namespace System;
using namespace System::Diagnostics;

void SomeMethod(Object * obj, Type * type);

int main(void)
    SomeMethod(0, 0); 

    return 0;

void SomeMethod(Object * obj, Type * type)
    Debug::Assert(type, S"type paramater is null", S"Can't get object for null type");

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


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

© 2015 Microsoft