Languages Focus: Comments
Commenting code generally has three purposes: to document your code, for psuedo coding prior to coding, and to embed compiler directives. Most languages support both a single line comment and a multiple line comment. Some languages also use comments to give instructions to the compiler or interpreter.
Delphi Comments
Commenting Code
Delphi uses // for a single line comment and both {} and (**) for multiple line comments. Although you can nest different types of multiple line comments, it is recommended that you don't.
Compiler Directives - $
A special comment. Delphi compiler directives are in the form of {$DIRECTIVE}. Of interest for comments is using the $IFDEF compiler directive to remark out code.
Compiler Directives
In the $IFDEF example above, the TEMPOUT identifier is not defined, the $IFDEF evaluates to false and skips the code during compile. The code is never compiled.
Common Examples:
The following are some commonly used compiler directives you should be familiar with:
{$APPTYPE CONSOLE}
The $APPTYPE compiler directive is used in the project source file (.DPR) to tell the compiler to create a Win32 console application or a GUI application. When left out, the default is {$APPTYPE GUI}.
The following is a simple console application:
program Hello;
{$APPTYPE CONSOLE}
uses
SysUtils;
begin
WriteLn('Hello, World!')
end.
{$R *.res} and {$R *.DFM}
The $R*.res compiler directive is frequently found in .DPR source files and the $R *.dfm is found in form source units. The $R compiler directive specifies the name of a resource file to be included. The * indicates to include the base name of the file, no extension, it does not mean to include all files (it is not a wild card character).
{$E cpl}
The $E directive sets the extension of the executable.The following is the project source code from a basic Control Panel Applet. Notice the extension is changed to .cpl. Also note the header starts with library instead of with program. There are three types of headings: program, library, and package.
library Project3;
uses
CtlPanel,
AppletModule1 in 'AppletModule1.pas' {AppletModule1AppletModule: TAppletModule};
exports CPlApplet;
{$R *.RES}
{$E cpl}
begin
Application.Initialize;
Application.CreateForm(TAppletModule1AppletModule, AppletModule1AppletModule);
Application.Run;
end.
Custom Compiler Directives
In addition to using the many built-in compiler directives, you can define your own. Here is a simple example.
//Switch on debug mode.
{$Define MYDEBUGMODE}
{$IfDef MYDEBUGMODE}
ShowMessage('In debug mode.');
{$Else}
ShowMessage('Out of debug mode.');
{$EndIf}
//Switch off debug mode.
{$UnDef MYDEBUGMODE}
{$IfDef MYDEBUGMODE}
ShowMessage('In debug mode.');
{$Else}
ShowMessage('Out of debug mode.');
{$EndIf}
Notice the $IFDEF above does NOT use a Then. If you put one, it will work because Delphi for Win32 sees the Then as a comment. However, Delphi Prism does not and you will get an error.
The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.