Description |
The $Warnings compiler directive determines whether Delphi shows compilation warnings or not.
Warnings are very useful for pointing out potential or real code problems. You should always have warnings on, and ideally always correct your code so that there are no compilation warnings. It is all too easy to see warnings as irritations, but they are there for a real purpose.
|
|
Notes |
The default value is $Warnings On
$Warnings can be set on and off multiple times in your code.
|
|
Related commands |
$Hints |
|
Determines whether Delphi shows compilation hints |
|
|
|
|
Example code : Give a warning for failing to return a value from a function |
// Full Unit code. // ----------------------------------------------------------- // You must store this code in a unit called Unit1 with a form // called Form1 that has an OnCreate event called FormCreate. unit Unit1; interface uses SysUtils, Forms, Dialogs; type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); function GetValue : Integer; end; var Form1: TForm1; implementation {$R *.dfm} // Include form definitions procedure TForm1.FormCreate(Sender: TObject); var
i : Integer;
begin // Set warnings on
{$Warnings On}
// Set i to a known value
i := 234;
// Show the value at the start
ShowMessage('i = '+IntToStr(i));
// Call our badly performing function
i := GetValue;
// Show the value now
ShowMessage('i now = '+IntToStr(i));
end;
// A function that fails to return a value!
function TForm1.GetValue: Integer;
begin // Do nothing!
end; end.
|
Hide full unit code |
Warning message display :
[Warning] Unit1.pas[57]: Return value of function 'TForm1.GetValue' might be undefined
i = 234
i now = 12404492
|
|
Example code : No warning for failing to return a value from a function |
var
i : Integer;
begin // Set warnings off
{$Warnings Off}
// Set i to a known value
i := 234;
// Show the value at the start
ShowMessage('i = '+IntToStr(i));
// Call our badly performing function
i := GetValue;
// Show the value now
ShowMessage('i now = '+IntToStr(i));
end;
// A function that fails to return a value!
function TForm1.GetValue: Integer;
begin // Do nothing!
end;
|
Show full unit code |
Code gives no warning message
i = 234
i now = 12404492
|
|