This documentation is archived and is not being maintained.

Trace.Assert Method (Boolean, String)

.NET Framework 1.1

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

[Visual Basic]
<Conditional("TRACE")>
Overloads Public Shared Sub Assert( _
   ByVal condition As Boolean, _
   ByVal message As String _
)
[C#]
[Conditional("TRACE")]
public static void Assert(
 bool condition,
 string message
);
[C++]
[Conditional("TRACE")]
public: static void Assert(
 bool condition,
 String* message
);
[JScript]
public
   Conditional("TRACE")
static function Assert(
 condition : Boolean,
 message : String
);

Parameters

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

Remarks

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>

Example

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.

[Visual Basic] 
Public Shared Sub MyMethod(type As Type, baseType As Type)
    Trace.Assert( Not (type Is Nothing), "Type parameter is null")

    ' Perform some processing.
End Sub


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


[C++] 
public:
   static void MyMethod(Type* type, Type* baseType) {
      Trace::Assert(type != 0, S"Type parameter is null");

      // Perform some processing.
   }

[JScript] 
import System
import System.Diagnostics

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

Requirements

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

Trace Class | Trace Members | System.Diagnostics Namespace | Trace.Assert Overload List | Debug | Trace | BooleanSwitch | TraceSwitch | TraceListener | DefaultTraceListener | ConditionalAttribute

Show: