Learn DELPHI with Legolas | Part 1 06-27-2014, 05:23 PM
#1
The Delphi Programming Language
Hello [username] !
I decided to make some complete threads about Delphi (Object Pascal) because there is not something similar here and actually nowhere else, with which i will show you. I have the opinion that Delphi, can support big projects, like Java and C++.
And i have used it, in many of my projects, so it will be nice to provide you my knowledges. Actually, i love this language. The original Object Pascal doesn't really exist in a distinct form any more. Borland developed Delphi 1 and incorporated much of the Apple Object Pascal language. So the language for the Delphi product was originally named Object Pascal. Apple stopped developing Object Pascal and it was never standardised as had been originally intended.
For the release of Delphi 6, Borland chose to rename their language as Delphi.
![[Image: ug7zdac.png]](http://i.imgur.com/ug7zdac.png)
So, Pascal is a family of languages, with Object Pascal as sub-family, and Delphi a sub-family of Object Pascal.
________________________________________________________________________________________________________________
What is Delphi?
Delphi is a Windows application that based on object-oriented programming and the programming language Pascal. Anyone who has an experience in other object-oriented applications, such as Visual Basic, will find many similarities in the development of applications. Unlike with the older, "traditional" systems of application development, Delphi is driven by events (event driven), which means that it does not perform successive the commands of a program, from beginning to end, but awaits the appearance of events and executes the program code (procedure or function) associated with these events. An event is an action that comes from the user or by the system and examples of events are pressing (clicking) of the mouse, on a command button, the moving of the mouse over an object, the selection of an item in a menu etc.
History of Delphi
The Delphi programming language was developed by Borland and is the descendant of Turbo Pascal. Delphi was released in February 1995. Delphi is a native code compiler that runs under Window v3.1 or Windows '95.
Delphi is essentially object Pascal with similar programming tools found in Microsoft Visual Basic 3.0. Delphi is the first programming language to shatter the barrier between high-level, easy-to-use rapid application development environments and
low-level bits-and-bytes power tools.
On February 8, 2006 Borland announced that it was looking for a buyer for its IDE and database line of products, including Delphi, to concentrate on its ALM line.
On November 14, 2006 Borland transferred the development tools group to an independent subsidiary company named CodeGear, instead of selling it. Borland sold CodeGear to Embarcadero Technologies in 2008.
Embarcadero retained the CodeGear division created by Borland to identify its tool and database offerings, but identified its own database tools under the DatabaseGear name.
On September 12, 2013 Embarcadero released RAD Studio XE5, which includes Delphi XE5 and C++Builder. It added support for :
- Android
- Ice Cream Sandwich
- Jelly Bean
- iOS 7
Advantages of Delphi
- A structured programming language and syntax are clear, so easy to read, studied and understood .
- Clear Syntax.
- You can develop any type of applications.( Web applications, mulitmedia, mobile applications)
- Speed || Delphi isn't an interpreted language – it compiles to machine code, making it exceptionally fast.
- No runtime. Languages such as C# and VB.NET require the .NET runtime
- The language is suitable for Rapid Application Development (RAD) and Delphi includes an integrated IDE.
- Startup time. || Related to the above, well-designed Delphi applications start quickly because they don't have to wait for any runtime to load (assuming it's not already loaded).
- Ability to create easily powerful, scalable and secure database applications. ( Delphi has a powerful database development capabilities.)
- Provides native access to the high-performance Advantage Database Server.
- Delphi can access many types of databases.( local databases like: Paradox ,DBase, network SQL server databases, like SQL Server, Oracle and InterBase, and any data source accessible though ODBC)
- Users can build single-source applications for both Windows and MAC (iOS).
- Ability to create iOS mobile applications for the iPhone, iPod Touch and iPad.
- All the common parts of the graphical user interface, like forms, buttons and lists objects, are included in Delphi as components / controls.
- Low level features: ability to use third party DLLs, write assembler code blocks, or use Windows hooks.
- Support for the entire Windows API.
- With Delphi, you can write Windows programs more quickly and more easily than was possible ever before.
- Delphi allows the developer to design the entire interface visually, and quickly implement an event driven code with the click of the mouse.
- Delphi is not created by Microsoft.( Someone who had experience with Microsoft development tools, can understand why i am telling this as advantage :Thumbs-Up:

Disadvantages of Delphi
Delphi can almost be said to be perfect. If you work with Delphi, it's hard for you to change to other langauges, because it's so powerful. You will love it.:HeartEyes:
The biggest disadvantage of Delphi is that it's not so popular such as C, Java, Objective-C, so there are not many Delphi jobs. But on the other hand, there are also a lot more Java and C++ programmers than Delphi programmers.
So it's harder to stand out as a Java or C++ programmer. Having a specialism that not many people have, such as for example Delphi, may help you get a higher salary, though you'll have to search harder to find Delphi jobs.
This can be an advantage as well as a disadvantage. Delphi has been rather marginalized for some time now, and doesn't seem to be in the process of making a huge comeback.
It's definitely worth learning Delphi. A lot of softwares still being written in it, stuff which people use and rely on.
I would not recommend Delphi as a first language for someone who tries to learn programming on his own.
Compilers of Delphi
- Embarcadero Delphi XE6 || Latest Version || Recommended.
- Embarcadero Delphi XE5.
- Borland
- Oxygene || developed by RemObjects Software.
- Free Pascal (FPC) || open-source Pascal compiler.
- Project JEDI || Open Source project.
- Lazarus.
Identifiers of Delphi
- Reserved words cannot be used as identifiers.
- Identifiers denote constants, variables, fields, types, properties, procedures, functions, programs, units, libraries,
and packages.
- HackCommunity
- hackCommunity
- HACKCOMMUNITY
- hackcommunity
Spoiler:
If an identifier has been declared in more than one place, it is necessary to qualify the identifier. The syntax for a qualified identifier is:
identifier1.identifier2
Qualifiers can be iterated.. For example:
Form1.Button1.Click
calls the Click method in Button1 of Form1.
Comments in Delphi
- {you must not forget to close the comments, with the use of hooks }
- (* ..............................*)
- // These comments are only until the end of the current line.
Delphi reserved words
[table]
[row]
[cell] begin [/cell]
[cell] function [/cell]
[cell] string[/cell]
[cell]case [/cell]
[cell] except [/cell]
[cell] type [/cell]
[cell] and [/cell]
[cell]object [/cell]
[cell] finally [/cell]
[/row]
[row]
[cell] if [/cell]
[cell] const [/cell]
[cell] on [/cell]
[cell] or [/cell]
[cell] xor [/cell]
[cell] goto[/cell]
[cell] library [/cell]
[cell]div [/cell]
[cell]inline [/cell]
[/row]
[row]
[cell] constructor [/cell]
[cell] destructor [/cell]
[cell] procedure[/cell]
[cell] program [/cell]
[cell] uses [/cell]
[cell] do [/cell]
[cell] try[/cell]
[cell]asm [/cell]
[cell] packed [/cell]
[/row]
[row]
[cell] property [/cell]
[cell] initialization [/cell]
[cell] downto [/cell]
[cell] interface[/cell]
[cell] inherited [/cell]
[cell] class [/cell]
[cell] array [/cell]
[cell]shr [/cell]
[cell] as [/cell]
[/row]
[row]
[cell] raise [/cell]
[cell] while [/cell]
[cell] record [/cell]
[cell] var [/cell]
[cell] label [/cell]
[cell] in [/cell]
[cell] implementation [/cell]
[cell] nil[/cell]
[cell] end [/cell]
[/row]
[row]
[cell] with[/cell]
[cell] else [/cell]
[cell] is [/cell]
[cell] repeat [/cell]
[cell] until [/cell]
[cell] to [/cell]
[cell] unit [/cell]
[cell]shl [/cell]
[cell]------[/cell]
[/row]
[row]
[cell] exports[/cell]
[cell] set [/cell]
[cell] for[/cell]
[cell] not [/cell]
[cell] then [/cell]
[cell] of [/cell]
[cell] file [/cell]
[cell]mod [/cell]
[cell]------[/cell]
[/row]
[/table]
- Reserved words can’t be redefined.
- Reserved words appear in lowercase boldface throughout this manual. Object Pascal is not case sensitive, however, so you can use either uppercase or lowercase letters in your programs.
Delphi directives
[table]
[row]
[cell] absolute[/cell]
[cell] export[/cell]
[cell]abstract [/cell]
[cell] interrupt[/cell]
[cell] virtual[/cell]
[cell] resident[/cell]
[/row]
[row]
[cell] name[/cell]
[cell] published[/cell]
[cell]message[/cell]
[cell] index[/cell]
[cell] forward[/cell]
[cell]at [/cell]
[/row]
[row]
[cell]public [/cell]
[cell]external [/cell]
[cell]write [/cell]
[cell]override [/cell]
[cell] stored[/cell]
[cell]------- [/cell]
[/row]
[row]
[cell] near[/cell]
[cell]read [/cell]
[cell]protected [/cell]
[cell]nodefault [/cell]
[cell] cdecl[/cell]
[cell]------- [/cell]
[/row]
[row]
[cell]assembler [/cell]
[cell]far [/cell]
[cell]dynamic [/cell]
[cell]default [/cell]
[cell] private[/cell]
[cell]------- [/cell]
[/row]
[/table]
- Directives are used only in contexts where user-defined identifiers can’t occur. Unlike reserved words, you can redefine standard directives, but we advise that you don’t.
- private, protected, public, and published act as reserved words within object type declarations, but are otherwise treated as directives.
Datatypes
![[Image: TFGpoVZ.png]](http://i.imgur.com/TFGpoVZ.png)
- The Data types of Delphi are the same with the data types of Pascal.
Intro to Subroutines
Subroutines are an important part of any programming language. In Delphi, there are generally two types of subroutines: a function and a procedure.
The usual difference between a function and a procedure is that a function can return a value, and a procedure generally will not do so.
Code:
procedure SayHellotoHC(const sEveryone:string) ;
begin
ShowMessage('Hello ' + sEveryone) ;
end;Code:
function YearsOld(const BirthYear:integer): integer;
var
Year, Month, Day : Word;
begin
DecodeDate(Date, Year, Month, Day) ;
Result := Year - BirthYear;
end;We will talk more about Subroutines in other parts.
Creating simple applications in Delphi - Hello World
Code:
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage('Hello Hackcommunity');
Close;
end;Console Application:
Code:
program HelloHC; // In this line we declare, a program called HelloHC
{$APPTYPE CONSOLE} (* The directive {$APPTYPE CONSOLE} is telling to Compiler that is a console application and being run from the command line ! *)
var
MyMessage : string; // We declare a Variable which is called MyMessage and this variable holds a string.
begin
MyMessage := 'Hello Hackcommunity!'; (* In this line, the program assigns the string "Hello Hackcommunity" TO the variable MyMessage *)
Writeln(MyMessage); (* The Writeln procedure is a standard output. In this line, the program sends the contents of MyMessage to the Writeln procedure. *)
end.We saw before how the ShowMessage procedure displays messages in a dialog box.
There is the function InputBox( ), by which we can get information from the user. This function displays a dialog box that contains a title, a message, a text box and, if we desire, and a default value in the text box.
The return value of the function is what we will write in the text box.
The following program shows the use of InputBox( ) :
Code:
procedure TForm1.Button1Click(Sender: TObject);
var
Response : String;
begin
Response := InputBox('Hi', 'What's your name;', 'Legolas');
ShowMessage(‘Hi‘+Response);
Close;
{ The message Hi will display in the title bar of the dialog box, the message What's your name; will display inside the dialog box and Legolas will be the default value for the variable Response, if we do not write something in the text box.
Whatever we write in the text box will be considered as the return value of the function and will be inserted in the variable Response. }
end;Relax, i just showed you something to "take a taste" and get in the mood of Delphi.:Smile:. Let's take them again from beginning.:Thumbs-Up:
Constants
1.
Code:
const int j = 100;
const float ADNumb = 3.14;
const char * HackCommunity = “Skids go away!”;2.
Code:
Const
j = 100;
ADNumb = 3.14;
ErrorString = “Skids go away!”;1st and 2nd code, is the same thing. You decide how you will write it.
!!!<> The Delphi compiler automatically allocates proper space for the constant based on its value, or, in the case of scalar constants such as Integer, the compiler keeps track of the values as it works, and space never is allocated.
So, Delphi doesn’t require you to declare the constant’s type along with the value in the declaration.
!!!<> Delphi, gives you the ability to specify a constant’s type in the declaration.This provides you with full control over how the compiler treats your constants:
Code:
Const
j : Integer = 100;
ADNumb : Double = 3.14;
HackCommunity: string = “Skids go away!”;Error:
Code:
const
LeftSide : Integer = 10;
begin
LeftSide := 14;
end.This code produces a compile error on the assignment to the LeftSide variable in the begin...end block. To fix it, change the const declaration to a var declaration.
Statements and Variables
An assignment statement has the form:
Code:
variable := expressionCode:
//An assignment statement
I := 10; //assigns the value 10 to the variable I .Code:
J := J + 1;Example of ther assignment statements:
Code:
Z := X + Y;
Sum := Sum - 1;
String[I] := 'A';
F := Sqr(Z) - I * K;
Array[I + 2] := P^;
Condition := True;
Result := [I, Succ(C)];
Shortint(MyChar) := 118;Compound Statements:
Code:
begin
Z := X;
X := Y;
X := Y;
end;Declaring Variables:
Code:
var I: Integer; //declares the variable I of type Integer.Code:
var X, Y: Real; //declares two variables X and Y of type Real.Consecutive variable declarations:
Code:
var
Value : Integer;
NewAdress : string;
Okay : boolean;
IwantMoney : Currency;
Something : variant;
A, B, C : char;
Ptr : Pointer;
F, L, K : Real;
M, Z, Z : Double;
Str : string;
Doub : Double;Assigning values to variables:
Code:
Str = 'Hello world'; // String type
Doub = 3.141579; //Double type
IwantMoney := 687,49; //Curreny type
Okay := True; //boolean type
NewAdress := 'Mordor'; //string typeWarning:
Code:
IwantMoney := 'one million';This is not allowed and we will get an error message. It's not allwed to assign string value to currency type variable.
As we saw before at Constants, a feature of Delphi is that it enables us to initialize global variables inside a var block.
Examples:
Code:
Var
Value: integer = 5;
Str: string = 'Hello world';
NewAdress: string = 'Mordor';A Comparison between C and Delphi:
Program in C:
Code:
void main(void)
{
int x = 6;
x++;
int y = 3;
float f;
y--;
char Legolas;
f = x + y/3
//......
}// End of MainProgram in Delphi:
Code:
Procedure Foo;
var
x, y: Integer;
f: Double;
Legolas: Char;
begin
x := 1;
inc(x); { Don't worry about this. It's a function that increases the number by 1 point forward. I will talk more about Functions on other Parts. }
y := 2;
//....
end;So what did you understand form this comparison ?
Click to see the answer :
Spoiler:
Delphi requires from user to declare all variables up front in their own section before he begins a procedure, function, or program.
Generally, in Delphi, you code must be a bit more structured.
Generally, in Delphi, you code must be a bit more structured.
At this point, i will stop the Part 1 of Delphi.
________________________________________________________________________________________________________________
In Part 2 and Part 3, i will talk about :
- Operations
- Overloading
- Strings
- Types
- Functions
- if - else // Decisions
- Loops
- Forms
- Arrays
- Records
- Pointers
- Data Structures
- Delphi Databases // Database Development
- Smart Techniques
And in Part 4 i will talk about Mobile Application Development with Delphi (iOS and Android).
So, we don't stop here. Stay tuned. More interesting threads are coming soon.
And don't forget, everything started from Pascal.:Thumbs-Up:
Thank you,
Legolas.
(This post was last modified: 08-29-2014, 04:53 PM by Pk2Global.)


![[+]](https://sinister.li/images/modern/collapse_collapsed.png)
![[Image: T4OUWZ1.png]](http://i.imgur.com/T4OUWZ1.png)
![[Image: OilyCostlyEwe.gif]](http://fat.gfycat.com/OilyCostlyEwe.gif)
![[Image: qJweLN6.jpg]](http://i.imgur.com/qJweLN6.jpg)
