Delphi Language Guide - Learn Delphi
Product Documentation
Delphi Language Guide
Version 10.3 Rio
Latest version is found on online docwiki.RADStudio/en/Delphi_Language_Guide_Index
? 2019 Embarcadero Technologies, Inc. Embarcadero, the Embarcadero Technologies logos, and all other Embarcadero Technologies product or service names are trademarks or registered trademarks of Embarcadero Technologies, Inc. All other trademarks are property of their respective owners.
Embarcadero tools are built for elite developers who build and maintain the world's most critical applications. Our customers choose Embarcadero because we are the champion of developers, and we help them build more secure and scalable enterprise applications faster than any other tools on the market. In fact, ninety of the Fortune 100 and an active community of more than three million users worldwide have relied on Embarcadero's award-winning products for over 30 years.
If you're trying to build a business-critical application in a demanding vertical, Embarcadero is for you. If you're looking to write steadfast code quickly that will pass stringent code reviews faster than any other, Embarcadero is for you. We're here to support elite developers who understand the scalability and stability of C++ and Delphi and depend on the decades of innovation those languages bring to development.
We invite you to try our products for free and see for yourself. products/rad-studio/start-for-free
Embarcadero is an Idera, Inc. company. Idera, Inc. is the parent company of global B2B software productivity brands whose solutions enable technical users to do more with less, faster. Idera, Inc. brands span three divisions ? Database Tools, Developer Tools, and Test Management Tools ? with products that are evangelized by millions of community members and more than 50,000 customers worldwide, including some of the world's largest healthcare, financial services, retail, and technology companies. Embarcadero and Idera are online at and
November, 2019
In support of the education of new developers, and as part of , this document is licensed under a Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) licenses/by-sa/4.0/
Please link back to and
Embarcadero Technologies
2
CONTENTS
Language Overview....................................................................................................... 15 Program Organization ................................................................................................ 15 Delphi Source Files ................................................................................................... 16 Other Files Used to Build Applications .................................................................. 16 Compiler-Generated Files ...................................................................................... 17 Example Programs ...................................................................................................... 17 A Simple Console Application............................................................................... 18 A More Complicated Example ............................................................................. 19 A VCL Application ................................................................................................... 20
Programs and Units Index .............................................................................................. 23 Topics ............................................................................................................................ 23
Programs and Units (Delphi).......................................................................................... 24 Program Structure and Syntax .................................................................................. 24 The Program Heading............................................................................................. 24 The Program Uses Clause ....................................................................................... 25 The Block ................................................................................................................... 25 Unit Structure and Syntax ........................................................................................... 26 The Unit Heading ..................................................................................................... 27 The Interface Section .............................................................................................. 27 The Implementation Section.................................................................................. 27 The Initialization Section.......................................................................................... 28 The Finalization Section........................................................................................... 28 Unit References and the Uses Clause ...................................................................... 29 The Syntax of a Uses Clause................................................................................... 29 Multiple and Indirect Unit References .................................................................. 30 Circular Unit References ......................................................................................... 31
Using Namespaces with Delphi .................................................................................... 32 Declaring Namespaces ............................................................................................. 33 Searching Namespaces............................................................................................. 34 Namespace search order...................................................................................... 34 A namespace search example ............................................................................ 34 Using Namespaces ..................................................................................................... 35 Fully qualified unit names ....................................................................................... 35 Multi-unit Namespaces ........................................................................................... 36
Fundamental Syntactic Elements Index...................................................................... 37 Topics ............................................................................................................................ 37
Fundamental Syntactic Elements (Delphi) ................................................................. 38 The Delphi Character Set........................................................................................... 38 Tokens............................................................................................................................ 39 Special Symbols ....................................................................................................... 39 Identifiers ................................................................................................................... 40
Embarcadero Technologies
3
Reserved Words ....................................................................................................... 41 Directives................................................................................................................... 42 Numerals ................................................................................................................... 44 Labels......................................................................................................................... 45 Character Strings ..................................................................................................... 45 Comments and Compiler Directives........................................................................ 46 in ........................................................................................................................................ 47 Expressions (Delphi)......................................................................................................... 48 Expressions .................................................................................................................... 48 Operators...................................................................................................................... 49 Arithmetic Operators .............................................................................................. 49 Boolean Operators .................................................................................................. 50 Complete Versus Short-Circuit Boolean Evaluation ........................................... 51 Logical (Bitwise) Operators .................................................................................... 52 String Operators ....................................................................................................... 54 Pointer Operators .................................................................................................... 54 Set Operators ........................................................................................................... 55 Relational Operators ............................................................................................... 56 Class and Interface Operators .............................................................................. 58 The @ Operator ........................................................................................................ 58 Operator Precedence............................................................................................ 58 Function Calls............................................................................................................... 60 Set Constructors........................................................................................................... 61 Indexes .......................................................................................................................... 61 Typecasts ...................................................................................................................... 62 Value Typecasts ....................................................................................................... 62 Variable Typecasts .................................................................................................. 62 Declarations and Statements (Delphi) ........................................................................ 64 Declarations ................................................................................................................. 64 Hinting Directives ..................................................................................................... 65 Statements ................................................................................................................... 66 Simple Statements....................................................................................................... 66 Assignment Statements .......................................................................................... 66 Procedure and Function Calls............................................................................... 67 Goto Statements ..................................................................................................... 67 Structured Statements ................................................................................................ 69 Compound Statements .......................................................................................... 70 With Statements ....................................................................................................... 70 If Statements............................................................................................................. 74 Case Statements ..................................................................................................... 76 Control Loops ........................................................................................................... 77 Blocks and Scope........................................................................................................ 86 Blocks ......................................................................................................................... 86 Scope ........................................................................................................................ 87
Embarcadero Technologies
4
Naming Conflicts ..................................................................................................... 87 Data Types, Variables, and Constants Index ............................................................. 88
Topics ............................................................................................................................ 88 About Data Types (Delphi)............................................................................................ 89 Simple Types (Delphi) ..................................................................................................... 91
Ordinal Types ............................................................................................................... 91 Integer Types ............................................................................................................ 92 Character Types ...................................................................................................... 95 Boolean Types .......................................................................................................... 96 Enumerated Types................................................................................................... 97 Subrange Types...................................................................................................... 102
Real Types...................................................................................................................104 String Types (Delphi) ..................................................................................................... 105
About String Types.....................................................................................................106 Short Strings................................................................................................................. 108 AnsiString ..................................................................................................................... 109 UnicodeString (the Default String Type).................................................................110 WideString................................................................................................................... 111 Working with null-Terminated Strings ...................................................................... 111
Using Pointers, Arrays, and String Constants......................................................112 Mixing Delphi Strings and Null-Terminated Strings ............................................113 Structured Types (Delphi).............................................................................................115 Alignment of Structured Types ................................................................................ 115 Sets............................................................................................................................... 115 Arrays ........................................................................................................................... 117 Static Arrays ............................................................................................................ 117 Dynamic Arrays ...................................................................................................... 118 Multidimensional Dynamic Arrays ....................................................................... 121 Array Types and Assignments .............................................................................. 122 String-Like Operations Supported on Dynamic Arrays.....................................122 Records (traditional) ................................................................................................. 123 Variant Parts in Records........................................................................................125 Records (advanced) ................................................................................................ 127 File Types (Win32)....................................................................................................... 128 Code Samples ........................................................................................................... 129 Pointers and Pointer Types (Delphi)............................................................................130 Overview of pointers.................................................................................................130 Using Extended Syntax with Pointers .................................................................. 132 Pointer Types .............................................................................................................. 132 Character Pointers ................................................................................................ 133 Byte Pointer.............................................................................................................133 Type-checked Pointers ......................................................................................... 133 Other Standard Pointer Types..............................................................................133 Procedural Types (Delphi) ........................................................................................... 134
Embarcadero Technologies
5
About Procedural Types...........................................................................................135 Method Pointers.....................................................................................................135
Procedural Types in Statements and Expressions ................................................. 137 Variant Types (Delphi) .................................................................................................. 138
Variants Overview ..................................................................................................... 138 Variant Type Conversions ........................................................................................ 140 Variants in Expressions............................................................................................... 142 Variant Arrays.............................................................................................................142 OleVariant .................................................................................................................. 143 Type Compatibility and Identity (Delphi) .................................................................. 143 Type Identity ............................................................................................................... 144 Type Compatibility .................................................................................................... 144 Assignment Compatibility ........................................................................................ 145 Data Types, Variables, and Constants Index (Delphi) ............................................ 146 Type Declaration Syntax .......................................................................................... 146 Variables (Delphi) ......................................................................................................... 148 Declaring Variables .................................................................................................. 148
Absolute Addresses ............................................................................................... 149 Dynamic Variables ................................................................................................ 150 Thread-local Variables..........................................................................................150 Declared Constants...................................................................................................... 151 True Constants ........................................................................................................... 151 Constant Expressions ............................................................................................. 154 Resource Strings ..................................................................................................... 155 Typed Constants........................................................................................................155 Array Constants...................................................................................................... 155 Record Constants .................................................................................................. 156 Procedural Constants ........................................................................................... 157 Pointer Constants...................................................................................................157 Writeable Typed Constants..................................................................................158 Procedures and Functions Index ................................................................................159 Topics .......................................................................................................................... 159 Procedures and Functions (Delphi)............................................................................160 About Procedures and Functions ........................................................................... 160 Declaring Procedures and Functions.....................................................................160 Procedure Declarations ....................................................................................... 161 Function Declarations ........................................................................................... 161 Calling Conventions.................................................................................................. 163 Forward and Interface Declarations......................................................................165 External Declarations ................................................................................................ 165 Linking to Object Files ........................................................................................... 166 Importing Functions from Libraries ...................................................................... 166 Overloading Procedures and Functions................................................................168 Local Declarations .................................................................................................... 171
Embarcadero Technologies
6
Nested Routines ..................................................................................................... 171 Parameters (Delphi)......................................................................................................172
About Parameters ..................................................................................................... 172 Parameter Semantics ............................................................................................... 173
Value and Variable Parameters ......................................................................... 173 Constant Parameters ............................................................................................ 174 Out Parameters......................................................................................................175 Untyped Parameters ............................................................................................. 176 String Parameters ...................................................................................................... 177 Array Parameters.......................................................................................................177 Open Array Parameters ....................................................................................... 178 Variant Open Array Parameters ......................................................................... 180 Default Parameters ................................................................................................... 181 Default Parameters and Overloaded Functions .............................................. 182 Default Parameters in Forward and Interface Declarations .......................... 183 Calling Procedures and Functions (Delphi) .............................................................. 183 Program Control and Parameters .......................................................................... 183 Open Array Constructors ......................................................................................... 184 Using the inline Directive .......................................................................................... 184 Anonymous Methods in Delphi...................................................................................186 Syntax .......................................................................................................................... 186 Using Anonymous Methods ..................................................................................... 188 Anonymous Methods Variable Binding ................................................................. 189 Variable Binding Illustration .................................................................................. 189 Anonymous Methods as Events .......................................................................... 190 Variable Binding Mechanism...............................................................................191 Utility of Anonymous Methods ................................................................................. 193 Variable Binding..................................................................................................... 193 Ease of Use..............................................................................................................194 Using Code for a Parameter ................................................................................ 195 Classes and Objects Index .......................................................................................... 197 Topics .......................................................................................................................... 197 Classes and Objects (Delphi)......................................................................................198 Class Types ................................................................................................................. 198 Inheritance and Scope ............................................................................................ 200 TObject and TClass................................................................................................201 Compatibility of Class Types ................................................................................ 201 Object Types .......................................................................................................... 201 Visibility of Class Members ....................................................................................... 202 Private, Protected, and Public Members .......................................................... 203 Strict Visibility Specifiers ......................................................................................... 203 Published Members ............................................................................................... 204 Automated Members (Win32 Only)....................................................................205 Forward Declarations and Mutually Dependent Classes ................................... 205
Embarcadero Technologies
7
Fields (Delphi) ................................................................................................................ 206 About Fields................................................................................................................206 Class Fields..................................................................................................................207
Methods (Delphi) .......................................................................................................... 208 About Methods..........................................................................................................209 Inherited .................................................................................................................. 210 Self............................................................................................................................ 210 Method Binding ......................................................................................................... 211 Static Methods ....................................................................................................... 211 Virtual and Dynamic Methods ............................................................................ 212 Class Methods............................................................................................................215 Ordinary Class Methods ....................................................................................... 215 Class Static Methods ............................................................................................. 216 Overloading Methods .............................................................................................. 216 Constructors ............................................................................................................... 217 Destructors .................................................................................................................. 219 Class Constructors ..................................................................................................... 220 Class Destructors........................................................................................................221 Message Methods.....................................................................................................222 Implementing Message Methods ....................................................................... 223 Message Dispatching ........................................................................................... 223
Properties (Delphi) ........................................................................................................ 224 About Properties........................................................................................................224 Property Access.........................................................................................................225 Array Properties ......................................................................................................... 227 Index Specifiers .......................................................................................................... 229 Storage Specifiers......................................................................................................229 Property Overrides and Redeclarations ................................................................ 230 Class Properties..........................................................................................................232
Events (Delphi)...............................................................................................................233 About Events .............................................................................................................. 233 Event Properties and Event Handlers ..................................................................... 233 Triggering Multiple Event Handlers ......................................................................... 235
Class References...........................................................................................................236 Class-Reference Types ............................................................................................. 236 Constructors and Class References....................................................................237 Class Operators ......................................................................................................... 238 The is Operator ....................................................................................................... 238 The as Operator ..................................................................................................... 238 Code Examples ......................................................................................................... 239
Exceptions (Delphi) ....................................................................................................... 240 About Exceptions ...................................................................................................... 240 When To Use Exceptions...........................................................................................240 Declaring Exception Types ...................................................................................... 241
Embarcadero Technologies
8
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.