Introduction
to Delphi
What is Delphi?
- Delphi is a object-oriented
two-way rapid application design
(RAD) tool for Windows and .NET environments
- With it you can create simple applications within minutes
just by dragging objects onto a form and setting their
properties and writing a minimal amount of code in Object Pascal language in
the object's events, all is needed then is to click
"Run" from the menu and the syntax will be
checked and the application will be compiled into machine
code as an exe file with the same name as the name of the
project you have created.
What can Delphi create?
- a DOS console exe file that can run from the DOS command
line without Windows
- a Window's application exe file
- unlike other RAD tools such as Visual Basic or
PBuilder, this exe does not need any other files
to be deployed on the user's computer unless you
use either:
- database application:
- will need Borland Database Engine
to be installed unless using
MIDAS (will need a file called
DBClient.dll instead) or 3rd
party database tool
- Delphi packets to reduce the size of the
exe, in which case these packets must be
deployed too
- a Window's library dll file
- a Window's packet file which is a Delphi component
library that stores component code so that an exe file
can be reduced in size if needed
- a component that can be re-used:
- a native Delphi VCL component that can be used in
other Delphi applications or in Borland's C++
Builder
- an Active X control (OCX) which can be embedded
in other programs such as Visual Basic,
Powerbuilder, MS Word, MS Excel, Internet
Explorer (v3.01 and higher)
- an Active Form that can be used in a web browser
such as Internet Explorer (v3.01 and higher)
- an application server for n-tier client server databases
which may either be:
- a MIDAS server which allows one to place all the
database connections & engines including ODBC
configurations on an NT middle-tier server so
that client computer only needs a MIDAS client, a
dll called DBClient.dll and a single registration
of where to locate the MIDAS server
- this allows for the following advantages:
- minimal configuration of
end-user's computers esp. when
ODBC is used
- minimal network traffic as the
business logic can be placed on
the server
- a Microsoft Transaction Server (MTS) application
- a CORBA server
- a .NET application or ASP.NET web application:
- Delphi 7 onwards provide support for MS .NET v1.0 framework
- Delphi 2007 onwards provide support for MS .NET v2.0 framework
What
can't Delphi do?
- although it doesn't support true multiple inheritance as
C++ does, it overcomes this by its use of interfaces and
aggregation techniques which results in the advantages of
multiple inheritance without its down-side
- the only Window's programming that can't be accomplished
is the writing of 16bit Virtual Device Driver's (VxDs) as
these are usually written in assembler language and not
even in C!
- programming for non-Window's environments (eg. Mac, Unix,
Linux) other than via .NET
- creation of Windows CE applications for palm pilots, etc other than via
.NET
- most of the other functionality that is not provided
within the Delphi product can be created by 3rd parties
or yourself (eg. trigger handlers for actively indexed
SQL databases)
What does Object-Oriented mean?
- in the early days of programming, program code was
created procedurally (ie. in a series of lines with GOTO
statements to redirect the flow of the code) and was
difficult to track and detect errors in code as well as
almost impossible to make re-use of code
- object-oriented programming allows one to create objects
and then assign them properties (eg. color), methods (eg.
create) and events which respond to a user's action (eg.
onMouseClick)
- in Delphi, a large number of objects have already been
designed and ready for you to just drag and drop them
from the component palette onto your form. Despite this,
if you wish you can easily create new objects
(components) either from scratch or by modifying existing
objects, these can then be registered in Delphi and then
become available on the component palette for dropping
onto future applications.
What are the fundamentals of Object-Oriented
technology:
- true object oriented languages require the following
principle concepts:
- abstraction:
- inheritance:
- defines a relationship between object
classes such that one can create a
modified object class which inherits all
the properties and methods of an
ancestral class and then modifies or
overrides the inheritance of some of
them, perhaps adding new properties and
methods
- thus any changes to the ancestral class
will automatically be reflected and
accessible in any descendant classes
- this creates a hierarchy tree of objects,
in Delphi, the main common ancestor
classes (which themselves descend from
TObject class) to all objects are:
- TWinControl:
- a visual object that has
a window's handle and can
contain other objects,
and can receive
keyboard-based focus (eg.
by using tab key to jump
from one control to the
next)
- eg TEdit
- TCustomControl:
- adds a Paint method
allowing you to draw the
display of your control
eg. a clockface
- TGraphicControl:
- these save windows
resources but are for
components that don't
need to receive input
focus, don't need to
contain other objects and
don't need a handle (use
their parent window's
handle and canvas fields)
- eg. TLabel, TShape
- TComponent:
- non-visual components
which provide access to
other objects or
programming API
- eg. TTable, TTimer,
TOpenDialog
- unlike C++, Delphi doesn't support
multiple inheritance but improves on this
feature by its use of interfaces and
aggregation to provide the benefits but
not the cons of multiple inheritance such
as name clashes and virtual base classes
- encapsulation:
- all code related to an object is confined
within the code for an object
- in Delphi code can be placed in various
segments: private, protected, public
- polymorphism:
- a means by which objects can communicate
freely with each other
- example:
- to avoid writing a separate event
for each object, I can write a
single event that is called from
a variety of objects, I can use
their common ancestral properties
or methods:
- in a
TTable.BeforeClose(Sender:
TDataset) event to save
edits before closing
tables:
- if (Sender as
TDataset).state = dsEdit
then (Sender as
TDataset).post;
- this event could be
called from a TQuery
object as it also is a
descendant of TDataset,
note that the word Sender
is Delphi's way of naming
a variable for the object
that actually called the
event
- But wait there's more to Object Pascal than just
object-oriented principles:
- user-definable data types:
- subrange types
- enumerated types eg. DOW = (Sunday,
Monday, Tuesday) then DOW[1] is Sunday;
- set type eg. set defined as ['A'..'D'] is
equivalent to ['A', 'B', 'C', 'D']
- dynamic arrays
- function and method overloading
What
is meant by two-way?
- this means that you can create the full Delphi code for
an application in two ways, either:
- visually within the Delphi IDE (Independent
Design Environment) using the drag and drop
techniques, or,
- in a text editor such as notepad as all the code
for a project is within 2 file types:
- a .pas which is a ASCII file containing
Object Pascal code
- a .dfm which is a coded file containing
information about the visual aspects of a
form which requires it to be converted to
ASCII text for editing in Notepad
- NB. the file that controls a project or
application is the .dpr file
Why
Object Pascal?
- Object Pascal is a true object-oriented language (as is
C++ or Smalltalk, unlike Visual Basic and Powerscript)
giving it all the power and versatility of C++ but not
its complexity of use and not the limitations inherant in
Visual Basic or Powerscript
- it is worth noting here that Windows is designed in C++
which is then compiled to machine assembler code, but C++
is a much more difficult language to learn than Object
Pascal which derives its language from Pascal which has
been used for decades.
- as both C++ and OP are hybrid languages derived from a
non-object-oriented parent with which they strive to
maintain compatibility, there are a few trade-offs which
may be seen as a short-coming compared to pure
object-oriented languages such as SmallTalk and Eiffel
- eg. only methods that are explicitly declared as virtual
can exhibit polymorphic behaviour