IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
Delphi
Search Delphi Group:

Advanced
-Collapse +Expand Delphi To/From
To/FromCODEGuides
-Collapse +Expand Delphi Store
PRESTWOODSTORE

Prestwood eMagazine

January Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingDelphi for W...Language Basics   Print This     
  From the March 2016 Issue of Prestwood eMag
 
Delphi Language Basics:
Delphi Comments (// or { ... } or (* ... *))
 
Posted 11 years ago on 11/4/2008 and updated 12/13/2008
Delphi Code Snippet:
 A flashcard from our Delphi Flashcards Library
 A code snippet from our Delphi Code Snippets Page
 Tags: Delphi , Comments

KB101505



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.

Syntax Example:
//This is a single line comment.
 
{
Multiple line
comment.
}
 
(*
This too is a
multiple line comment.
*)
 
{$IFDEF TEMPOUT}
//...code here
{$ENDIF}

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.

 

More Info


Comments

1 Comments.
Share a thought or comment...
Comment 1 of 3

Another form of a multiple line comment

(*
  some comments go here
*)

Speaking of comments:

// I am a Single line comment

Multiple levels of comments. You can nest comments like the example below

(*
  I am a comment
  {
    I am a comment within a comment
  }
*)

The hierarchy of comments:

(* comment *) = highest precedence
{ comment }
// comment

Contrl + / Shortcut

To mark or unmark a series of lines of code with a comment

  1. Highlight the lines of code you wish to comment out - hold down the shift key and move cursor up or down.
  2. Hit Ctrl + / and the lines will be commented out. If they already were commented out, they will no longer be commented

Before:

if (something) then
begin
  ShowMessage('I did it.');
end;

After:

//if (something) then
//begin
//  ShowMessage('I did it.');
//end;
Posted 11 years ago

Comment 2 of 3

Thanks Daniel. I added your info on (* *) to the multiple-line comment section and I combined the single line comment info with this post and deleted the single line comment. Originally I wanted to document single line comments from multiple line comments. In hindsight, that was a dumb idea.

Since compiler directives are also embedded in comments, I added a bit of introductory info about that too.

Posted 11 years ago

Comment 3 of 3

The following didn't work for me in Delphi 5 Enterprise version

To mark or unmark a series of lines of code with a comment

1) Highlight the lines of code you wish to comment out - hold down the shift key and move cursor up or down.

2) Hit Ctrl + / and the lines will be commented out. If they already were commented out, they will no longer be commented

Any ideas.

Thanks,

Charlie

Posted 11 years ago
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1212A1
Enter key:
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile


Linked Certification Question(s)

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.

Beginner

1 Beginner Level Question

Question #1: True or False?

You can add single line comments to your code using // and multiple line comments using either { } or (* *).

Answer:
  • True
  • False
  • Intermediate

    1 Intermediate Level Question

    Question #2: Multiple Choice

    What does the following line of code do?

    {$APPTYPE CONSOLE}
    Answer:
    1. 

    This is a comment so it does nothing.

    2. 

    The {$ } indicates this comment is actually a compiler directive. The $APPTYPE compiler directive is used in the project source file (.DPR) to tell the compiler what type of application to compile. In this case, a DOS-like console application. When left out, the default is {$APPTYPE GUI} which is a Windows forms based application.

    3. 

    This code will give you an error.

    4. 

    The {$ } indicates this comment is actually a compiler directive. The $APPTYPE compiler directive is used in the project source file (.DPR) to tell the compiler what type of application to compile. In this case, a Windows forms based application. When left out, the default is a DOS-like command application.

    5. 

    The {$ } indicates this comment is actually a compiler directive. The $APPTYPE compiler directive is used in the project source file (.DPR) to tell the compiler what type of application to compile. In this case, a Windows DLL. When left out, the default is a DOS-like command application.


     KB Article #101505 Counter
    38716
    Since 11/4/2008
    -
       Contact Us!
     
    Have a question? Need our services? Contact us now.
    --Mike Prestwood

    Call: 916-726-5675

    email: info@prestwood.com


    Go ahead!   Use Us! Call: 916-726-5675 


    ©1995-2019 Prestwood IT Solutions.   [Security & Privacy]