Delphi Language GuideBorland Software Corporation100 Enterprise Way, Scotts Valley, CA 95066-3249http://www.borland.comBorland®Delphi™
1-2Delphi Language GuideOther sources of informationThis manual generally assumes that you are working in the IDE and that you are building applicatio
5-38Delphi Language GuideType compatibility and identityType compatibilityEvery type is compatible with itself. Two distinct types are compatible if t
Data types, variables, and constants5-39Declaring types• T1 and T2 are compatible packed-string types.• T1 and T2 are compatible set types.• T1 and T2
5-40Delphi Language GuideVariablesX and Y are of the same type; at runtime, there is no way to distinguish TValue from Real. This is usually of little
Data types, variables, and constants5-41VariablesConsecutive variable declarations do not have to repeat the reserved word var:varX, Y, Z: Double;I, J
5-42Delphi Language GuideDeclared constantsDynamic variablesYou can create dynamic variables by calling the GetMem or New procedure. Such variables ar
Data types, variables, and constants5-43Declared constantsDeclared constants are either true constants or typed constants. These two kinds of constant
5-44Delphi Language GuideDeclared constantsHere are some examples of constant declarations:constMin = 0;Max = 100;Center = (Max - Min) div 2;Beta = Ch
Data types, variables, and constants5-45Resource stringsResource strings are stored as resources and linked into the executable or library so that the
5-46Delphi Language Guidecreates an array called Maze whereMaze[0,0,0] = 0Maze[0,0,1] = 1Maze[0,1,0] = 2Maze[0,1,1] = 3Maze[1,0,0] = 4Maze[1,0,1] = 5M
Data types, variables, and constants5-47Given these declarations, you can use the procedural constant MyFunction in a function call:I := MyFunction(5,
Introduction1-3Software registration and technical supportSoftware registration and technical supportBorland Software Corporation offers a range of su
5-48Delphi Language Guide
Procedures and functions6-1Chapter6Chapter6Procedures and functionsProcedures and functions, referred to collectively as routines, are self-contained
6-2Delphi Language GuideDeclaring procedures and functionsDeclaring procedures and functionsWhen you declare a procedure or function, you specify its
Procedures and functions6-3Declaring procedures and functionsThis procedure call assigns the value “17” to MyString (which must be a string variable).
6-4Delphi Language GuideDeclaring procedures and functionsdefines a constant function called WF that takes no parameters and always returns the intege
Procedures and functions6-5Declaring procedures and functionsIf the function exits without assigning a value to Result or the function name, then the
6-6Delphi Language GuideDeclaring procedures and functionsThe directives near, far, and export refer to calling conventions in 16-bit Windows programm
Procedures and functions6-7Declaring procedures and functionsWhen importing a C function that takes a variable number of parameters, use the varargs d
6-8Delphi Language GuideDeclaring procedures and functionswhere the first stringConstant gives the name of the library file and the second stringConst
Procedures and functions6-9Declaring procedures and functionsYou can pass to an overloaded routine parameters that are not identical in type with thos
1-4Delphi Language Guide
6-10Delphi Language GuideDeclaring procedures and functionsVariants can also be used as parameters in overloaded function declarations. Variant is con
Procedures and functions6-11Parametersin Unit1; if no routine in Unit1 matches the name and parameter list in the call, an error results.For informati
6-12Delphi Language GuideParametersthe = symbol and a default value. Parameter names must be valid identifiers. Any declaration can be preceded by var
Procedures and functions6-13ParametersThese functions return the same result, but only the second one—DoubleByRef—can change the value of a variable p
6-14Delphi Language GuideParametersas a var parameter to another routine. (But when you pass an object reference as a constant parameter, you can stil
Procedures and functions6-15ParametersThe following example uses untyped parameters in a function called Equal that compares a specified number of byt
6-16Delphi Language GuideParametersArray parametersWhen you declare routines that take array parameters, you cannot include index type specifiers in t
Procedures and functions6-17ParametersNoteThe syntax of open array parameters resembles that of dynamic array types, but they do not mean the same thi
6-18Delphi Language GuideParametersVariant open array parametersVariant open array parameters allow you to pass an array of differently typed expressi
Procedures and functions6-19ParametersDefault parametersYou can specify default parameter values in a procedure or function heading. Default values ar
Basic language descriptionPartIPart IBasic language descriptionThe chapters in Part I present the essential language elements required for most progra
6-20Delphi Language GuideDefault parameters and overloaded routinesIf you use default parameter values in an overloaded routine, avoid ambiguous param
Procedures and functions6-21You can omit parentheses when passing all and only the default parameters to a routine. For example, given the procedurepr
6-22Delphi Language Guide
Classes and objects7-1Chapter7Chapter7Classes and objectsA class, or class type, defines a structure consisting of fields, methods, and properties. In
7-2Delphi Language GuideClass typesClass typesA class type must be declared and given a name before it can be instantiated. (You cannot define a class
Classes and objects7-3Class typesInheritance and scopeWhen you declare a class, you can specify its immediate ancestor. For example,type TSomeControl
7-4Delphi Language GuideClass typesObject typesAs an alternative to class types, you can declare object types using the syntaxtype objectTypeName = ob
Classes and objects7-5Class typesYou can increase the visibility of a member in a descendant class by redeclaring it, but you cannot decrease its visi
7-6Delphi Language GuideClass typesA class cannot have published members unless it is compiled in the {$M+} state or descends from a class compiled in
Classes and objects7-7FieldsForward declarations allow mutually dependent classes. For example,typeTFigure = class; // forward declarationTDrawing = c
7-8Delphi Language GuideMethodsAlthough MyObject holds an instance of TDescendant, it is declared as TAncestor. The compiler therefore interprets MyOb
Classes and objects7-9MethodsMethod declarations can include special directives that are not used with other functions or procedures. Directives shoul
7-10Delphi Language GuideMethodsSelf is useful for a variety of reasons. For example, a member identifier declared in a class type might be redeclared
Classes and objects7-11MethodsVirtual and dynamic methodsTo make a method virtual or dynamic, include the virtual or dynamic directive in its declarat
7-12Delphi Language GuideMethodsOverriding versus hidingIf a method declaration specifies the same method identifier and parameter signature as an inh
Classes and objects7-13MethodsIf you overload a virtual method, use the reintroduce directive when you redeclare it in descendant classes. For example
7-14Delphi Language GuideMethodsparameters to the constructor. Finally, the constructor returns a reference to the newly allocated and initialized obj
Classes and objects7-15MethodsDestructorsA destructor is a special method that destroys the object where it is called and deallocates its memory. The
7-16Delphi Language GuideMethodsthe Messages unit. A message method must be a procedure that takes a single var parameter. For example, on Windows:typ
Classes and objects7-17Propertiesfor the given ID, inherited calls the DefaultHandler method originally defined in TObject.The implementation of Defau
Overview2-1Chapter2Chapter2OverviewDelphi is a high-level, compiled, strongly typed language that supports structured and object-oriented design. Base
7-18Delphi Language GuidePropertiesProperties are defined by their access specifiers. Unlike fields, properties cannot be passed as var parameters, no
Classes and objects7-19PropertiesWhen a property is referenced in an expression, its value is read using the field or method listed in the read specif
7-20Delphi Language GuidePropertiesArray propertiesArray properties are indexed properties. They can represent things like items in a list, child cont
Classes and objects7-21PropertiesThe definition of an array property can be followed by the default directive, in which case the array property become
7-22Delphi Language GuidePropertiesGiven the declaration above, if Rectangle is of type TRectangle, thenRectangle.Right := Rectangle.Left + 100;corres
Classes and objects7-23PropertiesProperty overrides and redeclarationsA property declaration that doesn’t specify a type is called a property override
7-24Delphi Language GuideClass referencescan cast MyObject to TDescendant to access the descendant class’s properties and their access specifiers.type
Classes and objects7-25Class referencesThis declaration says that to create a TCollection instance object, you must pass to the constructor the name o
7-26Delphi Language GuideClass referencesThe is operatorThe is operator, which performs dynamic type checking, is used to verify the actual runtime cl
Classes and objects7-27ExceptionsThe defining declaration of a class method must also begin with class. For example,class procedure TFigure.GetInfo(va
2-2Delphi Language GuideProgram organizationDelphi source filesThe compiler expects to find Delphi source code in files of three kinds:• unit source f
7-28Delphi Language GuideExceptionsConditional statements are often the best way to test for errors. For example, suppose you want to make sure that a
Classes and objects7-29ExceptionsRaising and handling exceptionsTo raise an exception object, use an instance of the exception class with a raise stat
7-30Delphi Language GuideExceptionsTry...except statementsExceptions are handled within try...except statements. For example,tryX := Y/Z;excepton EZer
Classes and objects7-31ExceptionsWhen an exception is handled, the stack is traced back to the procedure or function containing the try...except state
7-32Delphi Language GuideExceptionsAn exception block that contains no exception handlers, but instead consists only of a list of statements, handles
Classes and objects7-33Nested exceptionsCode executed in an exception handler can itself raise and handle exceptions. As long as these exceptions are
7-34Delphi Language Guidewhere each statementList is a sequence of statements delimited by semicolons. The try...finally statement executes the statem
Standard routines and I/O8-1Chapter8Chapter8Standard routines and I/OThis chapter discusses text and file I/O and summarizes standard library routines
8-2Delphi Language GuideFile input and outputA file variable is any variable whose type is a file type. There are three classes of file: typed, text,
Standard routines and I/O8-3File input and outputWhen a program completes processing a file, the file must be closed using the standard procedure Clos
Overview2-3Example programsVarious tools in the IDE store data in files of other types. Desktop settings (.dsk or .desk) files contain information abo
8-4Delphi Language GuideText file device driversSome of the standard I/O routines that work on text files don’t need to have a file variable explicitl
Standard routines and I/O8-5Text file device driversTo associate the device-interface functions with a specific file, you must write a customized Assi
8-6Delphi Language GuideHandling null-terminated stringsThe InOut functionThe InOut function is called by the Read, Readln, Write, Writeln, Eof, Eoln,
Standard routines and I/O8-7Handling null-terminated stringsStandard string-handling functions have multibyte-enabled counterparts that also implement
8-8Delphi Language GuideOther standard routinesOther standard routinesThe table below lists frequently used procedures and functions found in Borland
Standard routines and I/O8-9Other standard routinesFormatFloat Formats a floating point value.FreeMem Releases allocated memory.GetMem Allocates dynam
8-10Delphi Language GuideOther standard routinesFor information on format strings, see “Format strings” in the online Help.StrToCurr Converts a string
Special topicsPartIIPart IISpecial topicsThe chapters in Part II cover specialized language features and advanced topics. These chapters include:• Cha
Libraries and packages9-1Chapter9Chapter9Libraries and packagesA dynamically loadable library is a dynamic-link library (DLL) on Windows or a shared o
2-4Delphi Language GuideExample programsThe first line declares a program called Greeting. The {$APPTYPE CONSOLE} directive tells the compiler that th
9-2Delphi Language GuideCalling dynamically loadable librariesStatic loadingThe simplest way to import a procedure or function is to declare it using
Libraries and packages9-3Calling dynamically loadable librariesbeginHandle := LoadLibrary('libraryname');if Handle <> 0 thenbegin@GetT
9-4Delphi Language GuideWriting dynamically loadable librariesIn this case, when importing routines, the shared object is not loaded until the code co
Libraries and packages9-5Writing dynamically loadable librariesLibraries can be built from multiple units. In this case, the library source file is fr
9-6Delphi Language GuideWriting dynamically loadable librariesThe exports clauseA routine is exported when it is listed in an exports clause, which ha
Libraries and packages9-7Writing dynamically loadable librariesLibrary initialization codeThe statements in a library’s block constitute the library’s
9-8Delphi Language GuideWriting dynamically loadable librariesGlobal variables in a libraryGlobal variables declared in a shared library cannot be imp
Libraries and packages9-9Writing dynamically loadable librariesExceptions and runtime errors in librariesWhen an exception is raised but not handled i
9-10Delphi Language GuidePackagesPackagesA package is a specially compiled library used by applications, the IDE, or both. Packages allow you to rearr
Libraries and packages9-11Packageswhere packageName is any valid identifier. The requiresClause and containsClause are both optional. For example, the
Overview2-5Example programsdefined in Unit1. Here’s the source code for Unit1, which must be saved in a file called Unit1.pas:unit Unit1;interfaceproc
9-12Delphi Language GuidePackagesThe requires clauseThe requires clause lists other, external packages that are used by the current package. It functi
Libraries and packages9-13Compiling packagesPackages are ordinarily compiled from the IDE using .dpk files generated by the Package editor. You can al
9-14Delphi Language GuideIncluding {$DENYPACKAGEUNIT ON} in source code prevents the unit file from being packaged. Including {$G–} or {$IMPORTEDDATA
Object interfaces10-1Chapter10Chapter10Object interfacesAn object interface—or simply interface—defines methods that can be implemented by a class. In
10-2Delphi Language GuideInterface types• All members of an interface are public. Visibility specifiers and storage specifiers are not allowed. (But a
Object interfaces10-3Interface typesInterface identificationAn interface declaration can specify a globally unique identifier (GUID), represented by a
10-4Delphi Language GuideImplementing interfacesInterface propertiesProperties declared in an interface are accessible only through expressions of the
Object interfaces10-5Implementing interfacesdeclares a class called TMemoryManager that implements the IMalloc and IErrorInfo interfaces. When a class
10-6Delphi Language GuideImplementing interfacesFor example, the class declarationtypeTMemoryManager = class(TInterfacedObject, IMalloc, IErrorInfo)fu
Object interfaces10-7Implementing interfacesImplementing interfaces by delegationThe implements directive allows you to delegate implementation of an
Refer to the DEPLOY document located in the root directory of your product for a complete list of files that you can distribute in accordance with the
2-6Delphi Language GuideExample programsbegin{ calls to Application }Application.Initialize;Application.CreateForm(TForm1, Form1);Application.CreateFo
10-8Delphi Language GuideImplementing interfacesDelegating to a class-type propertyIf the delegate property is of a class type, that class and its anc
Object interfaces10-9Interface referencesInterface referencesIf you declare a variable of an interface type, the variable can reference instances of a
10-10Delphi Language GuideInterface referencesInterface references are typically managed through reference-counting, which depends on the _AddRef and
Object interfaces10-11Automation objects (Windows only)An interface query returns nil if object is nil. Otherwise, it passes the GUID of interface to
10-12Delphi Language GuideAutomation objects (Windows only)Dispatch interface propertiesProperties of a dispatch interface do not include access speci
Object interfaces10-13Automation objects (Windows only)Some Automation servers allow you to omit parameters from a method call, accepting their defaul
10-14Delphi Language Guide
Memory management11-1Chapter11Chapter11Memory managementThis chapter explains how programs use memory and describes the internal formats of Delphi dat
11-2Delphi Language GuideThe memory manager (Windows only)The memory manager maintains two status variables, AllocMemCount and AllocMemSize, which con
Memory management11-3Internal data formatsInternal data formatsThe following sections describe the internal formats of Delphi data types.Integer types
Overview2-7Example programsUnit1 creates a class named TForm1 (derived from TForm) and an instance of this class, Form1. TForm1 includes a button—Butt
11-4Delphi Language GuideInternal data formatsReal typesThe real types store the binary representation of a sign (+ or –), an exponent, and a signific
Memory management11-5Internal data formatsThe Double typeAn 8-byte (64-bit) Double number is divided into three fields:The value v of the number is gi
11-6Delphi Language GuideInternal data formatsLong string typesA long string variable occupies four bytes of memory which contain a pointer to a dynam
Memory management11-7Internal data formatsThe NULL character at the end of a wide string memory block is automatically maintained by the compiler and
11-8Delphi Language GuideInternal data formatsRecord typesWhen a record type is declared in the {$A+} state (the default), and when the declaration do
Memory management11-9Internal data formatsFile typesFile types are represented as records. Typed files and untyped files occupy 332 bytes, which are l
11-10Delphi Language GuideInternal data formatswhere fmClosed indicates that the file is closed, fmInput and fmOutput indicate a text file that has be
Memory management11-11Internal data formatscorresponding virtual method’s entry point. This layout is compatible with a C++ v-table and with COM. At n
11-12Delphi Language GuideInternal data formatsVariant typesA variant is stored as a 16-byte record that contains a type code and a value (or a refere
Program control12-1Chapter12Chapter12Program controlThis chapter explains how parameters and function results are stored and transferred. The final se
2-8Delphi Language GuideWhen the Greeting program starts, Form1 is displayed and Form2 is invisible. (By default, only the first form created in the p
12-2Delphi Language GuideParameters and function results• A long-string or dynamic-array parameter is passed as a 32-bit pointer to the dynamic memory
Program control12-3Parameters and function resultsRegister saving conventionsProcedures and functions must preserve the EBX, ESI, EDI, and EBP registe
12-4Delphi Language GuideParameters and function resultsMethod callsMethods use the same calling conventions as ordinary procedures and functions, exc
Program control12-5Exit proceduresExit proceduresExit procedures ensure that specific actions—such as updating and closing files—are carried out befor
12-6Delphi Language GuideAn exit procedure can learn the cause of termination by examining the ExitCode integer variable and the ErrorAddr pointer var
Inline assembly code13-1Chapter13Chapter13Inline assembly codeThe built-in assembler allows you to write assembly code within Delphi programs. It has
13-2Delphi Language GuideAssembler statement syntaxRegister useIn general, the rules of register use in an asm statement are the same as those of an e
Inline assembly code13-3Assembler statement syntaxInstruction opcodesThe built-in assembler supports all of the Intel-documented opcodes for general a
13-4Delphi Language GuideAssembler statement syntaxAssembly directivesThe built-in assembler supports three assembly define directives: DB (define byt
Inline assembly code13-5Assembler statement syntaxWhen an identifier precedes a DB, DW, or DD directive, it causes the declaration of a byte-, word-,
Programs and units3-1Chapter3Chapter3Programs and unitsA program is constructed from source-code modules called units. Each unit is stored in its own
13-6Delphi Language GuideAssembler statement syntaxDMTINDEX retrieves the dynamic method table index of the passed dynamic method. This directive also
Inline assembly code13-7Assembler statement syntaxprocedure CallVirtualMethod(e: TExample);asm // Instance pointer needs to be in EAX MOV EAX, e
13-8Delphi Language GuideExpressionsReserved words always take precedence over user-defined identifiers. For example,varCh: Char;ƒasmMOV CH, 1end;load
Inline assembly code13-9Expressionsthe built-in assembler cannot compute the value of X + Y at compile time. In this case, to move the sum of X and Y
13-10Delphi Language GuideExpressionsString constantsString constants must be enclosed in single or double quotation marks. Two consecutive quotation
Inline assembly code13-11ExpressionsRegistersThe following reserved symbols denote CPU registers in the inline assembler:When an operand consists sole
13-12Delphi Language GuideExpressionsThe following symbols cannot be used in asm statements:• Standard procedures and functions (for example, WriteLn
Inline assembly code13-13ExpressionsIdentifiers can be qualified within asm statements. For example, given the declarationstypeTPoint = recordX, Y: In
13-14Delphi Language GuideExpressionsImmediate values and memory references cause different code to be generated when used as operands. For example,co
Inline assembly code13-15ExpressionsExpression typesEvery built-in assembler expression has a type—or, more correctly, a size, because the assembler r
3-2Delphi Language GuideProgram structure and syntaxThe following example shows the project file for a program called Editor.1 program Editor;23 uses4
13-16Delphi Language GuideThe following table summarizes the predefined type symbols that the built-in assembler provides in addition to any currently
Inline assembly code13-17. Structure member selector. The result is the sum of the expression before the period and the expression after the period, w
13-18Delphi Language GuideAssembly procedures and functionsYou can write complete procedures and functions using inline assembly language code, withou
Inline assembly code13-19Assembly language functions return their results as follows.• Ordinal values are returned in AL (8-bit values), AX (16-bit va
13-20Delphi Language Guide
Delphi grammarA-1AppendixAAppendix ADelphi grammarGoal -> (Program | Package | Library | Unit)Program -> [PROGRAM Ident ['(' IdentList
A-2Delphi Language GuideImplementationSection -> IMPLEMENTATION[UsesClause][DeclSection]...[ExportsStmt]...Block -> [DeclSection][ExportsStmt]..
Delphi grammarA-3RealType -> REAL48-> REAL-> SINGLE-> DOUBLE-> EXTENDED-> CURRENCY-> COMPOrdinalType -> (SubrangeType | Enumer
A-4Delphi Language GuideVarSection -> VAR (VarDecl ';')...VarDecl On Windows -> IdentList ':' Type [(ABSOLUTE (Ident | Const
Delphi grammarA-5StructStmt -> CompoundStmt-> ConditionalStmt-> LoopStmt-> WithStmt-> TryExceptStmt-> TryFinallyStmt-> RaiseStmt-
Programs and units3-3Unit structure and syntaxstatements are simply method calls to the project’s Application object (most projects have an Applicatio
A-6Delphi Language GuideFunctionHeading -> FUNCTION Ident [FormalParameters] ':' (SimpleType | STRING)ProcedureHeading -> PROCEDURE Id
Delphi grammarA-7ClassFieldList -> (ClassVisibility ObjFieldList) ';'...ClassMethodList -> (ClassVisibility MethodList) ';'.
A-8Delphi Language Guide
IndexI-1Symbols- 4-4, 4-7, 4-10, 4-11" 13-10#4-5$4-4, 4-6(*, *) 4-6(, ) 4-2, 4-13, 4-15, 5-6, 5-45, 6-2, 6-3, 6-11, 7-2, 10-1* 4-2, 4-7, 4-11+ 4-
I-2Delphi Language GuideB$B directive 4-9base types 5-8, 5-18, 5-19, 5-20begin (reserved word) 3-2, 4-21, 6-2, 6-3binary operators 4-7bindingfields 7-
IndexI-3CmdLine variable 9-8COM 10-4interfaces 10-2, 10-11 to 10-13out parameters 6-14variants and 5-33, 5-35, 11-12COM error handling 6-5comments 4-1
I-4Delphi Language Guidedefault properties 7-21interfaces 10-2default property (COM object) 5-35default specifier 7-6, 7-17, 7-22DefaultHandler method
IndexI-5EE (in numerals) 4-4EAssertionFailed 7-28else (reserved word) 4-24, 4-26, 7-30empty set 5-18end (reserved word) 3-2, 4-21, 4-25, 5-23, 5-24, 6
I-6Delphi Language GuideG-$G- compiler switch 9-14$G directive 9-13generic types 5-1GetHeapStatus function 11-2GetMem procedure 5-28, 5-42, 9-9, 11-1,
IndexI-7interface declarations 3-4default paramters 6-20interface section 3-3, 3-4, 3-7forward declarations and 6-6methods 7-8scope 4-31uses clause 3-
3-4Delphi Language GuideUnit structure and syntaxUnit names must be unique within a project. Even if their unit files are in different directories, tw
I-8Delphi Language Guidedual-interface 6-5dynamic 7-10, 7-11implementation 7-8overloading 7-12overriding 7-11, 7-12, 10-6pointers 4-13, 5-31publishing
IndexI-9P$P directive 6-15package files 2-2, 2-3, 9-10, 9-13packages 9-10 to 9-14compiler directives 9-13compiler switches 9-14compiling 9-13declaring
I-10Delphi Language Guideprogram, control 6-20program (reserved word) 3-2program control ?? to 12-6programs 2-1 to 2-5, 3-1 to 3-8examples 2-3 to 2-5s
IndexI-11resource strings 5-45resourcestring (reserved word) 5-45Result variable 6-3, 6-4RET instruction 13-3return type (functions) 6-3, 6-4return va
I-12Delphi Language GuideStringToWideChar function 8-7strong typing 5-1structured statements 4-21structured types 5-17files and 5-26records and 5-25va
IndexI-13set 5-18, 11-7simple 5-3string 5-11 to 5-17, 11-5, 11-6structured 5-17subrange 5-8type identity 5-37user-defined 5-1variant 5-33 to 5-37typog
I-14Delphi Language GuideW$WARNINGS directive 4-18$WEAKPACKAGEUNIT directive 9-13while statements 4-21, 4-27wide characters and strings 5-13memory man
Programs and units3-5Unit references and the uses clauseSo, for example, if you have defined data structures that need to be initialized, you can do t
3-6Delphi Language GuideUnit references and the uses clauseIf such an explicit reference appears in the project file, other source files can refer to
Programs and units3-7Unit references and the uses clauseIn the uses clause of a unit, you cannot use in to tell the compiler where to find a source fi
iiiChapter 1Introduction 1-1What’s in this manual? . . . . . . . . . . . . . . 1-1Using Delphi . . . . . . . . . . . . .
3-8Delphi Language GuideUnit references and the uses clauseCircular unit referencesWhen units reference each other directly or indirectly, the units a
Syntactic elements4-1Chapter4Chapter4Syntactic elementsThe Delphi Language uses the ASCII character set, including the letters A through Z and a throu
4-2Delphi Language GuideFundamental syntactic elementsSpecial symbolsSpecial symbols are non-alphanumeric characters, or pairs of such characters, tha
Syntactic elements4-3Fundamental syntactic elementsQualified identifiersWhen you use an identifier that has been declared in more than one place, it i
4-4Delphi Language GuideFundamental syntactic elementsDirectivesDirectives are words that are sensitive in specific locations within source code. Dire
Syntactic elements4-5Fundamental syntactic elementsLabelsA label is a standard Delphi language identifier with the exception that, unlike other identi
4-6Delphi Language GuideComments and compiler directivesComments and compiler directivesComments are ignored by the compiler, except when they functio
Syntactic elements4-7ExpressionsThe operators @, not, and ^ are unary (taking one operand). All other operators are binary (taking two operands), exce
4-8Delphi Language GuideExpressions•The mod operator returns the remainder obtained by dividing its operands. In other words, x mod y = x – (x div y)
Syntactic elements4-9ExpressionsUse the $B compiler directive to control evaluation mode. The default state is {$B–}, which enables short-circuit eval
ivCase statements . . . . . . . . . . . . . . .4-25Control loops . . . . . . . . . . . . . . . . 4-27Repeat statements.
4-10Delphi Language GuideExpressionsThe following rules apply to string concatenation.•The operands for + can be strings, packed strings (packed array
Syntactic elements4-11ExpressionsSet operatorsThe following operators take sets as operands.The following rules apply to +, –, and *.•An ordinal O is
4-12Delphi Language GuideExpressionsFor most simple types, comparison is straightforward. For example, I = J is True just in case I and J have the sam
Syntactic elements4-13Expressions• When @ is applied to a method defined in a class, the method identifier must be qualified with the class name. For
4-14Delphi Language GuideExpressionsWithout parentheses, however, the compiler follows operator precedence rules and reads it as(X = (Y or X)) = Zwhic
Syntactic elements4-15ExpressionsExamples of set constructors:[red, green, MyColor][1, 5, 10..K mod 12, 23]['A'..'Z', 'a&apos
4-16Delphi Language GuideExpressionsVariable typecastsYou can cast any variable to any type, provided their sizes are the same and you do not mix inte
Syntactic elements4-17Declarations and statementsIn this example, TByteRec is used to access the low- and high-order bytes of a word, and TWordRec to
4-18Delphi Language GuideDeclarations and statementsHinting DirectivesThe “hint” directives platform, deprecated, and library may be appended to any d
Syntactic elements4-19Declarations and statementsSimple statementsA simple statement doesn’t contain any other statements. Simple statements include a
vString parameters. . . . . . . . . . . . . . . . 6-15Array parameters. . . . . . . . . . . . . . . .6-16Open array para
4-20Delphi Language GuideDeclarations and statementsWhen you use a function call in this way, its return value is discarded.For more information about
Syntactic elements4-21Declarations and statementsƒ {code to execute if no answer is found }Exit;FoundAnAnswer:ƒ { code to execute when an answer is fo
4-22Delphi Language GuideDeclarations and statementsCompound statements are essential in contexts where Delphi syntax requires a single statement. In
Syntactic elements4-23Declarations and statementsThis is equivalent toif OrderDate.Month = 12 thenbeginOrderDate.Month := 1;OrderDate.Year := OrderDat
4-24Delphi Language GuideDeclarations and statementsThe syntax of an if...then...else statement isif expression then statement1 else statement2where e
Syntactic elements4-25Declarations and statementsis equivalent toif ... { expression1 } thenbeginif ... { expression2 } then... { statement1 }else...
4-26Delphi Language GuideDeclarations and statementsEach value represented by a caseList must be unique in the case statement; subranges and lists can
Syntactic elements4-27Declarations and statementsControl loopsLoops allow you to execute a sequence of statements repeatedly, using a control conditio
4-28Delphi Language GuideDeclarations and statementsExamples of while statements includewhile Data[I] <> X do I := I + 1;while I > 0 dobegini
Syntactic elements4-29Blocks and scopeFor purposes of controlling execution of the loop, the expressions initialValue and finalValue are evaluated onl
viExceptions and runtime errors in libraries . . . . . . . . . . . . . . . 9-9Shared-memory manager (Windows only) . . . . . .
4-30Delphi Language GuideBlocksA block consists of a series of declarations followed by a compound statement. All declarations must occur together at
Syntactic elements4-31The rules that determine identifier scope are summarized below.Naming conflictsWhen one block encloses another, the former is ca
4-32Delphi Language GuideThe System and SysInit units are used automatically by every program or unit. The declarations in System, along with the pred
Data types, variables, and constants5-1Chapter5Chapter5Data types, variables, and constantsA type is essentially a name for a kind of data. When you d
5-2Delphi Language GuideAbout typesportability. However, changes in storage format from one implementation of a generic type to the next could cause c
Data types, variables, and constants5-3Simple typesSimple typesSimple types, which include ordinal types and real types, define ordered sets of values
5-4Delphi Language GuideSimple typesInteger typesAn integer type represents a subset of the whole numbers. The generic integer types are Integer and C
Data types, variables, and constants5-5Simple typesWhen you increment the last value or decrement the first value of an integer type, the result wraps
5-6Delphi Language GuideSimple typesFor more information about Unicode characters, see “About extended character sets” on page 5-13 and “Working with
Data types, variables, and constants5-7Simple typesdefines an enumerated type called Suit whose possible values are Club, Diamond, Heart, and Spade, w
viiChapter 13Inline assembly code 13-1The asm statement . . . . . . . . . . . . . . . . .13-1Register use . . . . . . . . . .
5-8Delphi Language GuideSimple typesEnumerated types with explicitly assigned ordinalityBy default, the ordinalities of enumerated values start from 0
Data types, variables, and constants5-9Simple typesYou can use numeric constants and characters (string constants of length 1) to define subrange type
5-10Delphi Language GuideSimple typesReal typesA real type defines a set of numbers that can be represented with floating-point notation. The table be
Data types, variables, and constants5-11String typesString typesA string represents a sequence of characters. Delphi supports the following predefined
5-12Delphi Language GuideString typesBe careful indexing strings in this way, since overwriting the end of a string can cause access violations. Also,
Data types, variables, and constants5-13String typesLong stringsAnsiString, also called a long string, represents a dynamically allocated string whose
5-14Delphi Language GuideString typessystem supports Unicode (UCS-2). The Linux operating system supports UCS-4, a superset of UCS-2. Borland’s RAD pr
Data types, variables, and constants5-15String typespoints P to an area of memory that contains a null-terminated copy of “Hello world!” This is equiv
5-16Delphi Language GuideString typesThe StrUpper function illustrates the use of pointer indexing to iterate through a null-terminated string:functio
Data types, variables, and constants5-17Structured types•PChar(S) always returns a pointer to a memory block; if S is empty, a pointer to #0 is return
viii4.1 Equivalent symbols . . . . . . . . . . . . . 4-24.2 Reserved words . . . . . . . . . . . . . . . 4-34.3 Directi
5-18Delphi Language GuideStructured typesSetsA set is a collection of values of the same ordinal type. The values have no inherent order, nor is it me
Data types, variables, and constants5-19Structured typesArraysAn array represents an indexed collection of elements of the same type (called the base
5-20Delphi Language GuideStructured typesDynamic arraysDynamic arrays do not have a fixed size or length. Instead, memory for a dynamic array is reall
Data types, variables, and constants5-21Structured typesIn contrast, to make an independent copy of a dynamic array, you must use the global Copy func
5-22Delphi Language GuideStructured typesMultidimensional dynamic arraysTo declare multidimensional dynamic arrays, use iterated array of ... construc
Data types, variables, and constants5-23Structured typesTo make the assignment work, declare the variables asvar Int1, Int2: array[1..10] of Integer;o
5-24Delphi Language GuideStructured typesOr use a with statement:with Record1 dobeginYear := 1904;Month := Jun;Day := 16;end;You can now copy the valu
Data types, variables, and constants5-25Structured types•Each constantList is a constant denoting a value of type ordinalType, or a comma-delimited li
5-26Delphi Language GuideStructured typestypeTShapeList = (Rectangle, Triangle, Circle, Ellipse, Other);TFigure = recordcase TShapeList ofRectangle: (
Data types, variables, and constants5-27Pointers and pointer typesYou can also use the file of ... construction directly in a variable declaration. Fo
Introduction1-1Chapter1Chapter1IntroductionThis manual describes the Delphi programming language as it is used in Borland development tools.What’s in
5-28Delphi Language GuidePointers and pointer typesThe symbol ^ has two purposes, both of which are illustrated in our example. When it appears before
Data types, variables, and constants5-29Pointers and pointer typesPointer typesYou can declare a pointer to any type, using the syntaxtype pointerType
5-30Delphi Language GuideProcedural typesProcedural typesProcedural types allow you to treat procedures and functions as values that can be assigned t
Data types, variables, and constants5-31Procedural typesThe previous variables are all procedure pointers—that is, pointers to the address of a proced
5-32Delphi Language GuideProcedural typesProcedural types in statements and expressionsWhen a procedural variable is on the left side of an assignment
Data types, variables, and constants5-33Variant typesThe @ operator can also be used to assign an untyped pointer value to a procedural variable. For
5-34Delphi Language GuideVariant typesThe standard function VarType returns a variant’s type code. The varTypeMask constant is a bit mask used to extr
Data types, variables, and constants5-35Variant typesOut-of-range assignments often result in the target variable getting the highest value in its ran
5-36Delphi Language GuideVariant typesIn this example, the Else part of the If statement will be executed. This behavior can be changed by setting the
Data types, variables, and constants5-37Type compatibility and identityWhen you assign a Variant that contains custom data (such as a Delphi string, o
Comentários a estes Manuais