Delphi Programming
Advertisement
Stub
This article is a stub.
Please help enhance the Delphi Programming Wiki by expanding it.

Overview

Object Pascal is a draft for an object oriented ANSI/ISO standard of the venerable Pascal programming language (circa 1989-1990). The draft never made it to a full standard, but some Apple dialects are pretty close to it.

The Object Pascal dialect used in Delphi was basically a strongly (and incompatibly) enhanced version of the proposals in this draft is the native language of Delphi. (e..g in the original Object Pascal, all methods are virtual automatically)

Name Issues

Technically speaking the developers of Delphi no longer call the language "Object Pascal" but consider it a variant of Object Pascal called "The Delphi Programming Language". This may be due to many non-ANSI standard changes they desired to include or possibly to make their extensions proprietary. Another reason might be more trademark related. But if it looks like pascal and quacks like pascal...

Free Pascal still calls the language Object Pascal, even though it is nearly 100% compatible to Delphi's version. Free Pascal is slowly also building a dialect closer to classical Object Pascal (for Mac Pascal compability). See also Objective Pascal

Coding in Pascal

Comments
{this is a comment}
(* this is another comment *)
// this is a comment up to the end of the line

Syntax Highlighting

To make it easier to read and write Pascal code, the Delphi editor has a feature called color syntax highlighting. Depending on the meaning in Pascal of the words you type in the editor, they are displayed using different colors.

Expressions and Operators

Operators and Precedence

Unary Operators (Highest Precedence)


  • '@' Address of the variable or function (returns a pointer)
  • 'not' Boolean or bitwise not

Multiplicative and Bitwise Operators


  • '*' Arithmetic multiplication or set intersection
  • '/' Floating-point division
  • 'div' Integer division
  • 'mod' Modulus (the remainder of integer division)
  • 'as' Allows a type-checked type conversion among at runtime (part of the RTTI support)
  • 'and' Boolean or bitwise and
  • 'shl' Bitwise left shift
  • 'shr' Bitwise right shift

Additive Operators


  • '+' Arithmetic addition, set union, string concatenation, pointer offset addition
  • '-' Arithmetic subtraction, set difference, pointer offset subtraction
  • 'or' Boolean or bitwise or
  • 'xor' Boolean or bitwise exclusive or

Relational and Comparison Operators (Lowest Precedence)


  • '=' Test whether equal
  • '<>' Test whether not equal
  • '<' Test whether less than
  • '>' Test whether greater than
  • '<=' Test whether less than or equal to, or a subset of a set
  • '>=' Test whether greater than or equal to, or a superset of a set
  • 'in' Test whether the item is a member of the set
  • 'is' Test whether object is type-compatible (another RTTI operator)

Set Operators

The set operators includes: union (+), difference (-), intersection (*), membership test (in), plus some relational operators. To add an element to a set, you can make the union of the set with another one that has only the element you need.

Here's a Delphi example related to font styles:

  • Style := Style + [fsBold];
  • Style := Style + [fsBold, fsItalic] - [fsUnderline];

Variables

Example:

 var
   Value: Integer;
   IsCorrect: Boolean;
   A, B: Char;
 begin
   Value := 10;
   IsCorrect := True;
 end;
 var
   Value: Integer = 10;
   Correct: Boolean = True;
 
 Note: This initialization technique works only for global variables, not for variables declared inside the scope of a
 procedure or method. 

Constants

When you declare a constant, the compiler can choose whether to assign a memory location to the constant, and save its value there, or to duplicate the actual value each time the constant is used. This second approach makes sense particularly for simple constants.

Example:

 const
   Thousand = 1000;
   Pi = 3.14;    
 const
   Thousand: Integer = 1000;

Resource String Constants

A string constant defined with the resourcestring directive is stored in the resources of the program, in a string table.

 resourcestring
   CompanyName = 'Delphi';
 procedure TForm1.Button1Click(Sender: TObject);
 begin
   ShowMessage (BookName);
 end;

The interesting aspect of this is that if you examine it with a resource explorer you'll see the new strings in the resources. This means that the strings are not part of the compiled code but stored in a separate area of the executable file.

The advantage of resources is in an efficient memory handling performed by Windows and in the possibility of localizing a program (translating the strings to a different language) without having to modify its source code.

Data Types

In Pascal there are several predefined data types, which can be divided into three groups: ordinal types, real types, and strings.

Ordinal Types

  Size                      Signed Range                                Unsigned Range
  8 bits                ShortInt(-128 to 127)                        Byte (0 to 255)
  16 bits               SmallInt (-32768 to 32767)                   Word (0 to 65,535)
  32 bits               LongInt (-2,147,483,648 to 2,147,483,647)    LongWord (0 to 4,294,967,295)
  64 bits               Int64	
  16/32 bits            Integer                                      Cardinal

Ordinal Types Routines

  Routine                                               Purpose
  • Dec              Decrements the variable passed as parameter, by one or by the value of the optional second parameter.
  • Inc              Increments the variable passed as parameter, by one or by the specified value. 
  • Odd              Returns True if the argument is an odd number. 
  • Pred             Returns the value before the argument in the order determined by the data type, the predecessor. 
  • Succ             Returns the value after the argument, the successor. 
  • Ord              Returns a number indicating the order of the argument within the set of values of the data type. 
  • Low              Returns the lowest value in the range of the ordinal type passed as its parameter. 
  • High             Returns the highest value in the range of the ordinal data type. 

{{[[[sghsgsf]]]}}

Advertisement