Sei sulla pagina 1di 39

Delphi Programming Exam

http://docwiki.embarcadero.com/RADStudio/XE5/en/Parameters_(Delphi
!nstructions:
" #his e%am contains &' multiple choice (uestions.
" )ou ha*e +5 minutes to complete this e%am.
" ,ake sure -ou attempt all the (uestions.
" Do not use the browser back button while takin. this test.
" #he timer o/ the e%am will not stop once the e%am starts.
" )ou can continue the e%am e*en a/ter -ou ha*e disconnected /rom the !nternet be/ore the e%am
e%pires.
" ,odern web browsers impro*e the e%am e%perience.
" #he e%am (uestions are in En.lish lan.ua.e.
#opics 0o*ered:
" Delphi 1asics
" Delphi Ad*anced
" Data #-pes2 3ariables2 and 0onstants
" Delphi Procedures and 4unctions
" Delphi 0lasses and 5b6ects
+/78
Programs and Units (Delphi)
#his topic co*ers the o*erall structure o/ a Delphi application: the pro.ram header2 unit declaration
s-nta%2 and the uses clause.
" Di*ide lar.e pro.rams into modules that can be edited separatel-.
" 0reate libraries that -ou can share amon. pro.rams.
" Distribute libraries to other de*elopers without makin. the source code a*ailable.
Program Structure and Syntax
A complete2 e%ecutable Delphi application consists o/ multiple unit modules2 all tied to.ether b- a sin.le
source code module called a pro6ect /ile. !n traditional Pascal pro.rammin.2 all source code2 includin. the
main pro.ram2 is stored in .pas /iles. Embarcadero tools use the /ile e%tension .dprto desi.nate the main
pro.ram source module2 while most other source code resides in unit /iles ha*in. the
traditional .pas e%tension. #o build a pro6ect2 the compiler needs the pro6ect source /ile2 and either a
source /ile or a compiled unit /ile /or each unit.
9ote: Strictl- speakin.2 -ou need not e%plicitl- use an- units in a pro6ect2 but all pro.rams automaticall-
use the S-stem unit and the S-s!nitunit.
#he source code /ile /or an e%ecutable Delphi application contains:
" a pro.ram headin.2
" a uses clause (optional2 and
" a block o/ declarations and e%ecutable statements.
#he compiler2 and hence the !DE2 e%pect to /ind these three elements in a sin.le pro6ect (.dpr /ile.
The Program Heading
#he pro.ram headin. speci/ies a name /or the e%ecutable pro.ram. !t consists o/ the reser*ed
word pro.ram2 /ollowed b- a *alid identi/ier2 /ollowed b- a semicolon. 4or applications de*eloped usin.
Embarcadero tools2 the identi/ier must match the pro6ect source /ile name.
#he /ollowin. e%ample shows the pro6ect source /ile /or a pro.ram called Editor. Since the pro.ram is
called Editor2 this pro6ect /ile is called Editor.dpr.
program Editor;
uses Forms, REAbout, // An "About" box
REMain; // Main form
{$R *.res}
begin
Appi!ation."ite #$ %"ext Editor%;
Appi!ation.&reateForm'"MainForm, MainForm(;
Appi!ation.Run;
end.
#he /irst line contains the pro.ram headin.. #he uses clause in this e%ample speci/ies a dependenc- on
three additional units: 4orms2 REAbout2 and RE,ain. #he :R compiler directi*e links the pro6ect;s
resource /ile into the pro.ram. 4inall-2 the block o/ statements between the be.in and end ke-words are
e%ecuted when the pro.ram runs. #he pro6ect /ile2 like all Delphi source /iles2 ends with a period (not a
semicolon.
Delphi pro6ect /iles are usuall- short2 since most o/ a pro.ram;s lo.ic resides in its unit /iles. A Delphi
pro6ect /ile t-picall- contains onl- enou.h code to launch the application;s main window2 and start the
</78
e*ent processin. loop. Pro6ect /iles are .enerated and maintained automaticall- b- the !DE2 and it is
seldom necessar- to edit them manuall-.
!n standard Pascal2 a pro.ram headin. can include parameters a/ter the pro.ram name:
program &a!'input, output(;
Embarcadero;s Delphi i.nores these parameters.
!n RAD Studio2 the pro.ram headin. introduces its own namespace2 which is called the pro6ect de/ault
namespace.
The Program Uses Clause
#he uses clause lists those units that are incorporated into the pro.ram. #hese units ma- in turn
ha*e uses clauses o/ their own. 4or more in/ormation on the uses clause within a unit source /ile2 see =nit
Re/erences and the =ses 0lause2 below.
#he uses clause consists o/ the ke-word uses2 /ollowed b- a comma delimited list o/ units the pro6ect /ile
directl- depends on.
The Block
#he block contains a simple or structured statement that is e%ecuted when the pro.ram runs. !n
most pro.ram /iles2 the block consists o/ a compound statement bracketed between the reser*ed
words be.in and end2 whose component statements are simpl- method calls to the
pro6ect;s Application ob6ect. ,ost pro6ects ha*e a .lobal Application *ariable that holds an instance
o/3cl.4orms.#Application2 >eb.>eb1roker.#>ebApplication2 or 3cl.S*c,.r.#Ser*iceApplication. #he
block can also contain declarations o/ constants2 t-pes2 *ariables2 procedures2 and /unctions? these
declarations must precede the statement part o/ the block. 9ote that the end that represents the end o/ the
pro.ram source must be /ollowed b- a period (.:
begin
.
.
.
end.
Unit Structure and Syntax
A unit consists o/ t-pes (includin. classes2 constants2 *ariables2 and routines (/unctions and procedures.
Each unit is de/ined in its own source (.pas /ile.
A unit /ile be.ins with a unit headin.2 which is /ollowed b- the inter/ace ke-word. 4ollowin.
the inter/ace ke-word2 the uses clause speci/ies a list o/ unit dependencies. 9e%t comes
theimplementation section2 /ollowed b- the optional initiali@ation2 and /inali@ation sections. A skeleton
unit source /ile looks like this:
unit )nit*;
interfa!e
uses // +ist of unit dependen!ies goes ,ere...
// -nterfa!e se!tion goes ,ere
impementation
uses // +ist of unit dependen!ies goes ,ere...
// -mpementation of !ass met,ods, pro!edures, and fun!tions goes ,ere...
initiai.ation
// )nit initiai.ation !ode goes ,ere...
7/78
finai.ation
// )nit finai.ation !ode goes ,ere...
end.
#he unit must conclude with the reser*ed word end /ollowed b- a period.
The Unit Heading
#he unit headin. speci/ies the unit;s name. !t consists o/ the reser*ed word unit2 /ollowed b- a *alid
identi/ier2 /ollowed b- a semicolon. 4or applications de*eloped usin. Embarcadero tools2 the identi/ier
must match the unit /ile name. #hus2 the unit headin.:
unit MainForm;
would occur in a source /ile called ,ain4orm.pas2 and the /ile containin. the compiled unit would
be ,ain4orm.dcu. =nit names must be uni(ue within a pro6ect. E*en i/ their unit /iles are in di//erent
directories2 two units with the same name cannot be used in a sin.le pro.ram.
The nter!ace Section
#he inter/ace section o/ a unit be.ins with the reser*ed word inter/ace and continues until the be.innin.
o/ the implementation section. #he inter/ace section declares constants2 t-pes2 *ariables2 procedures2 and
/unctions that are a*ailable to clients. #hat is2 to other units or pro.rams that wish to use elements /rom
this unit. #hese entities are called public because code in other units can access them as i/ the- were
declared in the unit itsel/.
#he inter/ace declaration o/ a procedure or /unction includes onl- the routine;s si.nature. #hat is2 the
routine;s name2 parameters2 and return t-pe (/or /unctions. #he block containin. e%ecutable code /or the
procedure or /unction /ollows in the implementation section. #hus procedure and /unction declarations in
the inter/ace section work like /orward declarations.
#he inter/ace declaration /or a class must include declarations /or all class members: /ields2 properties2
procedures2 and /unctions.
#he inter/ace section can include its own uses clause2 which must appear immediatel- a/ter the
ke-word inter/ace.
The mplementation Section
#he implementation section o/ a unit be.ins with the reser*ed word implementation and continues until
the be.innin. o/ the initiali@ation section or2 i/ there is no initiali@ation section2 until the end o/ the unit.
#he implementation section de/ines procedures and /unctions that are declared in the inter/ace section.
>ithin the implementation section2 these procedures and /unctions ma- be de/ined and called in an-
order. )ou can omit parameter lists /rom public procedure and /unction headin.s when -ou de/ine them in
the implementation section? but i/ -ou include a parameter list2 it must match the declaration in
the inter/ace section e%actl-.
!n addition to de/initions o/ public procedures and /unctions2 the implementation section can declare
constants2 t-pes (includin. classes2 *ariables2 procedures2 and /unctions that are pri*ate to the unit. #hat
is2 unlike the inter/ace section2 entities declared in the implementation section are inaccessible to other
units.
#he implementation section can include its own uses clause2 which must appear immediatel- a/ter the
ke-word implementation. #he identi/iers declared within units speci/ied in theimplementation section are
onl- a*ailable /or use within the implementation section itsel/. )ou cannot re/er to such identi/iers in
the inter/ace section.
&/78
The nitiali"ation Section
#he initiali@ation section is optional. !t be.ins with the reser*ed word initiali@ation and continues until the
be.innin. o/ the /inali@ation section or2 i/ there is no /inali@ation section2 until the end o/ the unit.
#he initiali@ation section contains statements that are e%ecuted2 in the order in which the- appear2 on
pro.ram startAup. So2 /or e%ample2 i/ -ou ha*e de/ined data structures that need to be initiali@ed2 -ou can
do this in the initiali@ation section.
4or units in the inter/ace uses list2 the initiali@ation sections o/ the units used b- a client are e%ecuted in
the order in which the units appear in the client;s uses clause.
#he older Bbe.in ... end.B s-nta% still /unctions. 1asicall-2 the reser*ed word Bbe.inB can be used in place
o/ initiali@ation /ollowed b- @ero or more e%ecution statements. 0ode usin. the older Bbe.in ... end.B
s-nta% cannot speci/- a /inali@ation section. !n this case2 /inali@ation is accomplished b- pro*idin. a
procedure to the E%itProc *ariable. #his method is not recommended /or code .oin. /orward2 but -ou
mi.ht see it used in older source code.
The #inali"ation Section
#he /inali@ation section is optional and can appear onl- in units that ha*e an initiali@ation section.
#he /inali@ation section be.ins with the reser*ed word /inali@ation and continues until the end o/ the unit.
!t contains statements that are e%ecuted when the main pro.ram terminates (unless the Calt procedure is
used to terminate the pro.ram. =se the /inali@ation section to /ree resources that are allocated in
the initiali@ation section.
4inali@ation sections are e%ecuted in the opposite order /rom initiali@ation sections. 4or e%ample2 i/ -our
application initiali@es units A2 12 and 02 in that order2 it will /inali@e them in the order 02 12 and A.
5nce a unit;s initiali@ation code starts to e%ecute2 the correspondin. /inali@ation section is .uaranteed to
e%ecute when the application shuts down. #he /inali@ation section must there/ore be able to handle
incompletel- initiali@ed data2 since2 i/ a runtime error occurs2 the initiali@ation code mi.ht not e%ecute
completel-.
Unit $e!erences and the Uses Clause
A uses clause lists units used b- the pro.ram2 librar-2 or unit in which the clause appears. A uses clause
can occur in
" the pro6ect /ile /or a pro.ram2 or librar-
" the inter/ace section o/ a unit
" the implementation section o/ a unit
,ost pro6ect /iles contain a uses clause2 as do the inter/ace sections o/ most units.
#he implementation section o/ a unit can contain its own uses clause as well.
#he S-stem unit and the S-s!nit unit are used automaticall- b- e*er- application and cannot be listed
e%plicitl- in the uses clause. (S-stem implements routines /or /ile !/52 strin. handlin.2 /loatin. point
operations2 d-namic memor- allocation2 and so /orth. 5ther standard librar- units2 such as S-s=tils2 must
be e%plicitl- included in the uses clause. !n most cases2 all necessar- units are placed in the uses clause b-
the !DE2 as -ou add and remo*e units /rom -our pro6ect.
0ase Sensiti*it-: !n unit declarations and uses clauses2 unit names must match the /ile names in case. !n
other conte%ts (such as (uali/ied identi/iers2 unit names are case insensiti*e. #o a*oid problems with unit
re/erences2 re/er to the unit source /ile e%plicitl-:
uses M/)nit in "m/unit.pas";
!/ such an e%plicit re/erence appears in the pro6ect /ile2 other source /iles can re/er to the unit with a
simple uses clause that does not need to match case:
uses M/unit;
5/78
The Syntax o! a Uses Clause
A uses clause consists o/ the reser*ed word uses2 /ollowed b- one or more comma delimited unit names2
/ollowed b- a semicolon. E%amples:
uses Forms, Main;
uses
Forms,
Main;
uses 0indo1s, Messages, 2/s)tis, 2trings, &asses, )nit3, M/)nit;
!n the uses clause o/ a pro.ram or librar-2 an- unit name ma- be /ollowed b- the reser*ed word in and the
name o/ a source /ile2 with or without a director- path2 in sin.le (uotation marks? director- paths can be
absolute or relati*e. E%amples:
uses
0indo1s, Messages, 2/s)tis,
2trings in %&#4&asses42trings.pas%, &asses;
=se the ke-word in a/ter a unit name when -ou need to speci/- the unit;s source /ile. Since the !DE
e%pects unit names to match the names o/ the source /iles in which the- reside2 there is usuall- no reason
to do this. =sin. in is necessar- onl- when the location o/ the source /ile is unclear2 /or e%ample when:
" )ou ha*e used a source /ile that is in a di//erent director- /rom the pro6ect /ile2 and that director-
is not in the compiler;s search path.
" Di//erent directories in the compiler;s search path ha*e identicall- named units.
" )ou are compilin. a console application /rom the command line2 and -ou ha*e named a unit with
an identi/ier that doesn;t match the name o/ its source /ile.
#he compiler also relies on the in ... construction to determine which units are part o/ a pro6ect. 5nl- units
that appear in a pro6ect (.dpr /ile;s uses clause /ollowed b- in and a /ile name are considered to be part o/
the pro6ect? other units in the uses clause are used b- the pro6ect without belon.in. to it. #his distinction
has no e//ect on compilation2 but it a//ects !DE tools like thePro6ect ,ana.er.
!n the uses clause o/ a unit2 -ou cannot use in to tell the compiler where to /ind a source /ile. E*er- unit
must be in the compiler;s search path. ,oreo*er2 unit names must match the names o/ their source /iles.
%ultiple and ndirect Unit $e!erences
#he order in which units appear in the uses clause determines the order o/ their initiali@ation and a//ects
the wa- identi/iers are located b- the compiler. !/ two units declare a *ariable2 constant2 t-pe2 procedure2
or /unction with the same name2 the compiler uses the one /rom the unit listed last in the uses clause. (#o
access the identi/ier /rom the other unit2 -ou would ha*e to add a (uali/ier: =nit9ame.!denti/ier.
A uses clause need include onl- units used directl- b- the pro.ram or unit in which the clause appears.
#hat is2 i/ unit A re/erences constants2 t-pes2 *ariables2 procedures2 or /unctions that are declared in unit
12 then A must use 1 e%plicitl-. !/ 1 in turn re/erences identi/iers /rom unit 02 then A is indirectl-
dependent on 0? in this case2 0 needn;t be included in a uses clause in A2 but the compiler must still be
able to /ind both 1 and 0 in order to process A.
#he /ollowin. e%ample illustrates indirect dependenc-:
program 5rog;
uses )nit3;
!onst a $ b;
// ...
unit )nit3;
interfa!e
uses )nit*;
!onst b $ !;
// ...
unit )nit*;
D/78
interfa!e
!onst ! $ *;
// ...
!n this e%ample2 Pro. depends directl- on =nit<2 which depends directl- on =nit+. Cence Pro. is
indirectl- dependent on =nit+. 1ecause =nit+ does not appear in Pro.;s uses clause2 identi/iers declared
in =nit+ are not a*ailable to Pro..
#o compile a client module2 the compiler needs to locate all units that the client depends on2 directl- or
indirectl-. =nless the source code /or these units has chan.ed2 howe*er2 the compiler needs onl-
their .dcu /iles2 not their source (.pas /iles.
>hen a chan.e is made in the inter/ace section o/ a unit2 other units that depend on the chan.e must be
recompiled. 1ut when chan.es are made onl- in the implementation or other sections o/ a unit2 dependent
units don;t ha*e to be recompiled. #he compiler tracks these dependencies automaticall- and recompiles
units onl- when necessar-.
Circular Unit $e!erences
>hen units re/erence each other directl- or indirectl-2 the units are said to be mutuall- dependent. ,utual
dependencies are allowed as lon. as there are no circular paths connectin. the usesclause o/ one inter/ace
section to the uses clause o/ another. !n other words2 startin. /rom the inter/ace section o/ a unit2 it must
ne*er be possible to return to that unit b- /ollowin. re/erences throu.h inter/ace sections o/ other units.
4or a pattern o/ mutual dependencies to be *alid2 each circular re/erence path must lead throu.h
the uses clause o/ at least one implementation section.
!n the simplest case o/ two mutuall- dependent units2 this means that the units cannot list each other in
their inter/ace uses clauses. So the /ollowin. e%ample leads to a compilation error:
unit )nit*;
interfa!e
uses )nit3;
// ...
unit )nit3;
interfa!e
uses )nit*;
// ...
Cowe*er2 the two units can le.all- re/erence each other i/ one o/ the re/erences is mo*ed to the
implementation section:
unit )nit*;
interfa!e
uses )nit3;
// ...
unit )nit3;
interfa!e
//...
impementation
uses )nit*;
// ...
#o reduce the chance o/ circular re/erences2 it;s a .ood idea to list units in the implementation uses clause
whene*er possible. 5nl- when identi/iers /rom another unit are used in the inter/ace section is it
necessar- to list that unit in the inter/ace uses clause.

E/78
Classes and &'(ects (Delphi)
#his topic co*ers the /ollowin. material:
" Declaration s-nta% o/ classes
" !nheritance and scope
" 3isibilit- o/ class members
" 4orward declarations and mutuall- dependent classes
Class Types
A class2 or class t-pe2 de/ines a structure consistin. o/ /ields2 methods2 and properties. !nstances o/ a class
t-pe are called ob6ects. #he /ields2 methods2 and properties o/ a class are called its components or
members.
" A /ield is essentiall- a *ariable that is part o/ an ob6ect. Fike the /ields o/ a record2 a class; /ields
represent data items that e%ist in each instance o/ the class.
" A method is a procedure or /unction associated with a class. ,ost methods operate on ob6ects2
that is2 instances o/ a class. Some methods (called class methods operate on class t-pes
themsel*es.
" A propert- is an inter/ace to data associated with an ob6ect (o/ten stored in a /ield. Properties
ha*e access speci/iers2 which determine how their data is read and modi/ied. 4rom other parts o/
a pro.ram outside o/ the ob6ect itsel/2 a propert- appears in most respects like a /ield.
5b6ects are d-namicall- allocated blocks o/ memor- whose structure is determined b- their class t-pe.
Each ob6ect has a uni(ue cop- o/ e*er- /ield de/ined in the class2 but all instances o/ a class share the
same methods. 5b6ects are created and destro-ed b- special methods called constructors and destructors.
A *ariable o/ a class t-pe is actuall- a pointer that re/erences an ob6ect. Cence more than one *ariable can
re/er to the same ob6ect. Fike other pointers2 classAt-pe *ariables can hold the *alue nil. 1ut -ou don;t
ha*e to e%plicitl- dere/erence a classAt-pe *ariable to access the ob6ect it points to. 4or e%ample2
Some5b6ect.Si@e :G +'' assi.ns the *alue +'' to the Si@e propert- o/ the ob6ect re/erenced b-
Some5b6ect? -ou would not write this as Some5b6ectH.Si@e :G +''.
A class t-pe must be declared and .i*en a name be/ore it can be instantiated. ()ou cannot de/ine a class
t-pe within a *ariable declaration. Declare classes onl- in the outermost scope o/ a pro.ram or unit2 not
in a procedure or /unction declaration.
A class t-pe declaration has the /orm:
t/pe
!ass6ame $ !ass 7abstra!t 8 seaed9 'an!estor&ass(
member+ist
end;
where class9ame is an- *alid identi/ier2 the sealed or abstract ke-word is optional2 (ancestor0lass is
optional2 and memberFist declares membersIthat is2 /ields2 methods2 and propertiesIo/ the class. Cere
the Jabstract K sealedL s-nta% (the J L brackets and the K pipe between them is used to speci/- that onl- one
o/ the optional sealed or abstract ke-words can be used. ,eanin./ul is onl- the sealed or abstract
ke-word? the brackets and pipe s-mbols themsel*es should be deleted.
!/ -ou omit (ancestor0lass2 then the new class inherits directl- /rom the prede/ined S-stem.#5b6ect
class. !/ -ou include (ancestor0lass and memberFist is empt-2 -ou can omit end. A class t-pe declaration
can also include a list o/ inter/aces implemented b- the class? see !mplementin. !nter/aces.
!/ a class is marked sealed2 then it cannot be e%tended throu.h inheritance.
An entire class can be declared abstract e*en i/ it does not contain an- abstract *irtual methods.
M/78
9ote: Delphi allows instantiatin. a class declared abstract2 /or backward compatibilit-2 but this /eature
should not be used an-more.
A class cannot be both abstract and sealed.
,ethods appear in a class declaration as /unction or procedure headin.s2 with no bod-. De/inin.
declarations /or each method occur elsewhere in the pro.ram.
4or e%ample2 here is the declaration o/ the #,emor-Stream class /rom the 0lasses unit:
t/pe
"Memor/2tream $ !ass'"&ustomMemor/2tream(
pri:ate
F&apa!it/# +ongint;
pro!edure 2et&apa!it/'6e1&apa!it/# +ongint(;
prote!ted
fun!tion Reao!':ar 6e1&apa!it/# +ongint(# 5ointer; :irtua;
propert/ &apa!it/# +ongint read F&apa!it/ 1rite 2et&apa!it/;
pubi!
destru!tor ;estro/; o:erride;
pro!edure &ear;
pro!edure +oadFrom2tream'2tream# "2tream(;
pro!edure +oadFromFie'!onst Fie6ame# string(;
pro!edure 2et2i.e'6e12i.e# +ongint(; o:erride;
fun!tion 0rite'!onst <uffer; &ount# +ongint(# +ongint; o:erride;
end;
0lasses.#,emor-Stream descends /rom 0lasses.#0ustom,emor-Stream2 inheritin. most o/ its
members. 1ut it de/inesIor rede/inesIse*eral methods and properties2 includin. its destructor method2
Destro-. !ts constructor2 0reate2 is inherited without chan.e /rom S-stem.#5b6ect2 and so is not
redeclared. Each member is declared as pri*ate2 protected2 orpublic (this class has no published
members. #hese terms are e%plained below.
Ni*en this declaration2 -ou can create an instance o/ #,emor-Stream as /ollows:
:ar
stream# "Memor/2tream;

begin
stream #$ "Memor/2tream.&reate;
nheritance and Scope
>hen -ou declare a class2 -ou can speci/- its immediate ancestor. 4or e%ample:
t/pe "2ome&ontro $ !ass'"&ontro(;
declares a class called #Some0ontrol that descends /rom 3cl.0ontrols.#0ontrol. A class t-pe
automaticall- inherits all o/ the members /rom its immediate ancestor. Each class can declare new
members and can rede/ine inherited ones2 but a class cannot remo*e members de/ined in an ancestor.
Cence #Some0ontrol contains all o/ the members de/ined in 3cl.0ontrols.#0ontrol and in each o/ the
3cl.0ontrols.#0ontrol ancestors.
#he scope o/ a member;s identi/ier starts at the point where the member is declared2 continues to the end
o/ the class declaration2 and e%tends o*er all descendants o/ the class and the blocks o/ all methods
de/ined in the class and its descendants.
T&'(ect and TClass
#he S-stem.#5b6ect class2 declared in the S-stem unit2 is the ultimate ancestor o/ all other classes.
S-stem.#5b6ect de/ines onl- a hand/ul o/ methods2 includin. a basic constructor and destructor. !n
addition to S-stem.#5b6ect2 the S-stem unit declares the class re/erence t-pe S-stem.#0lass:
"&ass $ !ass of "=b>e!t;
!/ the declaration o/ a class t-pe does not speci/- an ancestor2 the class inherits directl- /rom
S-stem.#5b6ect. #hus:
8/78
t/pe "M/&ass $ !ass
...
end;
is e(ui*alent to:
t/pe "M/&ass $ !ass'"=b>e!t(
...
end;
#he latter /orm is recommended /or readabilit-.
Compati'ility o! Class Types
A class t-pe is assi.nmentAcompatible with its ancestors. Cence a *ariable o/ a class t-pe can re/erence an
instance o/ an- descendant t-pe. 4or e%ample2 .i*en the declarations:
t/pe
"Figure $ !ass'"=b>e!t(;
"Re!tange $ !ass'"Figure(;
"2?uare $ !ass'"Re!tange(;
:ar
Fig# "Figure;
the *ariable 4i. can be assi.ned *alues o/ t-pe #4i.ure2 #Rectan.le2 and #S(uare.
&'(ect Types
#he >in7< Delphi compiler allows an alternati*e s-nta% to class t-pes. )ou can declare ob6ect t-pes
usin. the s-nta%:
t/pe ob>e!t"/pe6ame $ ob>e!t 'an!estor=b>e!t"/pe(
member+ist
end;
where ob6ect#-pe9ame is an- *alid identi/ier2 (ancestor5b6ect#-pe is optional2 and memberFist declares
/ields2 methods2 and properties. !/ (ancestor5b6ect#-pe is omitted2 then the new t-pe has no ancestor.
5b6ect t-pes cannot ha*e published members.
Since ob6ect t-pes do not descend /rom S-stem.#5b6ect2 the- pro*ide no builtAin constructors2
destructors2 or other methods. )ou can create instances o/ an ob6ect t-pe usin. the 9ew procedure and
destro- them with the Dispose procedure2 or -ou can simpl- declare *ariables o/ an ob6ect t-pe2 6ust as
-ou would with records.
5b6ect t-pes are supported /or backward compatibilit- onl-. #heir use is not recommended on >in7<.
)isi'ility o! Class %em'ers
E*er- member o/ a class has an attribute called *isibilit-2 which is indicated b- one o/ the reser*ed words
pri*ate2 protected2 public2 published2 or automated. 4or e%ample2
pubis,ed propert/ &oor# "&oor read @et&oor 1rite 2et&oor;
declares a published propert- called 0olor. 3isibilit- determines where and how a member can be
accessed2 with pri*ate representin. the least accessibilit-2 protected representin. an intermediate le*el o/
accessibilit-2 and public2 published2 and automated representin. the .reatest accessibilit-.
!/ a member;s declaration appears without its own *isibilit- speci/ier2 the member has the same *isibilit-
as the one that precedes it. ,embers at the be.innin. o/ a class declaration that don;t ha*e a speci/ied
*isibilit- are b- de/ault published2 pro*ided the class is compiled in the O:,PQ state or is deri*ed /rom a
class compiled in the O:,PQ state? otherwise2 such members are public.
4or readabilit-2 it is best to or.ani@e a class declaration b- *isibilit-2 placin. all the pri*ate members
to.ether2 /ollowed b- all the protected members2 and so /orth. #his wa- each *isibilit- reser*ed word
appears at most once and marks the be.innin. o/ a new ;section; o/ the declaration. So a t-pical class
declaration should be like this:
+'/78
t/pe
"M/&ass $ !ass'"&ontro(
pri:ate
{ pri:ate de!arations ,ere }
prote!ted
{ prote!ted de!arations ,ere }
pubi!
{ pubi! de!arations ,ere }
pubis,ed
{ pubis,ed de!arations ,ere }
end;
)ou can increase the *isibilit- o/ a member in a descendent class b- redeclarin. it2 but -ou cannot
decrease its *isibilit-. 4or e%ample2 a protected propert- can be made public in a descendant2 but not
pri*ate. ,oreo*er2 published members cannot become public in a descendent class. 4or more in/ormation2
see Propert- 5*errides and Redeclarations.
Pri*ate+ Protected+ and Pu'lic %em'ers
A pri*ate member is in*isible outside o/ the unit or pro.ram where its class is declared. !n other words2 a
pri*ate method cannot be called /rom another module2 and a pri*ate /ield or propert- cannot be read or
written to /rom another module. 1- placin. related class declarations in the same module2 -ou can .i*e
the classes access to one another;s pri*ate members without makin. those members more widel-
accessible. 4or a member to be *isible onl- inside its class2 it needs to be declared strict pri*ate.
A protected member is *isible an-where in the module where its class is declared and /rom an-
descendent class2 re.ardless o/ the module where the descendent class appears. A protected method can be
called2 and a protected /ield or propert- read or written to2 /rom the de/inition o/ an- method belon.in. to
a class that descends /rom the one where the protected member is declared. ,embers that are intended /or
use onl- in the implementation o/ deri*ed classes are usuall- protected.
A public member is *isible where*er its class can be re/erenced.
Strict )isi'ility Speci!iers
!n addition to pri*ate and protected *isibilit- speci/iers2 the Delphi compiler supports additional *isibilit-
settin.s with .reater access constraints. #hese settin.s are strict pri*ate and strict protected*isibilit-.
#hese settin.s can be used in >in7< applications.
0lass members with strict pri*ate *isibilit- are accessible onl- within the class in which the- are declared.
#he- are not *isible to procedures or /unctions declared within the same unit. 0lass members with strict
protected *isibilit- are *isible within the class in which the- are declared2 and within an- descendent
class2 re.ardless o/ where it is declared. 4urthermore2 when instance members (those declared without the
class or class *ar ke-words are declared strict pri*ate or strict protected2 the- are inaccessible outside o/
the instance o/ a class in which the- appear. An instance o/ a class cannot access strict protected or strict
protected instance members in other instances o/ the same class.
Delphi;s traditional pri*ate *isibilit- speci/ier maps to the 0FR;s assembl- *isibilit-. Delphi;s protected
*isibilit- speci/ier maps to the 0FR;s assembl- or /amil- *isibilit-.
9ote: #he word strict is treated as a directi*e within the conte%t o/ a class declaration. >ithin a class
declaration -ou cannot declare a member named ;strict;2 but it is acceptable /or use outside o/ a class
declaration.
Pu'lished %em'ers
Published members ha*e the same *isibilit- as public members. #he di//erence is that runAtime t-pe
in/ormation (R##! is .enerated /or published members. R##! allows an application to (uer- the /ields
and properties o/ an ob6ect d-namicall- and to locate its methods. R##! is used to access the *alues o/
++/78
properties when sa*in. and loadin. /orm /iles2 to displa- properties in the 5b6ect !nspector2 and to
associate speci/ic methods (called e*ent handlers with speci/ic properties (called e*ents.
Published properties are restricted to certain data t-pes. 5rdinal2 strin.2 class2 inter/ace2 *ariant2 and
methodApointer t-pes can be published. So can set t-pes2 pro*ided the upper and lower bounds o/ the base
t-pe ha*e ordinal *alues /rom ' throu.h 7+. (!n other words2 the set must /it in a b-te2 word2 or double
word. An- real t-pe e%cept Real&M can be published. Properties o/ an arra- t-pe (as distinct /rom arra-
properties2 discussed below cannot be published.
Some properties2 althou.h publishable2 are not /ull- supported b- the streamin. s-stem. #hese include
properties o/ record t-pes2 arra- properties o/ all publishable t-pes2 and properties o/enumerated t-pes
that include anon-mous *alues. !/ -ou publish a propert- o/ this kind2 the 5b6ect !nspector will not
displa- it correctl-2 nor will the propert-;s *alue be preser*ed when ob6ects are streamed to disk.
All methods are publishable2 but a class cannot publish two or more o*erloaded methods with the same
name. 4ields can be published onl- i/ the- are o/ a class or inter/ace t-pe.
A class cannot ha*e published members unless it is compiled in the O:,PQ state or descends /rom a class
compiled in the O:,PQ state. ,ost classes with published members deri*e /rom 0lasses.#Persistent2
which is compiled in the O:,PQ state2 so it is seldom necessar- to use the :, directi*e.
9ote: !denti/iers that contain =nicode characters are not allowed in published sections o/ classes2 or in
t-pes used b- published members.
,utomated %em'ers (-in./ &nly)
Automated members ha*e the same *isibilit- as public members. #he di//erence is that Automation t-pe
in/ormation (re(uired /or Automation ser*ers is .enerated /or automated members. Automated members
t-picall- appear onl- in >in7< classes . #he automated reser*ed word is maintained /or backward
compatibilit-. #he #Auto5b6ect class in the 0om5b6 unit does not useautomated.
#he /ollowin. restrictions appl- to methods and properties declared as automated.
" #he t-pes o/ all properties2 arra- propert- parameters2 method parameters2 and /unction results
must be automatable. #he automatable t-pes are 1-te2 0urrenc-2 Real2 Double2 Fon.int2!nte.er2
Sin.le2 Smallint2 AnsiStrin.2 >ideStrin.2 #Date#ime2 3ariant2 5le3ariant2 >ord1ool2 and all
inter/ace t-pes.
" ,ethod declarations must use the de/ault re.ister callin. con*ention. #he- can be *irtual2 but not
d-namic.
" Propert- declarations can include access speci/iers (read and write but other speci/iers (inde%2
stored2 de/ault2 and node/ault are not allowed. Access speci/iers must list a method identi/ier that
uses the de/ault re.ister callin. con*ention? /ield identi/iers are not allowed.
" Propert- declarations must speci/- a t-pe. Propert- o*errides are not allowed.
#he declaration o/ an automated method or propert- can include a dispid directi*e. Speci/-in. an alread-
used !D in a dispid directi*e causes an error.
5n the >in7< plat/orm2 this directi*e must be /ollowed b- an inte.er constant that speci/ies an
Automation dispatch !D /or the member. 5therwise2 the compiler automaticall- assi.ns the member a
dispatch !D that is one lar.er than the lar.est dispatch !D used b- an- method or propert- in the class and
its ancestors. 4or more in/ormation about Automation (on >in7< onl-2 see Automation 5b6ects.
#or0ard Declarations and %utually Dependent Classes
!/ the declaration o/ a class t-pe ends with the word class and a semicolonIthat is2 i/ it has the /orm
t/pe !ass6ame $ !ass;
with no ancestor or class members listed a/ter the word class2 then it is a /orward declaration. A /orward
declaration must be resol*ed b- a de/inin. declaration o/ the same class within the same t-pe declaration
+</78
section. !n other words2 between a /orward declaration and its de/inin. declaration2 nothin. can occur
e%cept other t-pe declarations.
4orward declarations allow mutuall- dependent classes. 4or e%ample:
t/pe
"Figure $ !ass; // for1ard de!aration
";ra1ing $ !ass
Figure# "Figure;
// ...
end;

"Figure $ !ass // defining de!aration
;ra1ing# ";ra1ing;
// ...
end;
Do not con/use /orward declarations with complete declarations o/ t-pes that deri*e /rom S-stem.#5b6ect
without declarin. an- class members.
t/pe
"First&ass $ !ass; // t,is is a for1ard de!aration
"2e!ond&ass $ !ass // t,is is a !ompete !ass de!aration
end; //
"",ird&ass $ !ass'"=b>e!t(; // t,is is a !ompete !ass de!aration
,'out Types
A t-pe is essentiall- a name /or a kind o/ data. >hen -ou declare a *ariable -ou must speci/- its t-pe2
which determines the set o/ *alues the *ariable can hold and the operations that can be per/ormed on it.
E*er- e%pression returns data o/ a particular t-pe2 as does e*er- /unction. ,ost /unctions and procedures
re(uire parameters o/ speci/ic t-pes.
#he Delphi lan.ua.e is a ;stron.l- t-ped; lan.ua.e2 which means that it distin.uishes a *ariet- o/ data
t-pes and does not alwa-s allow -ou to substitute one t-pe /or another. #his is usuall- bene/icial because
it lets the compiler treat data intelli.entl- and *alidate -our code more thorou.hl-2 pre*entin. hardAtoA
dia.nose runtime errors. >hen -ou need .reater /le%ibilit-2 howe*er2 there are mechanisms to circum*ent
stron. t-pin.. #hese include t-pecastin.2 pointers2 *ariants2 *ariant parts in records2 and absolute
addressin. o/ *ariables.
#here are se*eral wa-s to cate.ori@e Delphi data t-pes:
" Some t-pes are prede/ined (or builtAin? the compiler reco.ni@es these automaticall-2 without the
need /or a declaration. Almost all o/ the t-pes documented in this lan.ua.e re/erence are
prede/ined. 5ther t-pes are created b- declaration? these include userAde/ined t-pes and the t-pes
de/ined in the product libraries.
" #-pes can be classi/ied as either /undamental or .eneral. #he ran.e and /ormat o/ a /undamental
t-pe is the same in all implementations o/ the Delphi lan.ua.e2 re.ardless o/ the underl-in. 0P=
and operatin. s-stem. #he ran.e and /ormat o/ a .eneral t-pe is plat/ormAspeci/ic and could *ar-
across di//erent implementations. ,ost prede/ined t-pes are /undamental2 but a hand/ul o/
inte.er2 character2 strin.2 and pointer t-pes are .eneral. !t;s a .ood idea to use .eneral t-pes when
possible2 since the- pro*ide optimal per/ormance and portabilit-. Cowe*er2 chan.es in stora.e
/ormat /rom one implementation o/ a .eneral t-pe to the ne%t could cause compatibilit- problems
A /or e%ample2 i/ -ou are streamin. content to a /ile as raw2 binar- data2 without t-pe and
*ersionin. in/ormation.
" #-pes can be classi/ied as simple2 strin.2 structured2 pointer2 procedural2 or *ariant. !n addition2
t-pe identi/iers themsel*es can be re.arded as belon.in. to a special ;t-pe; because the- can be
passed as parameters to certain /unctions (such as Ci.h2 Fow2 and Si@e5/.
" #-pes can be parameteri@ed2 or .eneric2 as well. #-pes can be .eneric in that the- are the basis o/
a structure or procedure that operates in concert with di//erent t-pes determined later. 4or more
in/ormation about .enerics or parameteri@ed t-pes2 see the Nenerics !nde%.
+7/78
#he outline below shows the ta%onom- o/ Delphi data t-pes:
" simple
" ordinal
" inte.er
" character
" 1oolean
" enumerated
" subran.e
" real
" strin.
" structured
" set
" arra-
" record
" /ile
" class
" class re/erence
" inter/ace
" pointer
" procedural
" 3ariant
" t-pe identi/ier
#he standard /unction Si@e5/ operates on all *ariables and t-pe identi/iers. !t returns an inte.er
representin. the amount o/ memor- (in b-tes re(uired to store data o/ the speci/ied t-pe. 4or
e%ample2Si@e5/(Fon.int returns &2 since a Fon.int *ariable uses /our b-tes o/ memor-.
#-pe declarations are illustrated in the topics that /ollow. 4or .eneral in/ormation about t-pe declarations2
see Declarin. #-pes.
Parameters (Delphi)
#his topic co*ers the /ollowin. items:
" Parameter semantics
" Strin. parameters
" Arra- parameters
" De/ault parameters
,'out Parameters
,ost procedure and /unction headers include a parameter list. 4or e%ample2 in the header:
fun!tion 5o1er'A# Rea; B# -nteger(# Rea;
the parameter list is (X: Real? ): !nte.er.
A parameter list is a se(uence o/ parameter declarations separated b- semicolons and enclosed in
parentheses. Each declaration is a commaAdelimited series o/ parameter names2 /ollowed in most cases b-
a colon and a t-pe identi/ier2 and in some cases b- the G s-mbol and a de/ault *alue. Parameter names
must be *alid identi/iers. An- declaration can be preceded b- *ar2 const2 orout. E%amples:
'A, B# Rea(
':ar 2# string; A# -nteger(
'C0nd# -nteger; "ext, &aption# 5&,ar; Fags# -nteger(
'!onst 5; -# -nteger(
+&/78
#he parameter list speci/ies the number2 order2 and t-pe o/ parameters that must be passed to the routine
when it is called. !/ a routine does not take an- parameters2 omit the identi/ier list and the parentheses in
its declaration:
pro!edure )pdateRe!ords;
begin
...
end;
>ithin the procedure or /unction bod-2 the parameter names (X and ) in the /irst e%ample can be used as
local *ariables. Do not redeclare the parameter names in the local declarations section o/ the procedure or
/unction bod-.
Parameter Semantics
Parameters are cate.ori@ed in se*eral wa-s:
" E*er- parameter is classi/ied as *alue2 *ariable2 constant2 or out. 3alue parameters are the de/ault?
the reser*ed words *ar2 const2 and out indicate *ariable2 constant2 and out parameters2
respecti*el-.
" 3alue parameters are alwa-s t-ped2 while constant2 *ariable2 and out parameters can be either
t-ped or unt-ped.
" Special rules appl- to arra- parameters.
4iles and instances o/ structured t-pes that contain /iles can be passed onl- as *ariable (*ar parameters.
)alue and )aria'le Parameters
,ost parameters are either *alue parameters (the de/ault or *ariable (*ar parameters. 3alue parameters
are passed b- *alue2 while *ariable parameters are passed b- re/erence. #o see what this means2 consider
the /ollowin. /unctions:
fun!tion ;oube</Daue'A# -nteger(# -nteger; // A is a :aue parameter
begin
A #$ A * 3;
Resut #$ A;
end;

fun!tion ;oube</Ref':ar A# -nteger(# -nteger; // A is a :ariabe parameter
begin
A #$ A * 3;
Resut #$ A;
end;
#hese /unctions return the same result2 but onl- the second one A Double1-Re/ can chan.e the *alue o/ a
*ariable passed to it. Suppose we call the /unctions like this:
:ar
-, E, D, 0# -nteger;
begin
- #$ F;
D #$ F;
E #$ ;oube</Daue'-(; // E $ G, - $ F
0 #$ ;oube</Ref'D(; // 0 $ G, D $ G
end;
A/ter this code e%ecutes2 the *ariable !2 which was passed to Double1-3alue2 has the same *alue we
initiall- assi.ned to it. 1ut the *ariable 32 which was passed to Double1-Re/2 has a di//erent *alue.
A *alue parameter acts like a local *ariable that .ets initiali@ed to the *alue passed in the procedure or
/unction call. !/ -ou pass a *ariable as a *alue parameter2 the procedure or /unction creates a cop- o/ it?
chan.es made to the cop- ha*e no e//ect on the ori.inal *ariable and are lost when pro.ram e%ecution
returns to the caller.
+5/78
A *ariable parameter2 on the other hand2 acts like a pointer rather than a cop-. 0han.es made to the
parameter within the bod- o/ a /unction or procedure persist a/ter pro.ram e%ecution returns to the caller
and the parameter name itsel/ has .one out o/ scope.
E*en i/ the same *ariable is passed in two or more *ar parameters2 no copies are made. #his is illustrated
in the /ollowin. e%ample:
pro!edure Add=ne':ar A, B# -nteger(;
begin
A #$ A H *;
B #$ B H *;
end;

:ar -# -nteger;
begin
- #$ *;
Add=ne'-, -(;
end;
A/ter this code e%ecutes2 the *alue o/ ! is 7.
!/ a routine;s declaration speci/ies a *ar parameter2 -ou must pass an assi.nable e%pression A that is2 a
*ariable2 t-ped constant (in the O:RPQ state2 dere/erenced pointer2 /ield2 or inde%ed *ariable to the routine
when -ou call it. #o use our pre*ious e%amples2 Double1-Re/(E produces an error2 althou.h
Double1-3alue(E is le.al.
!nde%es and pointer dere/erences passed in *ar parameters A /or e%ample2 Double1-Re/(,-Arra-J!L A are
e*aluated once2 be/ore e%ecution o/ the routine.
Constant Parameters
A constant (const parameter is like a local constant or readAonl- *ariable. 0onstant parameters are similar
to *alue parameters2 e%cept that -ou can;t assi.n a *alue to a constant parameter within the bod- o/ a
procedure or /unction2 nor can -ou pass one as a *ar parameter to another routine. (1ut when -ou pass an
ob6ect re/erence as a constant parameter2 -ou can still modi/- the ob6ect;s properties.
=sin. const allows the compiler to optimi@e code /or structured A and strin.At-pe parameters. !t also
pro*ides a sa/e.uard a.ainst unintentionall- passin. a parameter b- re/erence to another routine.
Cere2 /or e%ample2 is the header /or the 0ompareStr /unction in the S-s=tils unit:
fun!tion &ompare2tr'!onst 2*, 23# string(# -nteger;
1ecause S+ and S< are not modi/ied in the bod- o/ 0ompareStr2 the- can be declared as constant
parameters.
0onstant parameters ma- be passed to the /unction b- *alue or b- re/erence2 dependin. on the speci/ic
compiler used. #o /orce the compiler to pass a constant parameter b- re/erence2 -ou can use the JRe/L
decorator with the const ke-word.
#he /ollowin. e%ample shows how -ou can speci/- the JRe/L decorator either be/ore or a/ter the const
ke-word:
fun!tion Fun!tion6ame'!onst 7Ref9 parameter*# &ass*6ame; 7Ref9 !onst parameter3#
&ass36ame(;
&ut Parameters
An out parameter2 like a *ariable parameter2 is passed b- re/erence. >ith an out parameter2 howe*er2 the
initial *alue o/ the re/erenced *ariable is discarded b- the routine it is passed to. #he outparameter is /or
output onl-? that is2 it tells the /unction or procedure where to store output2 but doesn;t pro*ide an- input.
4or e%ample2 consider the procedure headin.:
pro!edure @et-nfo'out -nfo# 2omeRe!ord"/pe(;
>hen -ou call Net!n/o2 -ou must pass it a *ariable o/ t-pe SomeRecord#-pe:
:ar M/Re!ord# 2omeRe!ord"/pe;
...
@et-nfo'M/Re!ord(;
+D/78
1ut -ou;re not usin. ,-Record to pass an- data to the Net!n/o procedure? ,-Record is 6ust a container
where -ou want Net!n/o to store the in/ormation it .enerates. #he call to Net!n/o immediatel- /rees the
memor- used b- ,-Record2 be/ore pro.ram control passes to the procedure.
5ut parameters are /re(uentl- used with distributedAob6ect models like 05,. !n addition2 -ou should use
out parameters when -ou pass an uninitiali@ed *ariable to a /unction or procedure.
Untyped Parameters
)ou can omit t-pe speci/ications when declarin. *ar2 const2 and out parameters. (3alue parameters must
be t-ped. 4or e%ample:
pro!edure "aIeAn/t,ing'!onst &(;
declares a procedure called #akeAn-thin. that accepts a parameter o/ an- t-pe. >hen -ou call such a
routine2 -ou cannot pass it a numeral or unt-ped numeric constant.
>ithin a procedure or /unction bod-2 unt-ped parameters are incompatible with e*er- t-pe. #o operate on
an unt-ped parameter2 -ou must cast it. !n .eneral2 the compiler cannot *eri/- that operations on unt-ped
parameters are *alid.
#he /ollowin. e%ample uses unt-ped parameters in a /unction called E(ual that compares a speci/ied
number o/ b-tes o/ an- two *ariables:
fun!tion E?ua':ar 2our!e, ;est; 2i.e# -nteger(# <ooean;
t/pe
"</tes $ arra/7J..Max-nt K *9 of </te;
:ar
6 # -nteger;
begin
6 #$ J;
1,ie '6 L 2i.e( and '"</tes';est(769 $ "</tes'2our!e(769( do
-n!'6(;
E?ua #$ 6 $ 2i.e;
end;
Ni*en the declarations:
t/pe
"De!tor $ arra/7*..*J9 of -nteger;
"5oint $ re!ord
A, B# -nteger;
end;
:ar
De!*, De!3# "De!tor;
6# -nteger;
5# "5oint;
-ou could make the /ollowin. calls to E(ual:
E?ua'De!*, De!3, 2i.e=f'"De!tor((; // !ompare De!* to De!3
E?ua'De!*, De!3, 2i.e=f'-nteger( * 6(; // !ompare first 6
// eements of De!* and De!3
E?ua'De!*7*9, De!*7M9, 2i.e=f'-nteger( * N(; // !ompare first N to
// ast N eements of De!*
E?ua'De!*7*9, 5, F(; // !ompare De!*7*9 to 5.A
// and De!*739 to 5.B
String Parameters
>hen -ou declare routines that take shortAstrin. parameters2 -ou cannot include len.th speci/iers in the
parameter declarations. #hat is2 the /ollowin. declaration causes a compilation error:
pro!edure &,e!I'2# string73J9(; // s/ntax error
1ut the /ollowin. declaration is *alid:
t/pe "2tring3J $ string73J9;
pro!edure &,e!I'2# "2tring3J(;
+E/78
#he special identi/ier 5penStrin. can be used to declare routines that take shortAstrin. parameters o/
*ar-in. len.th:
pro!edure &,e!I'2# =pen2tring(;
>hen the O:CQ and O:PPQ compiler directi*es are both in e//ect2 the reser*ed word strin. is e(ui*alent to
5penStrin. in parameter declarations.
Short strin.s2 5penStrin.2 :C2 and :P are supported /or backward compatibilit- onl-. !n new code2 -ou
can a*oid these considerations b- usin. lon. strin.s.
,rray Parameters
>hen -ou declare routines that take arra- parameters2 -ou cannot include inde% t-pe speci/iers in the
parameter declarations. #hat is2 the declaration:
pro!edure 2ort'A# arra/7*..*J9 of -nteger( // s/ntax error
causes a compilation error. 1ut:
t/pe ";igits $ arra/7*..*J9 of -nteger;
pro!edure 2ort'A# ";igits(;
is *alid. Another approach is to use open arra- parameters.
Since the Delphi lan.ua.e does not implement *alue semantics /or d-namic arra-s2 ;*alue; parameters in
routines do not represent a /ull cop- o/ the d-namic arra-. !n this e%ample:
t/pe
";/nami!Arra/ $ arra/ of -nteger;
pro!edure p'Daue# ";/nami!Arra/(;
begin
Daue7J9 #$ *;
end;

pro!edure Run;
:ar
a# ";/nami!Arra/;
begin
2et+engt,'a, *(;
a7J9 #$ J;
p'a(;
0riten'a7J9(; // 5rints %*%
end;
9ote that the assi.nment to 3alueJ'L in routine p will modi/- the content o/ d-namic arra- o/ the caller2
despite 3alue bein. a b-A*alue parameter. !/ a /ull cop- o/ the d-namic arra- is re(uired2 use the 0op-
standard procedure to create a *alue cop- o/ the d-namic arra-.
&pen ,rray Parameters
5pen arra- parameters allow arra-s o/ di//erent si@es to be passed to the same procedure or /unction. #o
de/ine a routine with an open arra- parameter2 use the s-nta% arra- o/ t-pe (rather than arra-JX..)L o/
t-pe in the parameter declaration. 4or e%ample:
fun!tion Find'A# arra/ of &,ar(# -nteger;
declares a /unction called 4ind that takes a character arra- o/ an- si@e and returns an inte.er.
9ote: #he s-nta% o/ open arra- parameters resembles that o/ d-namic arra- t-pes2 but the- do not mean
the same thin.. #he pre*ious e%ample creates a /unction that takes an- arra- o/ 0har elements2 includin.
(but not limited to d-namic arra-s. #o declare parameters that must be d-namic arra-s2 -ou need to
speci/- a t-pe identi/ier:
t/pe ";/nami!&,arArra/ $ arra/ of &,ar;
fun!tion Find'A# ";/nami!&,arArra/(# -nteger;
>ithin the bod- o/ a routine2 open arra- parameters are .o*erned b- the /ollowin. rules:
" #he- are alwa-s @eroAbased. #he /irst element is '2 the second element is +2 and so /orth. #he
standard Fow and Ci.h /unctions return ' and Fen.th A +2 respecti*el-. #he Si@e5/ /unction
returns the si@e o/ the actual arra- passed to the routine.
+M/78
" #he- can be accessed b- element onl-. Assi.nments to an entire open arra- parameter are not
allowed.
" #he- can be passed to other procedures and /unctions onl- as open arra- parameters or unt-ped
*ar parameters. #he- cannot be passed to SetFen.th.
" !nstead o/ an arra-2 -ou can pass a *ariable o/ the open arra- parameter;s base t-pe. !t will be
treated as an arra- o/ len.th +.
>hen -ou pass an arra- as an open arra- *alue parameter2 the compiler creates a local cop- o/ the arra-
within the routine;s stack /rame. 1e care/ul not to o*er/low the stack b- passin. lar.e arra-s.
#he /ollowin. e%amples use open arra- parameters to de/ine a 0lear procedure that assi.ns @ero to each
element in an arra- o/ reals and a Sum /unction that computes the sum o/ the elements in an arra- o/
reals:
pro!edure &ear':ar A# arra/ of Rea(;
:ar
-# -nteger;
begin
for - #$ J to Cig,'A( do A7-9 #$ J;
end;

fun!tion 2um'!onst A# arra/ of Rea(# Rea;
:ar
-# -nteger;
2# Rea;
begin
2 #$ J;
for - #$ J to Cig,'A( do 2 #$ 2 H A7-9;
2um #$ 2;
end;
>hen -ou call routines that use open arra- parameters2 -ou can pass open arra- constructors to them.
)ariant &pen ,rray Parameters
3ariant open arra- parameters allow -ou to pass an arra- o/ di//erentl- t-ped e%pressions to a sin.le
procedure or /unction. #o de/ine a routine with a *ariant open arra- parameter2 speci/- arra- o/ const as
the parameter;s t-pe. #hus:
pro!edure ;o2omet,ing'A# arra/ of !onst(;
declares a procedure called DoSomethin. that can operate on hetero.eneous arra-s.
#he arra- o/ const construction is e(ui*alent to arra- o/ #3arRec. S-stem.#3arRec2 which is declared in
the S-stem unit2 represents a record with a *ariant part that can hold *alues o/ inte.er2 1oolean2 character2
real2 strin.2 pointer2 class2 class re/erence2 inter/ace2 and *ariant t-pes. #3arRec;s 3#-pe /ield indicates
the t-pe o/ each element in the arra-. Some t-pes are passed as pointers rather than *alues? in particular2
strin.s are passed as Pointer and must be t-pecast to strin..
#he /ollowin. >in7< e%ample2 uses a *ariant open arra- parameter in a /unction that creates a strin.
representation o/ each element passed to it and concatenates the results into a sin.le strin.. #he strin.A
handlin. routines called in this /unction are de/ined in S-s=tils:
fun!tion MaIe2tr'!onst Args# arra/ of !onst(# string;
:ar
-# -nteger;
begin
Resut #$ %%;
for - #$ J to Cig,'Args( do
1it, Args7-9 do
!ase D"/pe of
:t-nteger# Resut #$ Resut H -nt"o2tr'D-nteger(;
:t<ooean# Resut #$ Resut H <oo"o2tr'D<ooean(;
:t&,ar# Resut #$ Resut H D&,ar;
:tExtended# Resut #$ Resut H Foat"o2tr'DExtendedO(;
:t2tring# Resut #$ Resut H D2tringO;
+8/78
:t5&,ar# Resut #$ Resut H D5&,ar;
:t=b>e!t# Resut #$ Resut H D=b>e!t.&ass6ame;
:t&ass# Resut #$ Resut H D&ass.&ass6ame;
:tAnsi2tring# Resut #$ Resut H string'DAnsi2tring(;
:t)ni!ode2tring# Resut #$ Resut H string'D)ni!ode2tring(;
:t&urren!/# Resut #$ Resut H &urr"o2tr'D&urren!/O(;
:tDariant# Resut #$ Resut H string'DDariantO(;
:t-ntMF# Resut #$ Resut H -nt"o2tr'D-ntMFO(;
end;
end;
>e can call this /unction usin. an open arra- constructor. 4or e%ample:
MaIe2tr'7%test%, *JJ, % %, "rue, P.*F*NQ, "Form9(
returns the strin. ;test+'' #7.+&+58#4orm;.
De!ault Parameters
)ou can speci/- de/ault parameter *alues in a procedure or /unction headin.. De/ault *alues are allowed
onl- /or t-ped const and *alue parameters. #o pro*ide a de/ault *alue2 end the parameter declaration with
the G s-mbol /ollowed b- a constant e%pression that is assi.nmentAcompatible with the parameter;s t-pe.
4or e%ample2 .i*en the declaration:
pro!edure FiArra/'A# arra/ of -nteger; Daue# -nteger $ J(;
the /ollowin. procedure calls are e(ui*alent.
FiArra/'M/Arra/(;
FiArra/'M/Arra/, J(;
A multipleAparameter declaration cannot speci/- a de/ault *alue. #hus2 while the /ollowin. declaration is
le.al:
fun!tion M/Fun!tion'A# Rea $ P.N; B# Rea $ P.N(# Rea;
#he /ollowin. declaration is not le.al:
fun!tion M/Fun!tion'A, B# Rea $ P.N(# Rea; // s/ntax error
Parameters with de/ault *alues must occur at the end o/ the parameter list. #hat is2 all parameters
/ollowin. the /irst declared de/ault *alue must also ha*e de/ault *alues. So the /ollowin. declaration is
ille.al:
pro!edure M/5ro!edure'-# -nteger $ *; 2# string(; // s/ntax error
De/ault *alues speci/ied in a procedural t-pe o*erride those speci/ied in an actual routine. #hus2 .i*en the
declarations:
t/pe "Resi.er $ fun!tion'A# Rea; B# Rea $ *.J(# Rea;
fun!tion Resi.er'A# Rea; B# Rea $ 3.J(# Rea;
:ar
F# "Resi.er;
6# Rea;
the statements:
F #$ Resi.er;
F'6(;
result in the *alues (92 +.' bein. passed to Resi@er.
De/ault parameters are limited to *alues that can be speci/ied b- a constant e%pression. Cence parameters
o/ a d-namicAarra-2 procedural2 class2 classAre/erence2 or inter/ace t-pe can ha*e no *alue other than nil as
their de/ault. Parameters o/ a record2 *ariant2 /ile2 staticAarra-2 or ob6ect t-pe cannot ha*e de/ault *alues at
all.
De!ault Parameters and &*erloaded #unctions
!/ -ou use de/ault parameter *alues in an o*erloaded routine2 a*oid ambi.uous parameter si.natures.
0onsider2 /or e%ample2 the /ollowin.:
pro!edure &onfused'-# -nteger(; o:eroad;
...
pro!edure &onfused'-# -nteger; E# -nteger $ J(; o:eroad;
<'/78
...
&onfused'A(; // 0,i!, pro!edure is !aedR
!n /act2 neither procedure is called. #his code .enerates a compilation error.
De!ault Parameters in #or0ard and nter!ace Declarations
!/ a routine has a /orward declaration or appears in the inter/ace section o/ a unit2 de/ault parameter *alues
i/ there are an- must be speci/ied in the /orward or inter/ace declaration. !n this case2 the de/ault *alues
can be omitted /rom the de/inin. (implementation declaration? but i/ the de/inin. declaration includes
de/ault *alues2 the- must match those in the /orward or inter/ace declaration e%actl-.
Calling Procedures and #unctions (Delphi)
#his topic co*ers the /ollowin. items:
" Pro.ram control and routine parameters
" 5pen arra- constructors
" #he inline directi*e
Program Control and Parameters
>hen -ou call a procedure or /unction2 pro.ram control passes /rom the point where the call is made to
the bod- o/ the routine. )ou can make the call usin. the routine;s declared name (with or without
(uali/iers or usin. a procedural *ariable that points to the routine. !n either case2 i/ the routine is declared
with parameters2 -our call to it must pass parameters that correspond in order and t-pe to the routine;s
parameter list. #he parameters -ou pass to a routine are called actual parameters2 while the parameters in
the routine;s declaration are called /ormal parameters.
>hen callin. a routine2 remember that:
" e%pressions used to pass t-ped const and *alue parameters must be assi.nmentAcompatible with
the correspondin. /ormal parameters.
" e%pressions used to pass *ar and out parameters must be identicall- t-ped with the correspondin.
/ormal parameters2 unless the /ormal parameters are unt-ped.
" onl- assi.nable e%pressions can be used to pass *ar and out parameters.
" i/ a routine;s /ormal parameters are unt-ped2 numerals and true constants with numeric *alues
cannot be used as actual parameters.
>hen -ou call a routine that uses de/ault parameter *alues2 all actual parameters /ollowin. the /irst
accepted de/ault must also use the de/ault *alues? calls o/ the /orm Some4unction(22X are not le.al.
)ou can omit parentheses when passin. all and onl- the de/ault parameters to a routine. 4or e%ample2
.i*en the procedure:
pro!edure ;o2omet,ing'A# Rea $ *.J; -# -nteger $ J; 2# string $ (;
the /ollowin. calls are e(ui*alent:
;o2omet,ing'(;
;o2omet,ing;
&pen ,rray Constructors
5pen arra- constructors allow -ou to construct arra-s directl- within /unction and procedure calls. #he-
can be passed onl- as open arra- parameters or *ariant open arra- parameters.
An open arra- constructor2 like a set constructor2 is a se(uence o/ e%pressions separated b- commas and
enclosed in brackets.
4or e%ample2 .i*en the declarations:
<+/78
:ar -, E# -nteger;
pro!edure Add'A# arra/ of -nteger(;
-ou could call the Add procedure with the statement:
Add'7N, S, -, - H E9(;
#his is e(ui*alent to:
:ar "emp# arra/7J..P9 of -nteger;
...
"emp7J9 #$ N;
"emp7*9 #$ S;
"emp739 #$ -;
"emp7P9 #$ - H E;
Add'"emp(;
5pen arra- constructors can be passed onl- as *alue or const parameters. #he e%pressions in a constructor
must be assi.nmentAcompatible with the base t-pe o/ the arra- parameter. !n the case o/ a *ariant open
arra- parameter2 the e%pressions can be o/ di//erent t-pes.
Using the inline Directi*e
#he Delphi compiler allows /unctions and procedures to be ta..ed with the inline directi*e to impro*e
per/ormance. !/ the /unction or procedure meets certain criteria2 the compiler will insert code directl-2
rather than .eneratin. a call. !nlinin. is a per/ormance optimi@ation that can result in /aster code2 but at
the e%pense o/ space. !nlinin. alwa-s causes the compiler to produce a lar.er binar- /ile. #he inline
directi*e is used in /unction and procedure declarations and de/initions2 like other directi*es.
pro!edure M/5ro!'x#-nteger(; inine;
begin
// ...
end;
fun!tion M/Fun!'/#&,ar( # 2tring; inine;
begin
// ..
end;
#he inline directi*e is a su..estion to the compiler. #here is no .uarantee the compiler will inline a
particular routine2 as there are a number o/ circumstances where inlinin. cannot be done. #he /ollowin.
list shows the conditions under which inlinin. does or does not occur:
" !nlinin. will not occur on an- /orm o/ lateAbound method. #his includes *irtual2 d-namic2 and
messa.e methods.
" Routines containin. assembl- code will not be inlined.
" 0onstructors and destructors will not be inlined.
" #he main pro.ram block2 unit initiali@ation2 and unit /inali@ation blocks cannot be inlined.
" Routines that are not de/ined be/ore use cannot be inlined.
" Routines that take open arra- parameters cannot be inlined.
" 0ode can be inlined within packa.es2 howe*er2 inlinin. ne*er occurs across packa.e boundaries.
" 9o inlinin. is done between units that are circularl- dependent. #his includes indirect circular
dependencies2 /or e%ample2 unit A uses unit 12 and unit 1 uses unit 0 which in turn uses unit A.
!n this e%ample2 when compilin. unit A2 no code /rom unit 1 or unit 0 will be inlined in unit A.
" #he compiler can inline code when a unit is in a circular dependenc-2 as lon. as the code to be
inlined comes /rom a unit outside the circular relationship. !n the abo*e e%ample2 i/ unit A also
used unit D2 code /rom unit D could be inlined in A2 since it is not in*ol*ed in the circular
dependenc-.
<</78
" !/ a routine is de/ined in the inter/ace section and it accesses s-mbols de/ined in the
implementation section2 that routine cannot be inlined.
" !/ a routine marked with inline uses e%ternal s-mbols /rom other units2 all o/ those units must be
listed in the uses statement2 otherwise the routine cannot be inlined.
" Procedures and /unctions used in conditional e%pressions in whileAdo and repeatAuntil statements
cannot be e%panded inline.
" >ithin a unit2 the bod- /or an inline /unction should be de/ined be/ore calls to the /unction are
made. 5therwise2 the bod- o/ the /unction2 which is not known to the compiler when it reaches
the call site2 cannot be e%panded inline.
!/ -ou modi/- the implementation o/ an inlined routine2 -ou will cause all units that use that /unction to
be recompiled. #his is di//erent /rom traditional rebuild rules2 where rebuilds were tri..ered onl- b-
chan.es in the inter/ace section o/ a unit.
#he O:!9F!9EQ compiler directi*e .i*es -ou /iner control o*er inlinin.. #he O:!9F!9EQ directi*e can be
used at the site o/ the inlined routine;s de/inition2 as well as at the call site. 1elow are the possible *alues
and their meanin.:
Value Meaning at definition Meaning at call site
O:!9F!9E
59Q(de/ault
#he routine is compiled as inlineable i/ it is
ta..ed with the inline directi*e.
#he routine will be e%panded inline
i/ possible.
O:!9F!9E
A=#5Q
1eha*es like O:!9F!9E 59Q2 with the addition
that routines not marked with inline will be
inlined i/ their code si@e is less than or e(ual to
7< b-tes.
O:!9F!9E A=#5Q has no e//ect on
whether a routine will be inlined
when it is used at the call site o/ the
routine.
O:!9F!9E
544Q
#he routine will not be marked as inlineable2
e*en i/ it is ta..ed with inline.
#he routine will not be e%panded
inline.
Classes and &'(ects (Delphi)
#his topic co*ers the /ollowin. material:
" Declaration s-nta% o/ classes
" !nheritance and scope
" 3isibilit- o/ class members
" 4orward declarations and mutuall- dependent classes
Class Types
A class2 or class t-pe2 de/ines a structure consistin. o/ /ields2 methods2 and properties. !nstances o/ a class
t-pe are called ob6ects. #he /ields2 methods2 and properties o/ a class are called its components or
members.
" A /ield is essentiall- a *ariable that is part o/ an ob6ect. Fike the /ields o/ a record2 a class; /ields
represent data items that e%ist in each instance o/ the class.
" A method is a procedure or /unction associated with a class. ,ost methods operate on ob6ects2
that is2 instances o/ a class. Some methods (called class methods operate on class t-pes
themsel*es.
" A propert- is an inter/ace to data associated with an ob6ect (o/ten stored in a /ield. Properties
ha*e access speci/iers2 which determine how their data is read and modi/ied. 4rom other parts o/
a pro.ram outside o/ the ob6ect itsel/2 a propert- appears in most respects like a /ield.
<7/78
5b6ects are d-namicall- allocated blocks o/ memor- whose structure is determined b- their class t-pe.
Each ob6ect has a uni(ue cop- o/ e*er- /ield de/ined in the class2 but all instances o/ a class share the
same methods. 5b6ects are created and destro-ed b- special methods called constructors and destructors.
A *ariable o/ a class t-pe is actuall- a pointer that re/erences an ob6ect. Cence more than one *ariable can
re/er to the same ob6ect. Fike other pointers2 classAt-pe *ariables can hold the *alue nil. 1ut -ou don;t
ha*e to e%plicitl- dere/erence a classAt-pe *ariable to access the ob6ect it points to. 4or e%ample2
Some5b6ect.Si@e :G +'' assi.ns the *alue +'' to the Si@e propert- o/ the ob6ect re/erenced b-
Some5b6ect? -ou would not write this as Some5b6ectH.Si@e :G +''.
A class t-pe must be declared and .i*en a name be/ore it can be instantiated. ()ou cannot de/ine a class
t-pe within a *ariable declaration. Declare classes onl- in the outermost scope o/ a pro.ram or unit2 not
in a procedure or /unction declaration.
A class t-pe declaration has the /orm:
t/pe
!ass6ame $ !ass 7abstra!t 8 seaed9 'an!estor&ass(
member+ist
end;
where class9ame is an- *alid identi/ier2 the sealed or abstract ke-word is optional2 (ancestor0lass is
optional2 and memberFist declares membersIthat is2 /ields2 methods2 and propertiesIo/ the class. Cere
the Jabstract K sealedL s-nta% (the J L brackets and the K pipe between them is used to speci/- that onl- one
o/ the optional sealed or abstract ke-words can be used. ,eanin./ul is onl- the sealed or abstract
ke-word? the brackets and pipe s-mbols themsel*es should be deleted.
!/ -ou omit (ancestor0lass2 then the new class inherits directl- /rom the prede/ined S-stem.#5b6ect
class. !/ -ou include (ancestor0lass and memberFist is empt-2 -ou can omit end. A class t-pe declaration
can also include a list o/ inter/aces implemented b- the class? see !mplementin. !nter/aces.
!/ a class is marked sealed2 then it cannot be e%tended throu.h inheritance.
An entire class can be declared abstract e*en i/ it does not contain an- abstract *irtual methods.
9ote: Delphi allows instantiatin. a class declared abstract2 /or backward compatibilit-2 but this /eature
should not be used an-more.
A class cannot be both abstract and sealed.
,ethods appear in a class declaration as /unction or procedure headin.s2 with no bod-. De/inin.
declarations /or each method occur elsewhere in the pro.ram.
4or e%ample2 here is the declaration o/ the #,emor-Stream class /rom the 0lasses unit:
t/pe
"Memor/2tream $ !ass'"&ustomMemor/2tream(
pri:ate
F&apa!it/# +ongint;
pro!edure 2et&apa!it/'6e1&apa!it/# +ongint(;
prote!ted
fun!tion Reao!':ar 6e1&apa!it/# +ongint(# 5ointer; :irtua;
propert/ &apa!it/# +ongint read F&apa!it/ 1rite 2et&apa!it/;
pubi!
destru!tor ;estro/; o:erride;
pro!edure &ear;
pro!edure +oadFrom2tream'2tream# "2tream(;
pro!edure +oadFromFie'!onst Fie6ame# string(;
pro!edure 2et2i.e'6e12i.e# +ongint(; o:erride;
fun!tion 0rite'!onst <uffer; &ount# +ongint(# +ongint; o:erride;
end;
0lasses.#,emor-Stream descends /rom 0lasses.#0ustom,emor-Stream2 inheritin. most o/ its
members. 1ut it de/inesIor rede/inesIse*eral methods and properties2 includin. its destructor method2
Destro-. !ts constructor2 0reate2 is inherited without chan.e /rom S-stem.#5b6ect2 and so is not
redeclared. Each member is declared as pri*ate2 protected2 orpublic (this class has no published
members. #hese terms are e%plained below.
Ni*en this declaration2 -ou can create an instance o/ #,emor-Stream as /ollows:
<&/78
:ar
stream# "Memor/2tream;

begin
stream #$ "Memor/2tream.&reate;
nheritance and Scope
>hen -ou declare a class2 -ou can speci/- its immediate ancestor. 4or e%ample:
t/pe "2ome&ontro $ !ass'"&ontro(;
declares a class called #Some0ontrol that descends /rom 3cl.0ontrols.#0ontrol. A class t-pe
automaticall- inherits all o/ the members /rom its immediate ancestor. Each class can declare new
members and can rede/ine inherited ones2 but a class cannot remo*e members de/ined in an ancestor.
Cence #Some0ontrol contains all o/ the members de/ined in 3cl.0ontrols.#0ontrol and in each o/ the
3cl.0ontrols.#0ontrol ancestors.
#he scope o/ a member;s identi/ier starts at the point where the member is declared2 continues to the end
o/ the class declaration2 and e%tends o*er all descendants o/ the class and the blocks o/ all methods
de/ined in the class and its descendants.
T&'(ect and TClass
#he S-stem.#5b6ect class2 declared in the S-stem unit2 is the ultimate ancestor o/ all other classes.
S-stem.#5b6ect de/ines onl- a hand/ul o/ methods2 includin. a basic constructor and destructor. !n
addition to S-stem.#5b6ect2 the S-stem unit declares the class re/erence t-pe S-stem.#0lass:
"&ass $ !ass of "=b>e!t;
!/ the declaration o/ a class t-pe does not speci/- an ancestor2 the class inherits directl- /rom
S-stem.#5b6ect. #hus:
t/pe "M/&ass $ !ass
...
end;
is e(ui*alent to:
t/pe "M/&ass $ !ass'"=b>e!t(
...
end;
#he latter /orm is recommended /or readabilit-.
Compati'ility o! Class Types
A class t-pe is assi.nmentAcompatible with its ancestors. Cence a *ariable o/ a class t-pe can re/erence an
instance o/ an- descendant t-pe. 4or e%ample2 .i*en the declarations:
t/pe
"Figure $ !ass'"=b>e!t(;
"Re!tange $ !ass'"Figure(;
"2?uare $ !ass'"Re!tange(;
:ar
Fig# "Figure;
the *ariable 4i. can be assi.ned *alues o/ t-pe #4i.ure2 #Rectan.le2 and #S(uare.
&'(ect Types
#he >in7< Delphi compiler allows an alternati*e s-nta% to class t-pes. )ou can declare ob6ect t-pes
usin. the s-nta%:
t/pe ob>e!t"/pe6ame $ ob>e!t 'an!estor=b>e!t"/pe(
member+ist
<5/78
end;
where ob6ect#-pe9ame is an- *alid identi/ier2 (ancestor5b6ect#-pe is optional2 and memberFist declares
/ields2 methods2 and properties. !/ (ancestor5b6ect#-pe is omitted2 then the new t-pe has no ancestor.
5b6ect t-pes cannot ha*e published members.
Since ob6ect t-pes do not descend /rom S-stem.#5b6ect2 the- pro*ide no builtAin constructors2
destructors2 or other methods. )ou can create instances o/ an ob6ect t-pe usin. the 9ew procedure and
destro- them with the Dispose procedure2 or -ou can simpl- declare *ariables o/ an ob6ect t-pe2 6ust as
-ou would with records.
5b6ect t-pes are supported /or backward compatibilit- onl-. #heir use is not recommended on >in7<.
)isi'ility o! Class %em'ers
E*er- member o/ a class has an attribute called *isibilit-2 which is indicated b- one o/ the reser*ed words
pri*ate2 protected2 public2 published2 or automated. 4or e%ample2
pubis,ed propert/ &oor# "&oor read @et&oor 1rite 2et&oor;
declares a published propert- called 0olor. 3isibilit- determines where and how a member can be
accessed2 with pri*ate representin. the least accessibilit-2 protected representin. an intermediate le*el o/
accessibilit-2 and public2 published2 and automated representin. the .reatest accessibilit-.
!/ a member;s declaration appears without its own *isibilit- speci/ier2 the member has the same *isibilit-
as the one that precedes it. ,embers at the be.innin. o/ a class declaration that don;t ha*e a speci/ied
*isibilit- are b- de/ault published2 pro*ided the class is compiled in the O:,PQ state or is deri*ed /rom a
class compiled in the O:,PQ state? otherwise2 such members are public.
4or readabilit-2 it is best to or.ani@e a class declaration b- *isibilit-2 placin. all the pri*ate members
to.ether2 /ollowed b- all the protected members2 and so /orth. #his wa- each *isibilit- reser*ed word
appears at most once and marks the be.innin. o/ a new ;section; o/ the declaration. So a t-pical class
declaration should be like this:
t/pe
"M/&ass $ !ass'"&ontro(
pri:ate
{ pri:ate de!arations ,ere }
prote!ted
{ prote!ted de!arations ,ere }
pubi!
{ pubi! de!arations ,ere }
pubis,ed
{ pubis,ed de!arations ,ere }
end;
)ou can increase the *isibilit- o/ a member in a descendent class b- redeclarin. it2 but -ou cannot
decrease its *isibilit-. 4or e%ample2 a protected propert- can be made public in a descendant2 but not
pri*ate. ,oreo*er2 published members cannot become public in a descendent class. 4or more in/ormation2
see Propert- 5*errides and Redeclarations.
Pri*ate+ Protected+ and Pu'lic %em'ers
A pri*ate member is in*isible outside o/ the unit or pro.ram where its class is declared. !n other words2 a
pri*ate method cannot be called /rom another module2 and a pri*ate /ield or propert- cannot be read or
written to /rom another module. 1- placin. related class declarations in the same module2 -ou can .i*e
the classes access to one another;s pri*ate members without makin. those members more widel-
accessible. 4or a member to be *isible onl- inside its class2 it needs to be declared strict pri*ate.
A protected member is *isible an-where in the module where its class is declared and /rom an-
descendent class2 re.ardless o/ the module where the descendent class appears. A protected method can be
called2 and a protected /ield or propert- read or written to2 /rom the de/inition o/ an- method belon.in. to
<D/78
a class that descends /rom the one where the protected member is declared. ,embers that are intended /or
use onl- in the implementation o/ deri*ed classes are usuall- protected.
A public member is *isible where*er its class can be re/erenced.
Strict )isi'ility Speci!iers
!n addition to pri*ate and protected *isibilit- speci/iers2 the Delphi compiler supports additional *isibilit-
settin.s with .reater access constraints. #hese settin.s are strict pri*ate and strict protected*isibilit-.
#hese settin.s can be used in >in7< applications.
0lass members with strict pri*ate *isibilit- are accessible onl- within the class in which the- are declared.
#he- are not *isible to procedures or /unctions declared within the same unit. 0lass members with strict
protected *isibilit- are *isible within the class in which the- are declared2 and within an- descendent
class2 re.ardless o/ where it is declared. 4urthermore2 when instance members (those declared without the
class or class *ar ke-words are declared strict pri*ate or strict protected2 the- are inaccessible outside o/
the instance o/ a class in which the- appear. An instance o/ a class cannot access strict protected or strict
protected instance members in other instances o/ the same class.
Delphi;s traditional pri*ate *isibilit- speci/ier maps to the 0FR;s assembl- *isibilit-. Delphi;s protected
*isibilit- speci/ier maps to the 0FR;s assembl- or /amil- *isibilit-.
9ote: #he word strict is treated as a directi*e within the conte%t o/ a class declaration. >ithin a class
declaration -ou cannot declare a member named ;strict;2 but it is acceptable /or use outside o/ a class
declaration.
Pu'lished %em'ers
Published members ha*e the same *isibilit- as public members. #he di//erence is that runAtime t-pe
in/ormation (R##! is .enerated /or published members. R##! allows an application to (uer- the /ields
and properties o/ an ob6ect d-namicall- and to locate its methods. R##! is used to access the *alues o/
properties when sa*in. and loadin. /orm /iles2 to displa- properties in the 5b6ect !nspector2 and to
associate speci/ic methods (called e*ent handlers with speci/ic properties (called e*ents.
Published properties are restricted to certain data t-pes. 5rdinal2 strin.2 class2 inter/ace2 *ariant2 and
methodApointer t-pes can be published. So can set t-pes2 pro*ided the upper and lower bounds o/ the base
t-pe ha*e ordinal *alues /rom ' throu.h 7+. (!n other words2 the set must /it in a b-te2 word2 or double
word. An- real t-pe e%cept Real&M can be published. Properties o/ an arra- t-pe (as distinct /rom arra-
properties2 discussed below cannot be published.
Some properties2 althou.h publishable2 are not /ull- supported b- the streamin. s-stem. #hese include
properties o/ record t-pes2 arra- properties o/ all publishable t-pes2 and properties o/enumerated t-pes
that include anon-mous *alues. !/ -ou publish a propert- o/ this kind2 the 5b6ect !nspector will not
displa- it correctl-2 nor will the propert-;s *alue be preser*ed when ob6ects are streamed to disk.
All methods are publishable2 but a class cannot publish two or more o*erloaded methods with the same
name. 4ields can be published onl- i/ the- are o/ a class or inter/ace t-pe.
A class cannot ha*e published members unless it is compiled in the O:,PQ state or descends /rom a class
compiled in the O:,PQ state. ,ost classes with published members deri*e /rom 0lasses.#Persistent2
which is compiled in the O:,PQ state2 so it is seldom necessar- to use the :, directi*e.
9ote: !denti/iers that contain =nicode characters are not allowed in published sections o/ classes2 or in
t-pes used b- published members.
,utomated %em'ers (-in./ &nly)
Automated members ha*e the same *isibilit- as public members. #he di//erence is that Automation t-pe
in/ormation (re(uired /or Automation ser*ers is .enerated /or automated members. Automated members
t-picall- appear onl- in >in7< classes . #he automated reser*ed word is maintained /or backward
compatibilit-. #he #Auto5b6ect class in the 0om5b6 unit does not useautomated.
<E/78
#he /ollowin. restrictions appl- to methods and properties declared as automated.
" #he t-pes o/ all properties2 arra- propert- parameters2 method parameters2 and /unction results
must be automatable. #he automatable t-pes are 1-te2 0urrenc-2 Real2 Double2 Fon.int2!nte.er2
Sin.le2 Smallint2 AnsiStrin.2 >ideStrin.2 #Date#ime2 3ariant2 5le3ariant2 >ord1ool2 and all
inter/ace t-pes.
" ,ethod declarations must use the de/ault re.ister callin. con*ention. #he- can be *irtual2 but not
d-namic.
" Propert- declarations can include access speci/iers (read and write but other speci/iers (inde%2
stored2 de/ault2 and node/ault are not allowed. Access speci/iers must list a method identi/ier that
uses the de/ault re.ister callin. con*ention? /ield identi/iers are not allowed.
" Propert- declarations must speci/- a t-pe. Propert- o*errides are not allowed.
#he declaration o/ an automated method or propert- can include a dispid directi*e. Speci/-in. an alread-
used !D in a dispid directi*e causes an error.
5n the >in7< plat/orm2 this directi*e must be /ollowed b- an inte.er constant that speci/ies an
Automation dispatch !D /or the member. 5therwise2 the compiler automaticall- assi.ns the member a
dispatch !D that is one lar.er than the lar.est dispatch !D used b- an- method or propert- in the class and
its ancestors. 4or more in/ormation about Automation (on >in7< onl-2 see Automation 5b6ects.
#or0ard Declarations and %utually Dependent Classes
!/ the declaration o/ a class t-pe ends with the word class and a semicolonIthat is2 i/ it has the /orm
t/pe !ass6ame $ !ass;
with no ancestor or class members listed a/ter the word class2 then it is a /orward declaration. A /orward
declaration must be resol*ed b- a de/inin. declaration o/ the same class within the same t-pe declaration
section. !n other words2 between a /orward declaration and its de/inin. declaration2 nothin. can occur
e%cept other t-pe declarations.
4orward declarations allow mutuall- dependent classes. 4or e%ample:
t/pe
"Figure $ !ass; // for1ard de!aration
";ra1ing $ !ass
Figure# "Figure;
// ...
end;

"Figure $ !ass // defining de!aration
;ra1ing# ";ra1ing;
// ...
end;
Do not con/use /orward declarations with complete declarations o/ t-pes that deri*e /rom S-stem.#5b6ect
without declarin. an- class members.
t/pe
"First&ass $ !ass; // t,is is a for1ard de!aration
"2e!ond&ass $ !ass // t,is is a !ompete !ass de!aration
end; //
"",ird&ass $ !ass'"=b>e!t(; // t,is is a !ompete !ass de!aration
1i'raries and Packages (Delphi)
A d-namicall- loadable librar- is a d-namicAlink librar- (DFF on >indows2 or a D)F!1 on ,ac. !t is a
collection o/ routines that can be called b- applications and b- other DFFs or shared ob6ects. Fike units2
d-namicall- loadable libraries contain sharable code or resources. 1ut this t-pe o/ librar- is a separatel-
compiled e%ecutable that is linked2 at run time2 to the pro.rams that use it.
<M/78
Delphi pro.rams can call DFFs and assemblies written in other lan.ua.es2 and applications written in
other lan.ua.es can call DFFs or assemblies written in Delphi.
Calling Dynamically 1oada'le 1i'raries
)ou can call operatin. s-stem routines directl-2 but the- are not linked to -our application until run time.
#his means that the librar- need not be present when -ou compile -our pro.ram. Also2 there is no
compileAtime *alidation o/ attempts to import a routine.
1e/ore -ou can call routines de/ined in DFF or assembl-2 -ou must import them. #his can be done in two
wa-s: b- declarin. an e%ternal procedure or /unction2 or b- direct calls to the operatin. s-stem.
>hiche*er method -ou use2 the routines are not linked to -our application until run time.
Delphi does not support importin. *ariables /rom DFFs or assemblies.
Static 1oading
#he simplest wa- to import a procedure or /unction is to declare it usin. the e%ternal directi*e. 4or
e%ample:
pro!edure ;o2omet,ing; externa %MB+-<.;++%;
!/ -ou include this declaration in a pro.ram2 ,)F!1.DFF is loaded once2 when the pro.ram starts.
#hrou.hout the e%ecution o/ the pro.ram2 the identi/ier DoSomethin. alwa-s re/ers to the same entr-
point in the same shared librar-.
Declarations o/ imported routines can be placed directl- in the pro.ram or unit where the- are called. #o
simpli/- maintenance2 howe*er2 -ou can collect e%ternal declarations into a separate Bimport unitB that
also contains an- constants and t-pes re(uired /or inter/acin. with the librar-. 5ther modules that use the
import unit can call an- routines declared in it.
Delayed 1oading
#he dela-ed directi*e can be used to decorate an e%ternal routine to dela- the loadin. o/ the librar-
containin. the routine. #he actual loadin. happens when the routine is called /or the /irst time. #he
/ollowin. e%ample demonstrates the use o/ the dela-ed directi*e:
fun!tion @et2omet,ing# -nteger; externa %someibrar/.d% dea/ed;
!n the e%ample abo*e2 the NetSomethin. routine is imported /rom the somelibrar-.dll librar-. #he dela-ed
directi*e ensures that somelibrar-.dll is not staticall- linked to the application2 but rather d-namicall-.
#he dela-ed directi*e is use/ul in the case where the imported routines do not e%ist on the tar.et operatin.
s-stem on which the application is run. Staticall- imported routines re(uire that the operatin. s-stem /ind
and load the librar- when the application is started. !/ the routine is not /ound in the loaded librar-2 or the
librar- does not e%ist2 the 5peratin. S-stem halts the e%ecution o/ the application. =sin. the dela-ed
directi*e enables -ou to check2 at run time2 whether the 5peratin. S-stem supports the re(uired AP!s?
onl- then -ou can call the imported routines.
Another potential use /or the dela-ed directi*e is related to the memor- /ootprint o/ the application:
decoratin. the less probabl- to be used routines2 as dela-ed ma- decrease the memor- /ootprint o/ the
application2 because the libraries are loaded onl- when re(uired. #he abusi*e use o/ dela-ed can dama.e
the speed per/ormance o/ the pro.ram (as percei*ed b- the end user.
9ote: #r-in. to call a dela-ed routine that cannot be resol*ed results in a runAtime error (or an e%ception2
i/ the S-s=tils unit is loaded.
!n order to /ineAtune the dela-Aloadin. process used b- the Delphi RunAtime Fibrar-2 -ou can re.ister
hook procedures to o*ersee and chan.e its beha*ior. #o accomplish this2 use SetDli9oti/-Cook<and
SetDli4ailureCook<2 declared in the S-s!nit unit. Also see the code e%ample at Dela-edFoadin. (Delphi.
<8/78
Dynamic 1oading (-indo0s2only)
)ou can access routines in a librar- throu.h direct calls to >indows AP!s2 includin. FoadFibrar-2
4reeFibrar-2 and NetProcAddress. #hese /unctions are declared in >indows.pas. !n this case2 use
proceduralAt-pe *ariables to re/erence the imported routines.
4or e%ample:
uses 0indo1s, ...;

t/pe
""imeRe! $ re!ord
2e!ond# -nteger;
Minute# -nteger;
Cour# -nteger;
end;

"@et"ime $ pro!edure':ar "ime# ""imeRe!(;
"Cande $ -nteger;

:ar
"ime# ""imeRe!;
Cande# "Cande;
@et"ime# "@et"ime;
.
.
.
begin
Cande #$ +oad+ibrar/'%ibrar/name%(;
if Cande LT J t,en
begin
U@et"ime #$ @et5ro!Address'Cande, %@et"ime%(;
if U@et"ime LT ni t,en
begin
@et"ime'"ime(;
1it, "ime do
0riten'%",e time is %, Cour, %#%, Minute, %#%, 2e!ond(;
end;
Free+ibrar/'Cande(;
end;
end;
>hen -ou import routines this wa-2 the librar- is not loaded until the code containin. the call to
FoadFibrar- e%ecutes. #he librar- is later unloaded b- the call to 4reeFibrar-. #his allows -ou to
conser*e memor- and to run -our pro.ram e*en when some o/ the libraries it uses are not present.
7'/78
dccil
Fon. strin.
Subran.e
All o/ these All o/ these
7+/78
declaration
prede/ined
All o/ these
#-pe none o/ these
7</78
All o/ these
t-pecast
All o/ these unsi.ned b-te
Si.ned b-te
77/78
Stack true
A compiler directi*e
4alse All o/ these
dependencies
inter/ace
7&/78
units
#5b6ect
4alse
#-pe
75/78
true
destructors
constructors
Propert-
7D/78
5pen arra- constructors
call
All o/ these
E%ternal
All o/ these
7E/78
sa/ecall
/orward
routines
Fon. strin.
7M/78
Data se.ment
RAD
78/78

Potrebbero piacerti anche