Basic Delphi Syntax
This topic provides an overview of Delphi syntax.
Comments
As a starting point you should know how to make comments. A comment is text that is ignored by Clarity and is simply for making a note about the code. This can be to explain how a complex piece of code works or perhaps give a reason for doing something unusual in the code. This helps both you and others after you who may come back to the code long after it was written to fix or modify it.
Don't make comments too long-winded - you don't have to explain every single step, just potentially confusing ones. It's also a good idea to add the date of the change and name or initials to the comment if you are making modifications to an existing report.
You can enclose a comment in curly braces. It is best to put comments at the end of a line of code, or on a line on their own. For example,
|
Value := 1; {This is a comment here, after a line of code} |
Variables
Variables need to be declared before they can be used. Use the var keyword to declare variables. The format is:
var variableName : type;
where type is one of the defined data types.
Semicolon and Layout
The semicolon is a statement separator. Every clause must end in a semicolon, with a few exceptions which are noted below. While you could put all your code on one line (the semicolons help Clarity know where one statement stops and the next one starts), it is not very practical to do so. Don't be afraid to give your code plenty of space, lay it out neatly, and indent properly. This helps with readability.
Delphi uses begin and end to create code blocks. A code block is a logical sequence of commands that occur one after the other. You need to use the begin and end with all major structures in Delphi when you need to do more than one command. A code block always ends in a semicolon, and each clause within the block (including embedded blocks) must end in a semicolon.
A procedure block (same for functions):
|
procedure MyProcedure(var inputvar: string); var a,b,c : integer; begin {Do some stuff with a,b,c,s and inputvar here} end; |
Notice that the variable declaration occurs before the procedure code block. In the code examples in this document, the procedure header, begin and end keywords are mostly omitted.
Operators
Operators are the symbols in your code that enable you to manipulate all types of data. The various types of operators that are most commonly used are explained below. Note the use of comments in this document is to explain the concepts, and commenting as verbosely is not usually necessary.
Assignment operators
To assign a value to a variable use the := operator.
Example:
|
Variable1.Value := 5; {assigning value of 5 to a variable component} Dbtext2.text := Master['Name']; {assigning a value of a database field to a dbtext component} |
Comparison operators
Comparison operators consist of = (equals), < (less than), > (greater than), <= (less than or equal to), >= (greater than or equal to), <> (not equal to). These are most often used in “if” statements.
Example:
|
If dr_trans['transtype'] = 1 then {Comparison used} |
Note: The := operator is used to assign a value to a variable. The = operator compares the values of two operands.
Logical operators
“AND” and “OR” logical operators are most commonly used as a part of an “if” statement or loop as demonstrated in the following two examples:
|
If (a = 2) AND (b < 3) then { ... Do something ... } If (a = 2) OR (b = 2) then { ... Do something ... } |
Note: If you have an “if” statement that makes multiple comparisons, make sure that you enclose each set of comparisons in parentheses, as shown above, or it may not work correctly.
Arithmetic operators
These are standard mathematical operators used in the same way you would on a calculator.
|
Operator |
Addition |
+ |
Subtraction |
- |
Multiplication |
* |
Division |
/ |
Modulus |
Mod |
Note: Because of Delphi's operator precedence, the order in which arithmetic operations are calculated is not necessarily the same as the order you type them, just like on a scientific calculator. You can use parentheses to group operations, and the parenthesised operations will happen first. While operator precedence is outside the scope of this document, two examples are used below to illustrate:
|
A := 1 + 2 * 3; { A is 7 after this statement, not 9! } A := (1 + 2) * 3; { A is 9 after this statement } |
String Concatenation Operator
Strings also have an operator, the concatenation operator, which is the same as arithmetic addition:
|
Text := 'Dear ' + Dr_Contacts['Firstname'] + ' ' + |
If you need extra text or more than one data field in a line, this is the best way to do it. It makes the report look a lot tidier than it would if you just dumped the fields next to each other.
Testing Conditions
Testing conditions allow you to give your code some “flow control”:
If Statements
An “if” statement enables you to determine whether certain conditions are met before executing some code. If statements don't need semicolons, but the statements after them (or blocks of statements) do.
Example:
|
If x = 4 then {no semicolon here} y := x; {there's a semicolon here because it ends the if statement; |
Use the begin and end keywords if you want to execute multiple lines of text when a given condition is true. This is called a “code block”.
Example:
|
If x = 6 then begin DoSomething; {semicolons after each of these lines in the block} DoSomethingElse; DoAnotherThing; end; {semicolon after the end keyword} |
You can combine multiple conditions using the “if … else” construct:
Example:
|
If x = 100 then Somefunction {note: NO SEMICOLON here for a single line of code!} else if x = 200 then begin Someotherfunction1; Someotherfunction2; end else begin Somethingelse1; Somethingelse2; End; {End of the if statements, so there's a semicolon here} |
Case Statements
A case statement provides a means for choosing one condition among many possibilities without needing lots of embedded “if…else” constructs. Case statements only work with numeric values though, not strings of text.
Example:
|
case Cr_Trans['Transtype'] of {Depending on this value,} 1: Text := 'Invoice'; {One of these 4 lines are run} 4: Text := 'Payment'; 5: Text := 'Adjustment'; else Text := ''; end; |