Friday, 9 November 2012

Delphi Programming Language Syntax

Delphi Programming Language Syntax

This article gives you basic information of delphi programming language syntax with an example.

01: unit Unit1;
02: interface
03: uses
03:  Windows, Messages, SysUtils, Variants, Classes,
03:  Graphics, Controls, Forms, Dialogs, StdCtrls;
04: type
05:   TForm1 = class(TForm)
06:     Edit1: TEdit;
07:     Button1: TButton;
08:     Label1: TLabel;
09:     procedure Button1Click(Sender: TObject);
10:   private
11:     { Private declarations }
12:   public
13:     { Public declarations }
14:   end;
15: var
16:   Form1: TForm1;
17: implementation
18: {$R *.dfm}
19: procedure TForm1.Button1Click(Sender: TObject);
20: var s: string;
21: begin
22:  s := 'Hello ' + Edit1.Text + ' Delphi welcomes you!';
23:  ShowMessage(s);
24: end;
25: end.

Unit: Defines the start of a unit file - a Delphi module. The unit name ( e.g. Unit1) must match the unit file name on a disk.

Example: unit myNameSpace;

Uses: Declares a list of Units(Namespaces) to be imported. The Uses  statement, if present, must be at the start.

Interface: It starts the definition of external interface of a Unit.  Declarations here are externally visible by other units. All of    these  declarations must be implemented in the Implementation section.

The interface section of a unit starts with the word interface and continues until the word implementation. This section is used to declare any public sections of code that appear in a unit. The entire contents of the interface section, including type, variable and procedure declarations, are visible to any other unit which uses this unit. When any other part of the program looks at a unit, all it sees is the interface section. Everything else is hidden, internal to the unit, part of the implementation. You could say that the interface section contains a list of items in the unit that other units can use.

In most cases the interface section will define several "subsections", you can see that the code for unit1.pas has a uses clause, a type section, and a variable declaration section.

Implementation: The implementation is where you write code that performs actions. This section is private to the unit, and can contain both declarations and code. The implementation section of a unit can contain its own uses clause as well.

Type: Defines a new category of variable or process

Class: Class name should start from T(Type) and variable name should start from F(Field). e.g. if class name is hello, then it  should be named as Thello.

Class declaration: Thello = class;

Inheritance: Thello = class(Tbuffallo). Here we are declaring a class  Thello which inherits Tbuffallo.

The implementation is where you write code that performs actions. This section is private to the unit, and can contain both declarations and code. The implementation section of a unit can contain its own uses clause as well.

Access Specifiers:

Private, Protected, Public, Published

Public: Constructor, Destructors and Properties should be made public.

Published: Published is just an extension of Public. As Delphi has a visual  IDE it needs a way to say what will get  displayed in the Object Inspector.  If you want to make any properties/events available to the user at  design-time, declare them as published.

Properties: Properties are data held by the component and Classes. You can read and write properties.

Declaration: property SomeProperty: Integer read FMyInteger write SetMyInteger;

Explanation: This defines a property "SomeProperty", of type Integer.  Note that properties are not actually variables in themselves, just ways of accessing other variables! Thus, the variable actually accessed with "SomeObject.SomeProperty" will be FMyInteger.  Now for the interesting bit: the read and write parts say how to access the data.  In this case, when the property is read (e.g. "if SomeObject.SomeProperty = 1") the value is taken directly from the variable, as if the code had said "SomeObject.FMyInteger".  However, when the property is set, it is not done directly!  The code actually calls a SetMyInteger procedure, which would be of the form:

procedure SetMyInteger(NewValue: Integer);
This gets called instead, as if the code had said "SomeObject.SetMyInteger(23)"


1. They allow you to say how data gets accessed, protecting it.
2. They save you from 50,000 "get/set" lines of code, helping readability.
3. They allow you to check data integrity before changing the variables.

Example 2:

property MQServer : string read getMQServer write setMQserver;

function TdmOsMetsMail.getMQServer: string;
  Result := FMQServer;
procedure TdmOsMetsMail.setMQserver(const Value: string);
  FMQServer := Value;

No comments:

Post a Comment