I.T. Discussion Community!
-Collapse +Expand
JavaScript
Search JavaScript Group:

Advanced
-Collapse +Expand JavaScript Store
   ► KBTo/From GuidesReferenceLanguage Basics  Print This    All Groups  

Comments (Cross Ref > Language Basics)

Comments

Languages Focus

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.

Access VBA:   ' or REM

Commenting Code
Access VBA, like all the VB-based languages, uses a single quote (') or the original class-style basic "REM" (most developers just use a quote). Access VBA does NOT have a multiple line comment.

Directives - #

Directives are sometimes called compiler or preprocessor directives. A # is used for directives within Access VBA code. Access VBA offers only an #If..then/#ElseIf/#Else directive.

Syntax Example:
'Single line comment.

REM Old school single line comment.

#If MyDirective Then
'...some code.
#End If

Multi-Line Comment Code Trick

You can simulate a multi-line comment with code which is useful especially if you wish to comment out a block of code.

For example:

If False Then
 '...code here skipped.
End If

Using Directives

Here is a simple example of using VB Classic's directive (change True to False to switch it).

#Const MyDirective = True

#If MyDirective Then
      MsgBox ("Yes")
#Else
      MsgBox ("No")
#End If

More Info

Code:  Access VBA Comments (' or REM)

ASP Classic:   ' or REM

Commenting Code
ASP Classic, like all the VB-based languages, uses a single quote (') or the original class-style basic "REM" (most developers just use a quote). ASP Classic does NOT have a multiple line comment.

Preprocessor Directives - @ and #
An @ is used for preprocessor directives within ASP code (within <% %>) and a # is used for HTML-style preprocessor directives.

Note: ASP Classic does not support VB Classic's #If directive.

Syntax Example:
'Single line comment.
REM Old school single line comment.

 

Common Preprocessor Directives include:

<%@LANGUAGE=VBScript%>
<!-- #Include File="includes.inc" -->

Multi-Line Comment Code Trick

You can simulate a multi-line comment with code which is useful especially if you wish to comment out a block of code.

For example:

If False Then
 '...code here skipped.
End If

More Info

Code:  ASP Classic Comments (' or REM)

C#:  "Multiple Line Comment" // or /* */

Commenting Code
C# uses "//" for a single line comment and /* */ for a multiple line comment.

Syntax Example:
//Single line comment.

/*
Multiple line
comment.
*/


More Info

C++:   // or /* ... */

Commenting Code
C++ uses "//" for a single line comment and /* */ for a multiple line comment.

Syntax Example:
//Single line comment in MS (not ANSI compliant so do NOT use).
/* ANSI compliant single line comment. */
/*
Multiple line
comment.
*/
  
/*
* This is another popular
* way to write multi-line
* comments.
*/

More Info

C++/CLI:   // or /* ... */

Commenting Code
Same as standard C++. C++ uses "//" for a single line comment and /* */ for a multiple line comment.

Syntax Example:
//Single line comment in MS (not ANSI compliant so do NOT use).
/* ANSI compliant single line comment. */
/*
Multiple line
comment.
*/
  
/*
* This is another popular
* way to write multi-line
* comments.
*/

More Info

Corel Paradox:   ; and { ... }

Commenting Code
ObjectPAL uses ; for a single line comment and { } for a multiple line comment.

Syntax Example:
;Single line comment.

{
Multiple line
comment.
}


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: Multiple Choice

Which answer best describes ObjectPAL comments?

Answer:
1. 

ObjectPAL uses ; for a single line comment but ObjectPAL does not support a multiple line comment.

;Single line comment.
2. 

ObjectPAL uses ; for a single line comment and { } for a multiple line comment.

;Single line comment.

{
Multiple line
comment.
}
3. 

ObjectPAL uses a quote (') for a single line comment and { } for a multiple line comment.

'Single line comment.

{
Multiple line
comment.
}
4. 

ObjectPAL uses // for a single line comment but ObjectPAL does not support a multiple line comment.

//Single line comment.
5. 

ObjectPAL uses ' for a single line comment and [ ] for a multiple line comment.

'Single line comment.

[
Multiple line
comment.
]

More Info

 

Delphi:   // or { ... } or (* ... *)

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.

 



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.

    More Info

    Code:  Delphi Comments (// or { ... } or (* ... *))

    Delphi Prism:   // or { ... } or (* ... *)

    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.

    Custom Compiler Directives

    In addition to using the many built-in compiler directives, you can define your own. Here is a simple example.

    {$Define MYDEBUGMODE}
    {$IfDef MYDEBUGMODE}
      MessageBox.Show("In debug mode.");
    {$Else}
      MessageBox.Show("Out of debug mode.");
    {$EndIf}
    {$UnDef MYDEBUGMODE}
    {$IfDef MYDEBUGMODE}
      MessageBox.Show("In debug mode.");
    {$Else}
      MessageBox.Show("Out of debug mode.");
    {$EndIf}


    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?

    In Prism, you can nest multiple line comments as in the following:

    {
    (*
    comment here
    *)
    }
      
    MessageBox.Show("Or, like this...");
    (*

    {

    comment here

    }

    *)

    Answer:
  • True
  • False
  • More Info

    Code:  Delphi Prism Comments (// or { ... } or (* ... *))

    Java:   // or /* ... */

    Commenting Code
    Java uses "//" for a single line comment and /* */ for a multiple line comment.

    Syntax Example:
    //Single line comment in MS (not ANSI compliant so do NOT use).
    /* ANSI compliant single line comment. */
    /*
    Multiple line
    comment.
    */
    /*
    * This is another popular way 
    * to write multi-line comments.
    */

    More Info

    JavaScript:   // or /* ... */

    Commenting Code
    JavaScript uses "//" for a single line comment and /* */ for a multiple line comment.

    Syntax Example:
    //This is a single line comment.

    /*
    Multiple line
    comment.
    */

    More Info

    Perl:   #

    Commenting Code
    Perl uses # for single line comments and Perl does NOT have a multiple line comment.

    Compiler Directives (A special comment.)

    Perl also uses compiler directives embedded in comments with #! as in:

    #!/usr/local/bin/perl -w
    Syntax Example:
    #This is a comment in Perl.

    More Info

    PHP:   # or // or /* ... */

    Commenting Code
    Use the multi-line to comment out large blocks of code and to write multiple line comments.

    Syntax Example:
    #This is a comment in PHP.

    //This is too!

    /*
    This is a multi-line
    comment.
    */

    More Info

    VB Classic:   ' or REM

    Commenting Code
    VB Classic, like all the VB-based languages, uses a single quote (') or the original class-style basic "REM" (most developers just use a quote). VB Classic does NOT have a multiple line comment.

    Directives - #

    Directives are sometimes called compiler or preprocessor directives. A # is used for directives within VB Classic code. VB Classic offers only an #If..then/#ElseIf/#Else directive.

    Syntax Example:
    'Single line comment.

    REM Old school single line comment.

    #If MyDirective Then
    '...some code.
    #End If

     

    Multi-Line Comment Code Trick

    You can simulate a multi-line comment with code which is useful especially if you wish to comment out a block of code.

    For example:

    If False Then
     '...code here skipped.
    End If

    Using Directives

    Here is a simple example of using VB Classic's directive (change True to False to switch it).

    #Const MyDirective = True

    #If MyDirective Then
       MsgBox ("Yes")
    #Else
       MsgBox ("No")
    #End If

    More Info

    Code:  VB Classic Comments (' or REM)

    VB.Net:   ' or REM

    Commenting Code
    VB.Net, like all the VB-based languages, uses a single quote (') or the original class-style basic "REM" (most developers just use a quote). VB.Net does NOT have a multiple line comment.

    Syntax Example:
    'Single line comment.

    REM Old school single line comment.

     


    Multi-Line Comment Code Trick

    You can simulate a multi-line comment with code which is useful especially if you wish to comment out a block of code.

    For example:

    If False Then
     '...code here skipped.
    End If


    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

    2 Beginner Level Questions

    Question #1: True or False?

    The VB.Net multiple line comment is {} as in:

    {
      This is a multiple 
      line comment.
    }
    Answer:
  • True
  • False
  • Question #2: True or False?

    The VB.Net single line comment is ' and REM as in the following examples:

    'Single line comment.
    REM Old school BASIC single line comment.
    Answer:
  • True
  • False
  • More Info

    Code:  VB.Net Comments (' or REM)





    Follow PrestwoodBoards on: 


    ©1995-2019 PrestwoodBoards  [Security & Privacy]
    Professional IT Services: Coding | Websites | Computer Tech