Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Assert Function Delphi
07-29-2010, 01:09 AM
Post: #1
Assert Function Delphi
This function is a bit advanced. It is used when you are testing/debugging your program in Delphi.Assert function is a debugging tool for testing if a certain conditions are always True. 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.
Syntax is like this :
Code:
function Assert(Condition:Boolean; [Message:String]);
Now let's test it. Open New Project and drag & drop a button on the form.
In the procedure of the button put this piece of code :
Code:
procedure TForm1.Button1Click(Sender: TObject);
var
  k:integer;
begin
   k:=10;
   assert(k<5,'k is bigger');
end;
Now run (compile) the program and click on the button.
You will get a dialog like this :
[Image: 71827_assertfunction.jpg]
That is because k is bigger than 5, k is 10.
And if condition is okay, program will continue as nothing happened.
You can use this for many tests !

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
07-29-2010, 02:08 AM
Post: #2
RE: Assert Function Delphi
Nice post... this can come really handy in testing and debugging program! Smile

Read rules Smile
[Image: legislator.png]
Find all posts by this user
Quote this message in a reply
07-29-2010, 05:45 AM
Post: #3
RE: Assert Function Delphi
Kind of like assert in C Big Grin Thanks for the share

"Character is determined more by the lack of certain experiences than by those one has had."
Friedrich Nietzsche
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: