1. Home
  2. Computing & Technology
  3. Delphi Programming
Glossary|Tips/Tricks|FREE App/VCL|Best'O'Net|Books|Link Back

Delphi Programming RTL Reference|By Category|Alphabetically|By Unit
 

Assert

unit
System
category
Miscellaneous routines

declaration
function AssertAssert(expr : Boolean [; const msg: string]);

description
Use Assert as a debugging tool to test that certain conditions are always True (as assumed).

Assert tests whether a Boolean expression expr is true. If not, Assert raises an EAssertionFailed exception. If a message string was passed to Assert, the exception object is created with that string (Exception.CreateFmt).
Assert provides an opportunity to intercept an unexpected condition and halt a program rather than allow execution to continue under unanticipated conditions.

example
{
A simple function that accepts a 
string and a delimiter char, splits a 
string into tokens (TStringList items)
delimited with a char value.

Usage:

procedure TForm1.Button1Click(Sender: TObject);
var
  A: TStringList;
begin
  A := TStringList.Create;
  try
    Split(' ', 'your delphi guide', A);
    ShowMessage(a[0]); //your
    ShowMessage(a[1]); //delphi
    ShowMessage(a[2]); //guide
  finally
    A.Free;
  end;
end;
}

procedure Split
  (const Delimiter: Char; 
   Input: string; 
   const Strings: TStrings);
begin
  Assert(Assigned(Strings));
  Strings.Clear;
  Strings.Delimiter := Delimiter;
  Strings.DelimitedText := Input;
end;

in real code
Before you start optimizing Delphi code
How to make sure that the Murphy's law: "Any program will expand to fill available memory" does not apply to your Delphi applications.

Since assertions are not usually used in final product version, Delphi compiler includes directives to disable the generation of assertion code:

$ASSERTIONS ON/OFF (long form)

see also
Assigned,


 Free Delphi code snippet inside every Delphi Newsletter!
Subscribe to the Newsletter
Name
Email

 Got some code to share? Got a question? Need some help?
Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2009 About.com, a part of The New York Times Company.

All rights reserved.