Question

Are the {} and // both for adding comments depending on size?

Sorry for the silly question, but when ever I try and google "{} // Delphi" I get links to Delphi.com and Wikipidia for random information.

Était-ce utile?

La solution

There are three ways to add comment to a delphi source file:

{ This can be a single line comment }

{   but it can also span multiple lines
}

// Single line comment

The single line comment ends at the end of the line. The other comments have their own end.

(* This also can be a single comment *)

(* And it also can span multiple lines
*)

{ Can be used to comment out code containing a (* comment *)
  // Or one of those 
}
(* Can be used to coment out code containing a { comment } 
   // Or one of those 
 *)

// And this can contain the (* Single *) version of the { other } comments.

There is no real difference. But some people reserve one comment style to (temporary) comment out code. Because you can't nest the same type but you can nest different types.

Trivia, the (* *) comment is included to support (real) old keyboards that had no { and }. You can also use (. .) for [].

Autres conseils

The first place to look at is the documentation.

DELPHI: Fundamental Syntactic Elements - Comments and Compiler Directives


Comments and Compiler Directives

Comments are ignored by the compiler, except when they function as separators (delimiting adjacent tokens) or compiler directives.

There are several ways to construct comments:

{ Text between left and right braces is a comment. }
(* Text between left-parenthesis-plus-asterisk and an 
 asterisk-plus-right-parenthesis is also a comment *)
// Text between double-slash and end of line is a comment.

Comments that are alike cannot be nested. For instance, (*{}*) will. This latter form is useful for commenting out sections of code that also contain comments.
Here are some recommendations about how and when to use the three types of comment characters:

  • Use the double-slash (//) for commenting out temporary changes made during development. You can use the Code Editor's convenient CTRL+/ (slash) mechanism to quickly insert the double-slash comment character while you are working.
  • Use the parenthesis-star (*...*) both for development comments and for commenting out a block of code that contains other comments. This comment character permits multiple lines of source, including other types of comments, to be removed from consideration by the compiler.
  • Use the braces ({}) for in-source documentation that you intend to remain with the code.

A comment that contains a dollar sign ($) immediately after the opening { or (* is a compiler directive. For example,

{$WARNINGS OFF}

tells the compiler not to generate warning messages.


Examples

// single line comment

WriteLn( { inline comment } 'hello' );

{ multi
  line
  comment }

correct me if I'm wrong, but I think this might help you along:

//This is a single line comment.

{
Multiple line
comment.
}

(*
This too is a
multiple line comment.
*)

{ } is the way to create BLOCK comments. Block comments are a feature that people demanded at the same time they demanded block-oriented languages like C and Pascal. Before that, people used line-oriented languages like BASIC, FORTRAN and COBOL, where the basic structural element of the language is the line, with no BEGIN-END blocks.

Line oriented languages had comment signifiers like "REM", "REMARK", "C", "COMMENT" or ; or : or ' People got very tired of putting 'C' in front of each line when they wanted to comment out a block of code. And they had to repunch each card if they wanted to do that -- or at best they had only line-editors, not screen-editors.

Once people got languages like C and Pascal that only had block comments, they realised that they really missed line-comments. That the reason line-comments were invented before block-comments was because line-comments were actually more useful than block-comments.

So line comments // were added back into C++, and from there into C and Delphi.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top