Borland Delphi 8 For NET
Borland Delphi 8 For NET
NET™
Borland Software Corporation may have patents and/or pending patent applications covering subject
matter in this document. The furnishing of this document does not give you any license to these patents.
Copyright© 2004 Borland Software Corporation. All rights reserved. All Borland brand and product
names are trademarks or registered trademarks of Borland Software Corporation. Other brand and
product names are trademarks or registered trademarks of their respective holders.
Borland Software Corporation
100 Enterprise Way, Scotts Valley, CA 95066-3259
1
Concepts
Getting Started
What's Delphi for .NET
Defining Requirements .................................................................................................................. 17
Modeling Applications .................................................................................................................... 17
Designing User Interfaces ............................................................................................................. 17
Generating and Editing Code ........................................................................................................ 17
Compiling, Debugging, and Deploying Applications ...................................................................... 17
Controlling Access and Tracking Changes to Code ...................................................................... 18
The .NET Framework .................................................................................................................... 18
Tour of the IDE
Welcome Page .............................................................................................................................. 19
Forms ............................................................................................................................................. 19
Designer Surface ........................................................................................................................... 21
Tool Palette .................................................................................................................................... 21
Object Inspector ............................................................................................................................. 22
Object Repository .......................................................................................................................... 22
Project Manager ............................................................................................................................ 23
Data Explorer ................................................................................................................................. 24
Code Editor .................................................................................................................................... 24
To-Do Lists .................................................................................................................................... 25
Starting a Project
Type of Projects ............................................................................................................................. 26
Additional Projects ......................................................................................................................... 29
Unmanaged Code and COM/Interop ............................................................................................. 29
Managing the Development Cycle
Managing the Development Cycle Overview
Source Control Integration ............................................................................................................. 31
User Interface Design .................................................................................................................... 31
Code Visualization ......................................................................................................................... 32
Build, Compile, Run, and Debug ................................................................................................... 32
Using Source Control
How Delphi 8 for .NET Interacts with Source Control Systems ..................................................... 33
Source Control Basics ................................................................................................................... 34
Configuring the Source Control System ........................................................................................ 35
Repository Basics .......................................................................................................................... 35
Working with Projects .................................................................................................................... 36
Working with Files .......................................................................................................................... 36
Synchronizing Files ........................................................................................................................ 36
Designing User Interfaces
Using the Designer ........................................................................................................................ 37
Setting Designer Options ............................................................................................................... 37
Using Code Visualization
Code Visualization and UML Static Structure Diagrams ............................................................... 38
Understanding the Relationship between Source Code and Code Visualization .......................... 38
2
Compiling and Building Applications
Compiler Options ........................................................................................................................... 40
Compiler Status and Information ................................................................................................... 40
Compiler Errors .............................................................................................................................. 41
Using Translation Tools
Satellite Assembly Wizard ............................................................................................................. 42
Translation Manager ..................................................................................................................... 42
Translation Repository ................................................................................................................... 42
Files Generated by the Translation Tools ...................................................................................... 43
Debugging Applications
Stepping Through Code ................................................................................................................. 44
Evaluate/Modify ............................................................................................................................. 44
Breakpoints .................................................................................................................................... 44
Watches ......................................................................................................................................... 45
Debug Windows ............................................................................................................................. 45
Remote Debugging ........................................................................................................................ 45
Deploying Applications
Simple Applications ....................................................................................................................... 46
Applications that Include Shared Assemblies ................................................................................ 46
Installation Programs ..................................................................................................................... 46
Redistributing Delphi 8 for .NET Files ............................................................................................ 46
Redistributing the .NET Framework ............................................................................................... 47
Redistributing Third Party Software ............................................................................................... 47
Modeling with Delphi for .NET
Modeling Tools Overview
Model-Powered Applications and the ECO Framework ................................................................ 50
Basic UML Concepts ..................................................................................................................... 50
Introduction to the ECO Framework .............................................................................................. 52
Integrated Modeling Tools in Delphi 8 for .NET ............................................................................. 58
Working with Unmanaged Code
Using COM Interop in Managed Applications
COM Interop Overview .................................................................................................................. 62
COM Interop Terminology ............................................................................................................. 63
COM Interop Tools in the .NET Framework SDK .......................................................................... 65
Using COM Interop Assemblies in the IDE .................................................................................... 66
Deploying Applications That Use COM Interop ............................................................................. 67
Using Platform Invoke with Delphi for .NET
Calling Unmanaged Functions ....................................................................................................... 69
Structures ...................................................................................................................................... 72
Callback Functions ........................................................................................................................ 75
Passing Object References ........................................................................................................... 75
Using COM Interfaces ................................................................................................................... 77
Building Web Applications with ASP.NET
ASP.NET Overview
ASP.NET Architecture ................................................................................................................... 81
Web Forms Components ............................................................................................................... 82
Web Forms Data Access ............................................................................................................... 82
3
Web Services ................................................................................................................................. 82
ASP.NET Namespace ................................................................................................................... 83
ASP.NET Application Deployment ................................................................................................. 83
DB Web Controls for ASP.NET
DB Web Controls Architecture ....................................................................................................... 84
Data-aware Components Advantages ........................................................................................... 84
Supported Data Access Components ............................................................................................ 85
DB Web ControlsNamespace ........................................................................................................ 85
ASP.NET Application Deployment with DB Web Controls ............................................................ 85
Building Web Services with ASP.NET
ASP.NET Web Services Overview
Web Service Architecture .............................................................................................................. 87
Web Service Prerequisites ............................................................................................................ 88
Web Service Scenarios ................................................................................................................. 88
ASP.NET Web Services Files ........................................................................................................ 89
Web Services Protocol Stack
Web Services Protocol Stack ....................................................................................................... 91
Transport Layer ............................................................................................................................. 92
XML Messaging ............................................................................................................................. 92
WSDL Layer .................................................................................................................................. 92
Service Discovery .......................................................................................................................... 93
ASP.NET Web Services Support
ASP.NET Web Services Client Support ........................................................................................ 94
ASP.NET Web Services Server Support ....................................................................................... 95
ASP.NET Web Services Namespaces .......................................................................................... 95
Building Windows Applications with Windows Forms
Windows Forms Overview
Windows Forms Architecture ......................................................................................................... 97
Windows Forms ............................................................................................................................. 97
Windows Forms Components ........................................................................................................ 98
Windows Forms Data Access ........................................................................................................ 98
Windows Forms Namespace ......................................................................................................... 98
Windows Forms Application Deployment ...................................................................................... 98
Building Database Applications with ADO.NET
ADO.NET Overview
ADO.NET Architecture ................................................................................................................. 100
ADO.NET User Interfaces ........................................................................................................... 102
BDP.NET Namespace ................................................................................................................. 102
ADO.NET Application Deployment .............................................................................................. 103
Borland Data Providers for Microsoft .NET
Data Provider Architecture ........................................................................................................... 104
BDP.NET Advantages ................................................................................................................. 105
BDP.NET and ADO.NET Components ........................................................................................ 105
BDP.NET Data Types .................................................................................................................. 105
BDP.NET Interfaces .................................................................................................................... 105
BDP.NET Data Types
BDP.NET and .NET Framework .................................................................................................. 106
4
Data Types .................................................................................................................................. 106
DB2 .............................................................................................................................................. 106
Interbase ...................................................................................................................................... 107
MS SQL and MSDE .................................................................................................................... 107
Oracle .......................................................................................................................................... 108
BDP.NET Component Designers
Component Designer Relationships ............................................................................................ 111
Connection Editor ........................................................................................................................ 111
Command Text Editor .................................................................................................................. 111
Generate DataSets ...................................................................................................................... 112
Configure Data Adapter ............................................................................................................... 112
Data Explorer ............................................................................................................................... 112
Delphi for .NET Database Technologies
Building .NET Applications with TADONETConnector ................................................................ 113
Building .NET Applications with dbExpress.NET ......................................................................... 113
Building .NET Applications with IBX.NET .................................................................................... 114
Building .NET Applications with the DataSnap .NET Client (DCOM) .......................................... 114
Building .NET Applications with BDE.NET .................................................................................. 114
Getting Started with IBX Components
IBX components .......................................................................................................................... 115
Building VCL Applications with VCL for .NET
VCL for .NET Overview
What is VCL? ............................................................................................................................... 124
The Relationship between VCL.NET and the .NET Framework .................................................. 125
VCL.NET Components ................................................................................................................ 125
Borland.VCL Namespace ............................................................................................................ 127
Porting Delphi Applications to Delphi 8 for .NET ......................................................................... 127
Importing .NET Components for Use in VCL.NET Applications .................................................. 128
Porting Applications to VCL for .NET
General Language Issues ............................................................................................................ 129
New Language Features ............................................................................................................. 129
Porting Web Service Client Applications ..................................................................................... 130
Building Reports in Delphi for Microsoft .NET
Rave Reports Overview
Creating New Reports in Delphi 8 for .NET ................................................................................. 132
Using Rave Reports ActiveX Components .................................................................................. 132
5
Procedures
Getting Started
Adding Components to a Form .................................................................................................... 135
Adding References ...................................................................................................................... 136
Adding and Removing Files ......................................................................................................... 137
Adding Templates to the Object Repository ................................................................................ 138
Copying References to a Local Path ........................................................................................... 139
Creating a Component Template ................................................................................................. 140
Creating a Project ........................................................................................................................ 142
Customizing the Form .................................................................................................................. 143
Customizing Toolbars .................................................................................................................. 144
Customizing the Tool Palette ....................................................................................................... 145
Docking Tool Windows ................................................................................................................ 146
Filtering Searched Components .................................................................................................. 147
Exploring .NET Assembly Metadata ............................................................................................ 148
Exploring Windows Type Libraries .............................................................................................. 149
Installing Custom Components .................................................................................................... 150
Renaming Files using the Project Manager ................................................................................. 151
Saving Desktop Layouts .............................................................................................................. 152
Setting Component Properties ..................................................................................................... 153
Setting Dynamic Properties ........................................................................................................ 154
Setting Project Options ................................................................................................................ 156
Setting Properties and Events ..................................................................................................... 157
Setting Tool Preferences ............................................................................................................. 158
Using To-Do Lists ........................................................................................................................ 159
Writing Event Handlers ................................................................................................................ 161
ASP.NET
Building an ASP.NET Database Application ................................................................................ 163
Building an ASP.NET "Hello world" Application ........................................................................... 167
Building an ASP.NET Application ................................................................................................ 169
Building an Application with ........................................................................................................ 171
Extending .................................................................................................................................... 173
Using the HTML Tag Editor ......................................................................................................... 178
Compiling and Building Applications
Adding Languages to a Project .................................................................................................... 182
Building Packages ....................................................................................................................... 184
Editing Resource Files in the Translation Manager ..................................................................... 186
Setting Up the External Translation Manager .............................................................................. 188
Database
Adding a New Connection to the Data Explorer .......................................................................... 191
Browsing a Database in the Data Explorer .................................................................................. 192
Building a Windows Forms Database Application ....................................................................... 193
Creating Database Projects from the Data Explorer .................................................................... 196
Creating Table Mappings ............................................................................................................. 197
6
Executing SQL in the Data Explorer ............................................................................................ 200
Handling Errors in Table Mapping ............................................................................................... 201
Modifying Connections in the Data Explorer ................................................................................ 203
Passing Parameters in a Database Application ........................................................................... 206
Using the Data Adapter Preview ................................................................................................. 210
Using the CommandText Designer .............................................................................................. 211
Using the Data Adapter Designer ................................................................................................ 212
Using the Connection Editor Designer ......................................................................................... 213
Using Standard DataSets ............................................................................................................ 215
Using Typed DataSets ................................................................................................................. 220
Debugging Applications
Adding a Watch ........................................................................................................................... 224
Attaching to a Running Process .................................................................................................. 225
Setting and Modifying Breakpoints ............................................................................................. 226
Inspecting and Changing the Value of Data Elements ................................................................ 230
Resolving internal errors .............................................................................................................. 232
Modifying Variable Expressions ................................................................................................... 235
Preparing a Project for Debugging ............................................................................................. 236
Debugging Remote Applications ................................................................................................. 237
Editing Code
Using Code Folding ..................................................................................................................... 241
Customizing Code Editor ............................................................................................................. 242
Recording a Keystroke Macro ..................................................................................................... 243
Using Code Insight ...................................................................................................................... 244
Using Code Snippets ................................................................................................................... 246
Modeling
Adding Columns to a Component ................................................................................................ 248
Building an ECO-Enabled User Interface .................................................................................... 249
Building Applications with the ECO Framework .......................................................................... 252
Deploying an ECO-Enabled Application ...................................................................................... 253
Exporting a Code Visualization Diagram to an Image ................................................................. 254
Importing and Exporting a Model Using XMI ............................................................................... 255
Using the Model View Window and Code Visualization Diagram ................................................ 256
Using the Model View Window and ECO Class Diagram ............................................................ 258
Using the ECO Space Designer .................................................................................................. 261
Using the ECO Wizards ............................................................................................................... 265
Using the OCL Expression Editor ................................................................................................ 268
Using the Overview Window ........................................................................................................ 270
Source Control
Adding Files to the Source Control Project .................................................................................. 272
Checking In Files ......................................................................................................................... 273
Checking Out Files ...................................................................................................................... 274
Using the Commit Browser .......................................................................................................... 275
Configuring Source Control Providers ......................................................................................... 278
Connecting to the Source Control Repository ............................................................................. 279
Placing a Project into Source Control .......................................................................................... 280
7
Pulling a Project from Source Control .......................................................................................... 282
Removing Files from Source Control ........................................................................................... 285
Undoing a Check Out Operation .................................................................................................. 286
Running an SCC Application ....................................................................................................... 287
VCL for .NET
Building VCL Forms Applications With Graphics ......................................................................... 289
Building a VCL Forms MDI Application Without Using a Wizard ................................................. 290
Building a VCL Forms MDI Application Using a Wizard .............................................................. 295
Building a VCL Forms SDI Application ........................................................................................ 296
Building a Network Application with Socket Components ........................................................... 297
Building a VCL Forms dbExpress.NET Database Application ..................................................... 299
Building a VCL Forms ADO.NET Database Application .............................................................. 302
Creating Actions in a VCL Forms Application .............................................................................. 305
Building a VCL Forms "Hello world" Application .......................................................................... 308
Using ActionManager to Create Actions in a VCL Forms Application ......................................... 310
Building a VCL Forms Application ............................................................................................... 312
Building an Application with XML Components ........................................................................... 313
Displaying a Bitmap Image in a VCL Forms Application ............................................................. 317
Drawing a Polygon in a VCL Forms Application .......................................................................... 319
Drawing Rectangles and Ellipses in a VCL Forms Application .................................................... 320
Drawing a Rounded Rectangle in a VCL Forms Application ....................................................... 321
Drawing Straight Lines In a VCL Forms Application .................................................................... 322
Building a Multithreaded Application ............................................................................................ 324
Writing Cleanup Code .................................................................................................................. 325
Avoiding Simultaneous Thread Access to the Same Memory ..................................................... 326
Defining the Thread Object .......................................................................................................... 328
Handling Exceptions .................................................................................................................... 331
Using the Main VCL Thread ........................................................................................................ 332
Waiting for Threads ..................................................................................................................... 334
Writing the Thread Function ........................................................................................................ 336
Placing A Bitmap Image in a Control in a VCL Forms Application .............................................. 337
Creating a VCL Forms ActiveX Active Form ................................................................................ 339
Creating a VCL Forms ActiveX Button ........................................................................................ 342
Importing .NET Controls to VCL.NET .......................................................................................... 345
8
Reference
Delphi Overview
Delphi Overview
Program Organization .................................................................................................................. 349
Example Programs ...................................................................................................................... 351
Programs and Units
Programs and Units
Program Structure and Syntax .................................................................................................... 356
Unit Structure and Syntax ............................................................................................................ 358
Unit References and the Uses Clause ......................................................................................... 360
Using Namespaces (.NET Only)
Declaring Namespaces ................................................................................................................ 365
Searching Namespaces ............................................................................................................... 366
Using Namespaces ...................................................................................................................... 368
Syntactic Elements
Syntactic Elements
The Delphi Character Set ............................................................................................................ 371
The Delphi Character Set and Basic Syntax ............................................................................... 371
Comments and Compiler Directives ............................................................................................ 376
Declarations and Statements
Declarations ................................................................................................................................. 377
Statements ................................................................................................................................... 379
Simple Statements ....................................................................................................................... 379
Structured Statements ................................................................................................................. 381
Blocks and Scope ........................................................................................................................ 390
Expressions
Expressions ................................................................................................................................. 393
Function Calls .............................................................................................................................. 401
Set Constructors .......................................................................................................................... 402
Indexes ........................................................................................................................................ 402
Typecasts .................................................................................................................................... 402
Data Types, Variables, and Constants
Data Types, Variables, and Constants
About Types ................................................................................................................................. 407
Simple Types
Ordinal Types .............................................................................................................................. 409
Real Types ................................................................................................................................... 416
String Types
About String Types ...................................................................................................................... 419
Short Strings ................................................................................................................................ 420
Long Strings ................................................................................................................................. 421
WideString ................................................................................................................................... 421
Working with null-Terminated Strings .......................................................................................... 422
Structured Types
Sets .............................................................................................................................................. 426
9
Arrays .......................................................................................................................................... 427
Records ....................................................................................................................................... 432
File Types .................................................................................................................................... 435
Pointer Types
Overview of pointers .................................................................................................................... 437
Pointer Types ............................................................................................................................... 438
Procedural Types
About Procedural Types .............................................................................................................. 441
Procedural Types in Statements and Expressions ...................................................................... 442
Variant Types
Variants Overview ........................................................................................................................ 445
Variant Type Conversions ........................................................................................................... 445
Variants in Expressions ............................................................................................................... 447
Variant Arrays .............................................................................................................................. 447
OleVariant .................................................................................................................................... 448
Type Compatibility
Type Identity ................................................................................................................................ 449
Type Compatibility ....................................................................................................................... 450
Assignment Compatibility ............................................................................................................ 450
Declaring Types
Type Declaration Syntax .............................................................................................................. 452
Variables
Declaring Variables ...................................................................................................................... 454
Declared Constants
True Constants ............................................................................................................................ 457
Typed Constants .......................................................................................................................... 459
Procedures and Functions
Procedures and Functions
About Procedures and Functions ................................................................................................ 464
Declaring Procedures and Functions ........................................................................................... 464
Calling Conventions ..................................................................................................................... 467
Forward and Interface Declarations ............................................................................................. 468
External Declarations ................................................................................................................... 469
Overloading Procedures and Functions ...................................................................................... 471
Local Declarations ....................................................................................................................... 473
Parameters
About Parameters ........................................................................................................................ 475
Parameter Semantics .................................................................................................................. 475
String Parameters ........................................................................................................................ 479
Array Parameters ......................................................................................................................... 480
Default Parameters ...................................................................................................................... 483
Calling Procedures and Functions
Program Control and Parameters ................................................................................................ 485
Open Array Constructors ............................................................................................................. 485
Classes and Objects
Classes and Objects
Class Types ................................................................................................................................. 488
10
Inheritance and Scope ................................................................................................................. 490
Visibility of Class Members .......................................................................................................... 491
Forward Declarations and Mutually Dependent Classes ............................................................. 494
Fields
About Fields ................................................................................................................................. 496
Class Fields (.NET) ...................................................................................................................... 497
Methods
About Methods ............................................................................................................................. 498
Method Binding ............................................................................................................................ 500
Overloading Methods ................................................................................................................... 504
Constructors ................................................................................................................................ 505
The Class Constructor (.NET) ..................................................................................................... 506
Destructors .................................................................................................................................. 507
Message Methods ....................................................................................................................... 508
Properties
About Properties .......................................................................................................................... 510
Property Access ........................................................................................................................... 510
Array Properties ........................................................................................................................... 512
Index Specifiers ........................................................................................................................... 514
Storage Specifiers ....................................................................................................................... 515
Property Overrides and Redeclarations ....................................................................................... 515
Class Properties (.NET) ............................................................................................................... 517
Class References
Class-Reference Types ............................................................................................................... 519
Class Operators ........................................................................................................................... 520
Class Methods ............................................................................................................................. 521
Exceptions
About Exceptions ......................................................................................................................... 523
When To Use Exceptions ............................................................................................................ 523
Declaring Exception Types .......................................................................................................... 524
Raising and Handling Exceptions ................................................................................................ 525
Standard Exception Classes and Routines ................................................................................. 530
Standard Routines and I/O
Standard Routines and I/O
File Input and Output ................................................................................................................... 532
Text File Device Drivers ............................................................................................................... 535
Handling null-Terminated Strings ................................................................................................ 537
Other Standard Routines ............................................................................................................. 538
Libraries and Packages
Libraries and Packages
Calling Dynamically Loadable Libraries ....................................................................................... 543
Static Loading .............................................................................................................................. 543
Dynamic Loading ......................................................................................................................... 543
Writing Dynamically Loadable Libraries
Using Export Clause in Libraries ................................................................................................. 545
Library Initialization Code ............................................................................................................ 547
Global Variables in a Library ........................................................................................................ 548
11
Libraries and System Variables ................................................................................................... 548
Exceptions and Runtime Errors in Libraries ................................................................................ 549
Shared-Memory Manager (Win32 Only) ...................................................................................... 549
Packages
Understanding Packages ............................................................................................................. 550
Package Declarations and Source Files ...................................................................................... 550
Compiling Packages .................................................................................................................... 552
Object Interfaces
Object Interfaces
Interface Types ............................................................................................................................ 556
IInterface and Inheritance ............................................................................................................ 557
Interface Identification .................................................................................................................. 557
Calling Conventions for Interfaces ............................................................................................... 558
Interface Properties ..................................................................................................................... 559
Forward Declarations ................................................................................................................... 559
Implementing Interfaces
Class Declarations ....................................................................................................................... 560
Method Resolution Clause ........................................................................................................... 561
Changing Inherited Implementations ........................................................................................... 562
Implementing Interfaces by Delegation ....................................................................................... 562
Interface References
Implementing Interface References ............................................................................................. 565
Interface Assignment Compatibility ............................................................................................. 566
Interface Typecasts ..................................................................................................................... 566
Interface Querying ....................................................................................................................... 567
Automation Objects
Dispatch Interface Types ............................................................................................................. 568
Dispatch interface methods ......................................................................................................... 568
Dispatch interface properties ....................................................................................................... 569
Accessing Automation Objects .................................................................................................... 569
Automation Object Method-Call Syntax ....................................................................................... 569
Dual Interfaces ............................................................................................................................. 570
Memory Management
Memory Management
The Memory Manager (Win32 Only) ........................................................................................... 572
Variables ...................................................................................................................................... 572
Internal Data Formats
Integer Types ............................................................................................................................... 574
Character Types .......................................................................................................................... 574
Boolean Types ............................................................................................................................. 574
Enumerated Types ...................................................................................................................... 574
Real Types ................................................................................................................................... 574
Pointer Types ............................................................................................................................... 576
Short String Types ....................................................................................................................... 577
Long String Types ........................................................................................................................ 577
Wide String Types ....................................................................................................................... 577
Set Types ..................................................................................................................................... 578
12
Static Array Types ....................................................................................................................... 578
Dynamic Array Types .................................................................................................................. 578
Record Types .............................................................................................................................. 579
File Types .................................................................................................................................... 580
Procedural Types ......................................................................................................................... 582
Class Types ................................................................................................................................. 582
Class Reference Types ............................................................................................................... 583
Variant Types ............................................................................................................................... 583
Memory Management Issues on the .NET Platform
Constructors ................................................................................................................................ 505
Finalization ................................................................................................................................... 584
The Dispose Pattern .................................................................................................................... 585
Unit Initialization and Finalization ................................................................................................ 586
Unit Initialization Considerations for Assemblies and Dynamically Linked Packages ................. 587
Program Control
Program Control
Passing Parameters .................................................................................................................... 590
Handling Function Results ........................................................................................................... 592
Handling Method Calls ................................................................................................................. 592
Understanding Exit Procedures ................................................................................................... 593
Inline Assembly Code (Win32 Only)
Inline Assembly Code
Using the asm Statement ............................................................................................................ 596
Using Registers ........................................................................................................................... 596
Assembler Syntax
Assembler Statement Syntax ...................................................................................................... 597
Labels .......................................................................................................................................... 597
Instruction Opcodes ..................................................................................................................... 598
Assembly Directives .................................................................................................................... 599
Operands ..................................................................................................................................... 603
Expressions
Differences between Delphi and Assembler Expressions ........................................................... 605
Expression Elements ................................................................................................................... 606
Expression Classes ..................................................................................................................... 610
Expression Types ........................................................................................................................ 612
Expression Operators .................................................................................................................. 613
Assembly Procedures and Functions
Compiler Optimizations ................................................................................................................ 616
Function Results .......................................................................................................................... 617
.NET Topics
Using .NET Custom Attributes
Declaring a Custom Attribute Class ............................................................................................. 619
Using Custom Attributes .............................................................................................................. 620
Using the DllImport Custom Attribute .......................................................................................... 621
Custom Attributes and Interfaces ................................................................................................ 621
Command-Line Switches
13
IDE command-line switches and options
IDE command-line switches ........................................................................................................ 623
General options ........................................................................................................................... 623
Debugger options ........................................................................................................................ 624
Project options ............................................................................................................................. 624
14
Concepts
15
Getting Started
The Delphi 8 for .NET integrated development environment (IDE) provides many tools and features to
help you build powerful .NET applications quickly. Not all features and tools are available in all editions
of Delphi 8 for .NET. For a list of features and tools included in your edition, refer to the feature matrix
on www.borland.com/delphi_net.
16
What's Delphi 8 for .NET?
Delphi 8 for .NET is an integrated development environment (IDE) for building Delphi applications that
run in the Microsoft .NET environment. The Delphi 8 for .NET IDE provides a comprehensive set of tools
that streamline and simplify the development life cycle. The tools available in the IDE depend on the
edition of Delphi 8 for .NET you are using. The following sections briefly describe these tools.
Defining Requirements
Delphi 8 for .NET provides an interface to CaliberRM, a Web-based requirements definition and
management system designed to help control the product development process. Within the IDE, you
can access CaliberRM to collaborate on project requirements and ensure that your applications meets
end-user needs.
Modeling Applications
Modeling can help you can improve the performance, effectiveness, and maintainability of your
applications by creating a detailed visual design before you ever write a line of code. Delphi for .NET
provides UML-based class diagramming tools and a framework of Enterprise Core Objects (ECO) to
help you create model-powered .NET applications.
17
Controlling Access and Tracking Changes to Code
Source control systems enable team development by controlling access and tracking changes to source
code and other files. Delphi 8 for .NET uses the Microsoft Common Source Code Control API (SCC
API) to provide a common interface to StarTeam, CVS, ClearCase, and Visual SourceSafe. Within the
Delphi 8 for .NET IDE, you can perform common source control tasks, such as file check in, check out,
and synchronization.
Delphi 8 for .NET makes the entire Framework class library available in the IDE to help you
develop .NET applications. Delphi 8 for .NET enhances the Framework in the following areas:
• The Delphi 8 for .NET Borland Data Providers for .NET provide access to InterBase, Oracle, DB2
Universal, and Microsoft SQL Server databases.
• Several database utilities assist in performing tasks such as connecting to databases, browsing
and editing databases, and executing SQL queries.
• The .NET Menu Designers simplify the creation of main menus and context menus on Windows
Forms.
18
Tour of the IDE
When you start Delphi 8 for .NET, the integrated development environment (IDE) launches and displays
several tools and menus. The IDE helps you visually design user interfaces, set object properties, write
code, and view and manage your application in various ways.
The default IDE desktop layout includes some of the most commonly used tools. You can use the View
menu to display or hide certain tools. You can also customize and save the desktop layouts that work
best for you.
The tools available in the IDE depend on the edition of Delphi 8 for .NET you are using and include the:
• Welcome Page
• Forms
• Designer Surface
• Tool Palette
• Object Inspector
• Object Repository
• Project Manager
• Data Explorer
• Code Editor
• To-Do Lists
Welcome Page
When you open Delphi 8 for .NET, the Welcome Page appears with a number of options and links to
select from. As you develop a project or multiple projects, a table displays your most recent projects and
a timestamp of when each project was modified. This provides quick access to your most recent projects.
If you close the Welcome Page, you can reopen it by choosing View Welcome Page.
The Welcome Page contains buttons to access and open projects. You can also access the latest online
Help information by clicking the Help button. The Project button opens an existing project or project
group . The New button opens the Object Repository and lets you select from a variety of project
templates to create your desired .NET application. You can also choose File New Other and select
the template most appropriate for your user interface design.
Forms
Typically, a form represents a window or HTML page in a user interface. At design-time, a form is
displayed on the designer surface. You add components from the Tool Palette to a form to create your
user interface.
19
Delphi 8 for .NET provides three types of forms, as described in the following sections. Select the form
that best suits your application design, whether it’s a Web application that provides business logic
functionality over the Web, or a Windows application that provides processing and high-performance
content display. To switch between the designer and Code Editor, click their associated tabs below the
IDE.
Windows Forms
Use Windows Forms to build native Windows applications that run in a managed environment. You use
the .NET classes to build Windows clients which presents two major advantages—it allows application
clients to use features unavailable to browser clients, and it leverages the .NET Framework for
infrastructure. Windows Forms combine the best of both worlds, presenting a programming model that
takes advantage of a unified .NET Framework (for security and dynamic application updates, for
instance) and the richness of GUI Windows clients. You use Windows controls, such as buttons, list
boxes, and text boxes, to build your Windows applications.
To access a Windows Form, choose File New Other and select the Application icon from the
Projects directory.
To access an ASP.NET Web Form, choose File New Other and select the ASP.NET Web
application icon from the ASP Projects directory.
Janeva
Use Janeva to create applications that use VCL.NET components to run in the .NET Framework. You
use the Borland Visual Component Library for .NET classes to create a Janeva application.
Janeva are especially useful if you want to port an existing Delphi application containing VCL controls
to the .NET environment, or if you are already familiar with the VCL and prefer to use it.
To access a Janeva, choose File New Other and select the VCL.NET application icon from the
Delphi for .NET Projects directory.
HTML Designer
Use the HTML designer to view and edit ASP.NET Web Forms or HTML pages. This designer provides
a Tag Editor for editing HTML tags alongside the visual representation of the form or page. You can
also use the Object Inspector to edit properties of the visible items on the HTML page and to display
20
the properties of any current HTML tag in the Tag Editor. A combo box located above the Tag Editor
lets you display and edit SCRIPT tags.
To create a new HTML file, choose File New Other and select HTML page from the Markup File
directory. When the HTML page is displayed, an Insert menu is added to the main menu. Use this menu
to dynamically insert tables, images, user controls, and more.
Designer Surface
The designer surface, or designer, is displayed automatically when you are using a form. The
appearance and functionality of the designer depends on the type of form you are using. For example,
if you are using an ASP.NET Web Form, the designer will display an HTML tag editor. To access the
designer, click the Design tab at the bottom of the IDE.
Visual Components
Visual components appear on the form at design-time and are visible to the end user at runtime. They
include such things as buttons, labels, toolbars, and listboxes.
When you add an nonvisual component to a form, they are displayed in the component tray at the bottom
of the designer surface. The component tray lets you distinguish between visual and nonvisual
components.
Tool Palette
The Tool Palette contains items to help you develop your application. The items displayed depend on
the current view. For example, if you are viewing a form on the designer, the Tool Palette displays
components that are appropriate for that form. You can double-click a control to add it to your form. If
you are viewing code in the Code Editor, the Tool Palette displays code snippets that you can add to
your code.
Customized Components
In addition to the components that are installed with Delphi 8 for .NET, you can add customized or third
party components to the Tool Palette and save them in their own category.
21
Component Templates
You can create templates that are made up of one or more components. After arranging components
on a form, setting their properties, and writing code for them, you can save them as a component
template. Later, by selecting the template from the Tool Palette, you can place the preconfigured
components on a form in a single step; all associated properties and event-handling code are added to
your project at the same time. You can reposition the components independently, reset their properties,
and create or modify event handlers for them just as if you had placed each component in a separate
operation.
Object Inspector
The Object Inspector lets you set design-time properties and create event handlers for components.
This provides the connection between your application’s visual appearance and the code that makes
the application run. The Object Inspector contains two tabs: Properties and Events.
Use the Properties tab to change physical attributes of your components. Depending on your selection,
some category options let you enter values in a text box while others require you to select values from
a drop-down box. For Boolean operations, you toggle between True or False. After you change your
components’ physical attributes, you create event handlers that control how the components function.
Use the Events tab to specify the event of a specific object you select. If there is an existing event
handler, use the drop-down box to select it. By default, some options in the Object Inspector are
collapsed. To expand the options, click the plus sign (+) next to the category.
Certain nonvisual components, for example, the Borland Data Providers, allow quick access to editors
such as the Connection Editor and Command Text Editor. You can access these editors in the Designer
Verb area at the bottom of the Object Inspector. To open the editors, point your cursor over the name
of the editor until your cursor changes into a hand and the editor turns into a link. Alternatively, you can
right-click the nonvisual component, scroll down to its associated editor and select it. Note that not all
nonvisual components have associated editors. In addition to editors, this area can also display
hyperlinks to show custom component editors, launch a web page and show dialog boxes.
Object Repository
To simplify development, Delphi 8 for .NET offers predesigned templates, forms, and other items that
you can easily access and use in your application. The Object Repository is accessible by choosing
File New Other. A New Items dialog box appears. You can also edit or remove existing objects
from the Object Repository by right-clicking the Object Repository to view your editing options.
Note: Although the dialog box is called New Items, this is actually the Object Repository.
22
Inside the Object Repository
The Object Repository contains items that address the types of .NET applications you can develop. It
contains templates, forms, and many others items. You can create projects such as class library, control
library, console applications, HTML pages, and many others by accessing the available templates.
Project Manager
A project is made up of several application files. The Project Manager lets you view and organize your
project files such as forms, executables, assemblies, objects and library files. Within the Project
Manager, you can add, remove, and rename files. You can also combine related projects to form project
group, which you can compile at the same time.
Add References
You can integrate your legacy COM servers and ActiveX controls into managed applications by adding
references to unmanaged DLLs to your project, and then browse the types just as you would with
managed assemblies. Choose Project Add Reference to integrate your legacy COM servers or
ActiveX controls. Alternatively, right-click the Reference folder in the Project Manager and click Add
Reference. You can add other .NET assemblies, COM/ActiveX components, or type libraries using the
Add Reference feature.
23
8 for .NET generates all the interfaces and class definitions you need for calling that Web Service. To
use the Add Web Reference feature, from your Project Manager, right-click the Web Services node.
Data Explorer
The Data Explorer lets you browse database server-specific schema objects including tables, fields,
stored procedure definitions, triggers, and indexes. Using the context menus, you can create and
manage database connections. You can also drag and drop data from a data source to a project to build
your database application quickly.
Code Editor
As you design the user interface for your application, Delphi 8 for .NET generates the underlying code.
When you select and modify the properties of your objects, your changes are automatically reflected in
the source files. The Code Editor gives you the flexibility of writing additional code directly into the Code
Editor because it’s a full-feature ANSI editor.
Because all your programs share common characteristics, Borland Delphi 8 for .NET supplies
autogenerated code to get you started. Do not modify the autogenerated code for the Initialize
Components method. Doing so will cause your form to disappear when you click the Design tab. You
can think of the autogenerated code as an outline that you can examine to create your program.
Additionally, Delphi 8 for .NET provides supplemental tools to help you write code such as code insight,
code browsing, code snippets, and code folding.
Code Insight
Code Insight refers to a subset of features embedded in the Code Editor that aid in the code writing
process. These features display context-sensitive pop-up windows and provide the following services:
• Help identify common statements you wish to insert into your code.
• Assist in the selection of properties and methods.
• Display events available for a particular class.
• Provide view declaration information for identifiers.
To enable and configure Code Insight features, choose Tools Options and click Code Insight.
Code Browsing
The Code Editor has browse buttons similar to those found in Web browsers. To display the browse
buttons, right-click a empty area of the IDE toolbar and select Browser. As you move through your files,
the Code Editor keeps track of where you have been in the code. You can click the drop-down arrow
next to the browse buttons to move forward and backward through a history of these references.
24
Code Snippets
Code snippets are commonly used programming statements, such as if, while, and for statements,
that you can insert into your code. When the Code Editor is open, you can double-click a code snippet
on the Tool Palette to add it to your code. You can also create your own code snippets by selecting code
in the Code Editor, pressing the ALT key, and dragging the code to the Tool Palette.
Code Folding
Code folding lets you collapse sections of the code to create a hierarchal view of your code and to make
it easier to read and navigate. The collapsed code is not deleted, but hidden from view. To use code
folding, click the plus and minus signs next to the code.
To-Do Lists
A to-do list records tasks that need to be completed for a project. After you add a task to the to-do list,
you can edit the task, add it to your code as a comment, indicate that it has been completed, and remove
it from the list. You can filter the list to display only those tasks that interest you.
25
Starting a Project
A project is a collection of files which includes, but is not limited to, project files (name.bdsproj),
assemblies (System.dll), program database files (name.pdb), optional resource files (.html, .jpeg, .gif),
executables (name.exe), and many others that make up an application. Projects are either created at
design time or generated when you compile the project source code. To assist in the development
process, the Object Repository offers many predesigned templates, forms, files, and other items that
you can use to create .NET applications.
To create a project, click New from the Welcome Page and select the type of application you want to
create, or choose File New Other. To open an existing project, click Project from the Welcome
Page or choose File Open Project.
Type of Projects
Depending on the edition of Delphi 8 for .NET that you are using, you can create traditional Windows
applications, ASP.NET Web applications, ADO.NET database applications, Web Services applications,
and many others. Delphi 8 for .NET also supports assemblies, custom components, multi-threading,
and COM. For a list of the features and tools included in your edition, refer to the feature matrix on www.
borland.com/delphi_net.
Windows Applications
You can create Windows applications using Windows Forms to provide processing and high-
performance content display. Windows applications can function as a front end to ADO.NET databases.
In addition to drag and drop components and visual designers, Borland provides an easy way to create
application menus and submenus. The .NET Menu Designers MainMenu and ContextMenu are
components that work like editors to let you visually design menus and quickly code their functionality.
26
ASP.NET Web Services Applications
You can create Web Services applications that deliver content, such as HTML pages or XML documents,
over the Web. Web Services is an internet-based integration methodology that allows applications to
connect through the Web and exchange information using standard messaging protocols.
Delphi 8 for .NET simplifies the creation of Web Services by providing methods for creating a SOAP
Server application. The .asmx and .dlls files are created automatically and you can test the Web Service
within the IDE, without writing a client application for it.
When writing a client application that uses, or consumes, a published Web Service, you can use the
UDDI Browser to locate and import WSDL that describes the Web Service into your client application.
VCL.NET Applications
You can use Janeva to create a .NET Windows application that uses components from the VCL.NET
framework.
Delphi 8 for .NET simplifies the task of building .NET-enabled applications by supporting VCL
components that have been augmented to run on the .NET Framework. This eliminates the need for
you to create custom components to provide standard VCL component capabilities. This makes the
process of porting Win32 applications to .NET much simpler and reliable.
Database Applications
Whether your application uses Windows Forms, Web Forms, or VCL Forms, Delphi 8 for .NET has
several tools that make it easy to connect to a database, browse and edit a database, execute SQL
queries, and display live data at design time.
The ADO.NET framework data providers let you access MS SQL, Oracle, and ODBC and OLE DB-
accessible databases. The Borland Data Providers (BDP.NET) let you access MS SQL, Oracle, DB2,
and InterBase databases. You can connect to any of these data sources, expose their data in datasets,
and use SQL commands to manipulate the data. Using BDP.NET provides the following advantages:
• Portable code that's written once and connects to any supported database.
• Open architecture that allows you to provide support for additional database systems.
• Logical data types that map easily to .NET native types.
• Consistent data types that map across databases, where applicable.
• Unlike OLE DB, there is no need for a COM/Interop layer.
When using Janeva and the VCL.NET framework components, you can extend database support even
further by using the BDE.NET, dbExpress.NET, and Midas Client for .NET connection technologies.
Model-Driven Applications
Modeling is a term used to describe the process of software design. Developing a model of a software
system is roughly equivalent to an architect creating a set of blueprints for a large development project.
27
Like a set of blueprints, a model not only depicts the system as a whole, but also allows you to focus in
on specifics such as structural and behavioral details. Abstracted away from any particular programming
language (and at some levels, even from specific technology), the model allows all participants in the
development cycle to communicate in the same language.
Borland's Model Driven Architecture (MDA) describes an approach to software engineering where the
modeling tools are completely integrated within the development environment itself. The MDA is
designed around Borland’s Enterprise Core Objects (ECO) framework. The ECO framework is a set of
interface, classes, and custom attributes that provide the communication conduit between your
application and the modeling-related features of the IDE.
Delphi 8 for .NET IDE leverages the ECO framework to provide an integrated surface on which to
develop your application model. The IDE and its modeling surface features include:
• Creating model-driven applications as a new kind of project.
• Creating class diagrams, and manipulating model elements (packages, and classes) directly on the
surface.
• Adding, removing, and changing class attributes and methods on the class diagram.
• Two-way updating between source code and the modeling surface. Changes in source code are
reflected in the graphical depiction, and vice versa.
• Two-way navigating between model elements and source code. You can navigate from the
graphical depiction of a model element directly to its corresponding source code. Similarly, you can
navigate from a modeled class in source code directly to its graphical diagram on the modeling
surface.
• Exporting and importing models using XMI 1.1.
Note: Not all modeling features are available in all editions of Delphi 8 for .NET. To
determine the modeling features supported in your product edition, refer to the feature
matrix on www.borland.com/delphi_net.
28
Assemblies
An assembly is a logical package, much like a DLL file, that consists of manifests, modules, portable
executable (PE) files, and resources (.html, .jpeg, .gif) and is used for deployment and versioning. An
application can have one or more assemblies that are referenced by one or more applications,
depending on whether the assemblies reside in an application directory or in a global assembly cache
(GAC).
Additional Projects
In addition to the project types described above, Delphi 8 for .NET provides templates to create class
libraries, control libraries, console applications, Visual Basic applications, reports, text files, and more.
These templates are stored in the Object Repository and you can access them by choosing File
New Other.
29
Managing the Development Life Cycle
The application development life cycle is an iterative process that involves designing, developing,
testing, debugging, and deploying applications. Delphi 8 for .NET provides powerful tools to support this
iterative process, including integrated source control, form design tools, the Delphi for .NET compiler,
an integrated debugging environment, and installation and deployment tools.
30
Managing the Development Cycle Overview
The development cycle as described here is a subset of Application Lifecycle Management (ALM),
dealing specifically with the part of the cycle that includes the implementation and control of actual
development tasks. It does not include such things as modeling applications. Delphi 8 for .NET provides
a framework of tools that helps you manage and perform all of your development requirements.
Invoke the source control system in a separate process if you need to use specific features of that
system, which are not exposed in the Delphi 8 for .NET IDE. The source control application appears in
a separate window.
In most cases, you manage your project files from within the Delphi 8 for .NET IDE. The integration
provided allows you to check-in, check-out, update, commit, and otherwise manage your source files
using a simplified user interface. The integration supports the level of multi-user capabilities provided
by your specific source control system.
31
Code Visualization
The Code Visualization feature of Delphi 8 for .NET provides the means to document and debug your
class designs using a visual paradigm. As you load your projects and code files, you can use the Model
View window to get both a hierarchical graphical view of all of the objects represented in your classes,
as well as a UML-like model of your application objects. This feature can help you visualize the
relationships between objects in your application, and can assist you in developing and implementing.
The integrated debugger allows you to set watches and breakpoints, and to step through, into, and over
individual lines of code. A set of debugger windows provides details on variables, processes, and
threads, and lets you drill down deeply into your code to find and fix errors.
Because the entire set of processes is both menu and shortcut key driven, you can develop your own
personal style of programming that is seamless and integrated as much as possible, making the entire
development lifecycle effective and painless.
32
Using Source Control
Borland's Delphi 8 for .NET provides integration with several source control systems, allowing you to
perform the most common source control tasks from within the development environment. Additionally,
you can write your own interfaces to unsupported source control systems, using the Microsoft SCC API.
The Delphi 8 for .NET development environment supports the following source code control systems:
• Borland StarTeam and the StarTeam Microsoft SCC Integration
• Concurrent Versions System (CVS) GUI implementations that support SCC API
• Rational ClearCase
• Microsoft Visual SourceSafe
Your source code control system typically consists of server and client components. On the server side,
the system maintains a database repository that captures a complete snapshot of your project files and
incremental changes (deltas or differences) to those files. On your local client, you use the source control
client software to manage access to projects and files stored in the server repository, to maintain an
audit trail of changes you make to the projects and files, and to provide you with conflict management
capabilities. In Delphi 8 for .NET the Source Control Manager is an integrated feature that allows you
33
to connect to your source control system on a remote server, to place projects into and pull projects
from your source control repository, and to check in, check out, merge, and compare files.
Delphi 8 for .NET uses the Microsoft SCC API to manage interaction with source control systems. You
must use a system that includes an integration to the SCC API. Some products such as StarTeam,
provide a separately installable SCC Integration application. Other products, like CVS, do not directly
support the SCC API, but a variety of 3rd-party clients built as front-end applications to CVS do provide
the integration. Any integration must support MSSCCI 1.1.
Most source control systems adhere to a concept of a logical project, within which files are stored, usually
in one or more tree directory structures. A source control system project might contain one or many
Delphi 8 for .NET projects, in addition to other documents and artifacts. The system also enforces its
own user authentication or, very often, takes advantage of the authentication provided by the underlying
operating system. Doing so allows the source control system to maintain an audit trail or snapshot of
updates to each file. These snapshots are typically referred to as diffs, for differences. By storing only
the differences, the source control system can keep track of all changes with minimal storage
requirements. When you want to see a complete copy of your file, the system performs a merge of the
differences and presents you with a unified view. At the physical level, these differences are kept in
separate files until you are ready to permanently merge your updates, at which time you can perform a
commit action.
This approach allows you and other team members to work in parallel, simultaneously coding within
multiple, shared projects, without the danger of code collision. Source control systems, in their most
basic form, protect you from code conflicts and loss of early sources. Most source control systems give
you the tools to manage code files with check in/check out capabilities, conflict reconciliation, and
reporting capabilities. Most systems do not include logic conflict reconciliation or build management
capabilities, although some products such as Rational ClearCase do provide build and workspace
management capabilities. For details about your particular source control system capabilities, refer to
the appropriate product documentation provided by your source control system vendor.
Commonly, source control systems only allow you to compare and merge updates to text files, including
code files and other types of documents. The source control systems supported by Delphi 8 for .NET
allow you to include binary files in the projects you place under control, but you cannot compare or merge
multiple sources of those files. Typically, when you move binary files, or any files that are not textual,
into your source control system, the system overwrites any existing copies of those files with newer
sources. Resource files or other types of automatically generated binaries might also be overwrittten or
ignored by the source control system. If you need to maintain copies of different sources of these types
of files, you might consider creating a manual tracking system for those files.
34
In Delphi 8 for .NET, you can access any supported and installed source control system from the Team
menu.
You can install and use multiple source control systems from within Delphi 8 for .NET. When you perform
certain tasks you are required to log in to the source control system repository of your choice.
Repository Basics
Source control systems store copies of files and difference files in some form of database repository. In
some systems, such as CVS or VSS, the repository is a logical structure that consists of a set of flat
files and control files. In other systems, the repositories are instances of a particular database
management system (DBMS) such as InterBase, Microsoft Access, MS SQL Server, IBM DB2, or
Oracle.
Repositories are typically stored on a remote server, which allows multiple users to connect, check out
files, check in files, and perform other management tasks simultaneously. As such, you need to make
sure that you establish connectivity not only with the server, but also with the database instance. Check
with your network, system, and database administrators to make sure your machine is equipped with
the necessary drivers and connectivity software, in addition to the client-side source control software.
Some source control systems allow you to create a local repository in which you can maintain a snapshot
of your projects. Over time the local image of your projects differs from the remote repository. You can
establish a regular policy for merging and committing changes from your local repository to the remote
repository. In a small shop, you can install the repository on your local machine and give shared access
to that machine to other users. Generally, it is not safe to give each member of your team a separate
repository on a shared project. If you are each working on completely separate projects and you want
to keep each project under source control locally, you might use individual local repositories. Even in
this case, however, you can create multiple repositories on a remote server, which provides centralized
support, backup, and maintenance.
35
Working with Projects
Source control systems, like development environments, use the project concept to organize and track
groups of related files. No matter which source control system you use, you create a project that
maintains your file definitions and locations. You also create projects in Delphi 8 for .NET to organize
the various assemblies and source code files for any given application. Delphi 8 for .NET stores the
project parameters in a project file. You can store the project file in your source control system project,
in addition to the various code files you create. You might share your project file among all the developers
on your team, or you might each maintain a separate project file. Most source control systems consider
development environment project files to be binary, whether they are actually binary files or not. As a
consequence, when you check a project file into a source control system repository in which the same
project file or a project file with the same name is already stored, the source control system overwrites
the older file with the newer one. The same is true when you pull a file, or check it out, from the repository
onto your local system, if the same project file or a project file with the same name is located there.
Synchronizing Files
One of the most powerful features of any source control system is its ability to synchronize multiple
instances of the same file, to compare lines of text and mark those that overlap, then to merge the file
instances without destroying conflicting lines of code or text. Most systems also provide the capability
to update your local instance of a file or even an entire project’s files with the latest source of those files
stored in the source control system repository, without destroying any new lines of code you’ve added
to the local instance of the file. You can perform these synchronization operations using the Commit
Browser from within Delphi 8 for .NET.
36
Designing User Interfaces
A graphical user interface (GUI) consists of one or more windows that let users interact with your
application. At design time, those windows are called forms. Delphi 8 for .NET provides a designer for
creating Windows Forms, Web Forms, Janeva, and HTML pages. The designer and forms help you
create professional-looking user interfaces quickly and easily.
The Tool Palette provides dozens of controls to simplify the creation of Windows Forms, Web Forms,
and HTML pages. When creating a Windows Form, for example, you can use the MainMenu component
to create a customized main menu in minutes. After placing the component on a Windows Form, you
type the main menu entries and commands in the boxes provided. The ContextMenu component
provides similar functionality for creating context menus. There are also several dialog box components
for commonly performed functions, such as opening and saving files, setting fonts, selecting colors, and
printing. Using these components saves time and provides a consistent look and feel for the dialogs in
your application.
As you design your user interface, you can undo and repeat previous changes to a form by choosing
Edit Undo and Edit Redo. When you are satisfied with the appearance of the form, you can lock
the components and form to prevent accidental changes by right-clicking the form and choosing Lock
Controls.
37
Code Visualization Overview
The Code Visualization feature is available in both the Enterprise and Architect versions of Delphi 8 for .
NET. All other modeling tools and information related to modeling relates only to the Architect version
of Delphi 8 for .NET.
38
of your projects in a tree, as opposed to the file-centric view of the Project Manager window. Each project
in a project group is a top-level node in the Model View tree.
Nested within each project tree-node, you will find UML packages. Each UML package corresponds to
a .NET namespace declaration in your source code (.NET namespaces can span multiple source files).
You can expand the UML package to reveal the types declared within.
The UML term for interface implementation is realization. Similar to the case of inheritance, the IDE
creates a realization link when it sees a class declaration that implements an interface. The realization
link appears within the implementor class in the Model View tree, and on the diagram as a dotted line
with a hollow arrowhead pointing at the interface. There will be one such realization link for every
interface implemented by the class.
Associations
In the UML, an association is a navigational link produced when one class holds a reference to another
class (for example, as an attribute or property). Code visualization creates association links when one
class contains an attribute or property that is a non-primitive data type. On the diagram the association
link exists between the class containing the non-primitive member, and the data type of that member.
Standard UML syntax is used to display the UML declaration of attributes, operations, and properties.
Each of the four categories can be independently expanded or collapsed to show or hide the members
within.
39
Compiling, Building, and Running Applications
As you develop your application, you can compile, build, and run the application in the IDE. While all
three operations can produce either an executable (.exe) or an assembly (.dll), they differ slightly in
behavior:
• Compiling a project compiles only the source code in the current project that has changed since
the last build, but does not execute the application.
• Building a project compiles all of the source code in the current project, regardless of whether any
source code has changed. Building is useful when you are unsure which files have changed, or if
you have changed project or compiler options.
• Running a project compiles any changed source code and, if the compile is successful, executes
your application, allowing you to use and test it in the IDE.
Use the commands on the Project and Run menus to compile, build, and run your project.
Compiler Options
You can set many of the compiler options for a project by choosing Project Options and selecting
the Compiler page. Most of the options on the Compiler page correspond to a compiler option and are
described in the online Help for that page.
On the Compiler page, you can also save compiler options as an option set. This lets you quickly change
options based on your development activity. For example, you can set compiler options specific to
debugging your project, and then change the option set when you are done debugging it.
If you need to specify additional compiler options, you can invoke the compiler from the command line.
For a complete list of the compiler options and information about running the compiler from the command
line, see the Language Guide section of this Help system.
As you compile your project, you can display the current compiler options in the Messages window.
Choose Tools Options Environment Options and select the Show command line option. The next
time you compile a project, the command used to compile the project and the response file will displayed
in the Messages window. The response file lists the compiler options and the files to be compiled.
After you compile a project, you can display information about it by choosing Project Information.
The resulting Information dialog box displays the number of lines of source code compiled, the byte size
of your code and data, the stack and file sizes, and the compile status of the project.
40
Compiler Errors
As you compile a project, compiler messages are displayed in the Messages window. For an explanation
of a message, select the message and press F1.
41
Using Translation Tools
Delphi 8 for .NET includes a suite of Translation Tools to facilitate localization and development of .NET
applications for different locales. The Translation Tools include the following:
• Satellite Assembly Wizard
• Translation Manager
• Translation Repository
The Satellite Assembly Wizard lets you add languages to your project and creates a satellite assembly
for each of those languages. You can add new satellite assemblies to a project at any time. If you have
multiple projects open in the IDE, you can process several at once. The Satellite Assembly Wizard can
also help you remove and restore languages.
Translation Manager
After satellite assemblies have been added to your project, you can use the Translation Manager to view
and edit forms and resource strings. After modifying your translations, you can update all of your
application’s satellite assemblies.
The External Translation Manager (ETM) is a version of the Translation Manager that you can set up
and use outside of the IDE. The ETM has the same functionality as the Translation Manager, with some
additional menus and toolbars.
Translation Repository
The Translation Repository provides a central database for translations that can be shared across
projects, by different developers. While working in the Translation Manager, you can store translated
strings in the Repository and retrieve translated strings from the Repository.
By default, each time your assemblies are updated, they will be populated with translations for any
matching strings that exist in the Repository. You can also access the Repository directly, through its
own interface, to find and edit strings or delete unwanted strings.
42
The Translation Repository stores data in XML format. By default, the file is named default.tmx and is
located in the Delphi 8 for .NET\bin directory.
.nfn The Translation Tools maintain a separate .nfn file for each form in your application and each
target language. These files contain the data (including translated strings) that you see in the
Translation Manager.
.resx The Satellite Assembly Wizard uses the compiler-generated .drcil file to create an .resx file for
each target language. These .resx files contain special comments that are used by the Translation
Tools.
.tmx The Translation Repository stores data in an .tmx file. You can maintain more than one repository
by saving multiple .tmx files.
.bdsproj The External Translation Manager lists the satellite assemblies (languages) and resources to be
translated into a .bdsproj project file. When third-party translators add and remove languages
from a project, they can save these changes in an .bdsproj file, which they return to the developer.
43
Debugging Applications
The integrated debugger lets you find and fix both runtime errors and logic errors in your Delphi 8
for .NET application. Using the debugger, you can step through code, set breakpoints and watches, and
inspect and modify program values. As you debug your application, the debug windows are available
to help you manage the debug session and provide information about the state of your application. The
remote debug server lets you debug a Delphi 8 for .NET application running on a remote computer.
The Run menu provides the Trace Into and Step Over commands. Both commands tell the debugger
to execute the next line of code. However, if the line contains a function call, Trace Into executes the
function and stops at the first line of code inside the function. Step Over executes the function, then
stops at the first line after the function.
Evaluate/Modify
The Evaluate/Modify functionality allows you to evaluate an expression for which you've set a breakpoint.
You can also pass expression values to the currently running process. For instance, you can modify a
value for a variable and insert that value into the variable, which you might want to do if that value is to
be passed to another method at some point during the execution of the application. This allows you to
provide values in-process, which you might otherwise not be able to do. The Evaluate/Modify
functionality is customized to whichever implementation language you are using and that is supported
by the product.
Breakpoints
Breakpoints pause program execution at a certain point in the program or when a particular condition
occurs. You can then use the debugger to view the state of your program, or step over or trace into your
code one line or machine instruction at a time. The debugger supports two types of breakpoints. Source
breakpoints pause execution at a specified location in your source code. Address breakpoints pause
execution at a specified memory address.
44
Watches
Watches lets you track the values of program variables or expressions as you step over or trace into
your code. As you step through your program, the value of the watch expression changes if your program
updates any of the variables contained in the watch expression.
Debug Windows
The following debug windows are available to help you debug your program. By default, most of the
windows are displayed automatically when you start a debugging session. You can also view the
windows individually by using the View Debug Windows sub-menu.
Each window provides one or more right-click context menus. The F1 Help for each window provides
detailed information about the window and the context menus.
Breakpoint List Displays all of the breakpoints currently set in the Code Editor or Disassembly window.
Call Stack Displays the current sequence of function calls.
Watch List Displays the current value of watch expressions based on the scope of the execution point.
Local Variables Displays the current function’s local variables, enabling you to to monitor how your program
updates the values of variables as the program runs.
Modules Displays processes under control of the debugger and the modules currently loaded by each
process. It also provides a hierarchical view of the namespaces, classes, and methods used in
the application.
Threads Status Displays the status of all processes and threads of execution that are executing in each
application being debugged. This is helpful when debugging multi-threaded applications.
Event Log Displays messages that pertain to process control, breakpoints, output, threads, and module.
Disassembly Displays the low-level state of your program, including the assembly instructions for each line
of source code and the contents of certain registers.
Remote Debugging
Remote debugging lets you debug a Delphi 8 for .NET .NET application running on a remote computer.
Your computer must be connected to the remote computer through TCP/IP. After you install and run the
remote debug server, and copy the required application files to the remote computer, you can use Delphi
8 for .NET to connect to that computer and begin debugging.
45
Deploying Applications
This section provides general information about deploying .NET applications. For more detailed
information, see the .NET Framework SDK online Help, or the topics listed at the end of this section.
After you have written, tested, and debugged your application, you can make it available to others by
deploying it. Depending on the size and complexity of the application, you can package it as one or more
assemblies, as compressed cabinet (.cab) files, or in an installer program format (such as .msi). After
the application is packaged, you can distribute it by using XCOPY, FTP, as a download, or with an
installer program.
Simple Applications
Assuming that the target computer already has the .NET Framework installed on it, deploying a simple
application that consists of a single executable is as easy as copying the .exe file to the target computer.
You don't need to register the application and deleting the application files effectively uninstalls it.
Installation Programs
For more complex applications that consist of multiple files, you can use an installation tool, such as
InstallShield Express. InstallShield Express can be installed from the Delphi 8 for .NET installation CD.
After installing it, refer to the online InstallShield online Help for information about using the product.
However, if you build the application by compiling the application to have external references to VCL
for .NET assemblies, the application will have external dependencies on the .NET Framework, the
46
Borland.Delphi.dll, and whatever Delphi 8 for .NET packages you have added to the project references,
for example, Borland.VclRtl.dll or Borland.Vcl.dll.
Some of the files that are associated with Delphi 8 for .NET applications are subject to redistribution
limitations or cannot be redistributed at all. Refer to the following documents for the legal stipulations
regarding the redistribution of these files.
File Description
Deploy.txt Contains deployment considerations for each edition of Delphi 8 for .NET.
License.txt Addresses legal rights and obligations concerning Delphi 8 for .NET.
Readme.txtContains last minute information about Delphi 8 for .NET, possibly including information that could affect
the redistribution rights for Delphi 8 for .NET files.
47
Modeling with Delphi 8 for .NET
Delphi 8 for .NET's integrated modeling tools tie together the processes of design and development.
The class diagramming tools integrated into the IDE are based on well known industry standards such
as UML and OCL. The Enterprise Core Object (ECO) framework implements the UML version 1.4
Metamodel, and leverages the .NET framework to make the model available at both designtime and
runtime. This section provides an overview of UML concepts and terminology, the ECO framework, and
the IDE's built in graphical modeling tools.
Please note that the Code Visualization feature is available in both Enterprise and Architect versions of
the product. As such, any information about this feature refers to both versions. All other modeling
features relate only to the Architect version of Delphi 8 for .NET and the information contained herein
refers only to modeling tools and features in the Architect version.
48
Modeling Tools Overview
With the exception of the Code Visualization feature, all material regarding modeling and modeling tools
herein relates only to the Architect version of Delphi 8 for .NET. The information about Code Visualization
refers to both the Enterprise and Architect versions of Delphi 8 for .NET. This topic contains material
adapted from the Together ControlCenter User Guide.
Modeling is a term used to describe the process of software design. Developing a model of a software
system is roughly equivalent to an architect creating a set of blueprints for a large development project.
Like a set of blueprints, a model not only depicts the system as a whole, it also allows you to focus in
on specifics such as structural and behavioral details. Abstracted away from any particular programming
language (and at some levels, even from specific technology), the model allows all participants in the
development cycle to communicate in the same language. Borland's design driven architecture is an
approach to software engineering where the modeling tools are completely integrated within the
development environment itself. The software development process therefore centers primarily around
the model, rather than the nuts and bolts of source code.
Central to Delphi 8 for .NET's modeling toolset is a powerful class library called the Enterprise Core
Objects (ECO) framework. The ECO framework is both a designtime and a runtime platform that
leverages the .NET Framework to put the system model at the center of your development activities.
The modeling tools in Delphi 8 for .NET are based on important industry standards published by the
Object Management Group (OMG). These are, the OMG Unified Modeling Language Specification, the
Object Constraint Language Specification, and the OMG XML Metadata Interchange (XMI)
Specification. These specifications are freely available at the OMG website; see the link at the end of
this topic for more information. This topic presents an overview of these concepts:
• Basic modeling and UML concepts.
• Introduction to the Enterprise Core Objects framework.
• Integrated modeling tools in the Delphi 8 for .NET IDE.
49
Model-Powered Applications and the ECO Framework
The ECO framework is both a design-time and a run-time platform. The model is the central hub of
activity for designers and developers; the ECO framework carries the design driven philosophy all the
way through to the application end-users.
50
Note: This topic covers Delphi 8 for .NET's built-in UML modeling tools, which are based
entirely on the ECO framework. For information on code visualization, and producing
a class diagram from an arbitrary set of Delphi source code, please refer to the Code
Visualization link at the bottom of this topic.
UML Packages
A UML package is a unit of organization analogous to a .NET namespace. Using packages, you can
partition classes and other datatypes along just about any lines that make sense for your particular
application. Usually classes are created along functional lines. For example, you could create a package
to hold a set of user interface components. Like namespaces, UML packages can be nested within each
other.
Note: While the concept of a UML package and a namespace are analogous, the UML
packages you create with Delphi 8 for .NET are not implemented as .NET namespaces
in source code. This is a major difference between the code visualization diagram, and
the UML diagramming tools. When producing a code visualization diagram from an
arbitrary Delphi source file, a .NET namespace is represented with a UML package
symbol. On the code visualization diagram .NET namespaces are drawn in white, as
opposed to ECO-enabled UML packages, which are drawn in yellow.
A class diagram focuses on the internal structure and the relationships of the classes in the model. The
internal structure of a class consists of its attributes (including derived attributes), and operations. The
kinds of relationships shown on the class diagram are inheritance (known as generalization in the UML),
and links such as associations (including derived associations). You can also add OCL invariant
constraints and query expressions to the model; you can call the ECO framework at runtime to enforce
the constraints or to execute the query.
Note: As with .NET namespaces and UML packages, classes on the code visualization
diagram are drawn in white, while ECO-enabled classes are drawn in yellow.
51
of an Employee class cannot be set to null. You can then use the ECO framework to enforce this
constraint at runtime.
• Creating derived associations and attribute values. Derived associations and attribute values
are computed from other elements. OCL expressions are used to specify how this computation is
carried out. Derived elements avoid redundancy, but perhaps even more important, they allow you
to specify business rules in one place, rather than in multiple places in source code.
• Navigating associations on the class diagram. You can use OCL as a query language, to return
collections of objects in your ECO Space. You can then use these collections directly in .NET data-
aware controls to display and edit the values. For example, the following OCL statement Person.
allInstances.birthDate retrieves a collection of the values of the birthDate attribute for all
instances of the Person class. OCL's querying capabilities not only apply to class properties and
attributes, but to associations you have defined on the class diagram. For example, say you have
a Person class, and it has an association end called "Home", of type Building, and Building
has an attribute called Address. When you evaluate the OCL expression Home.Address in the
context of a specific Person object, you will get back the value of the Address attribute of the
instance of Home associated with that Person.
Execution of the model means that the designtime support for creating the model carries through to
runtime. A truly design driven software engineering process includes support not only for creation of a
model, but also for maintaining and enforcing the integrity of the model at all phases of the application's
lifetime. The common thread that runs through Delphi 8 for .NET's modeling toolset is the ECO
framework. It is helpful to understand how the ECO framework splits its functionality into designtime
support and runtime support.
52
• Undo/Redo mechanism
• OCL querying
• OCL evaluation
• Caching of objects
• Subscription mechanism
• Object versioning
• Transactions
• Binding to data-aware .NET UI controls
ECO Spaces
An ECO Space is a container of objects. At runtime, the ECO Space contains actual instances of the
classes in your model. It is helpful to think of the ECO Space as an actual instance of a model, much
like an object is an instance of a class. The objects contained in the ECO Space retain the domain
properties (attributes and operations) and relationships defined in the model.
As a container of objects, an ECO Space is both a cache, and a transactional context. Within the ECO
Space, another component called a persistence mapper is used as the conduit between the persistence
layer (either an RDBMS or XML file), and the instances of the classes defined in the model. When you
configure an ECO Space using the IDE, you will select those UML packages in your model that you wish
to persist in the ECO Space.
Borland.Eco.Services
The Borland.Eco.Services namespace is the primary interface to the runtime capabilities of the
ECO framework. The services provided are:
• IStateService. Provides routines to determine if a given object or property has changed since the
last database update.
• IPersistenceService. Provides routines for querying and saving, using the chosen persistence
mechanism of the ECO Space.
• IDirtyListService. Provides routines for returning lists of objects that have changed since the last
database update.
• IExtentService. Queries the ECO Space for all objects of a given class.
53
• IObjectFactoryService. Creates a new instance of a given class using the model's UML type system.
When creating new instances, you can use either the IObjectFactoryService interface, or you can
use the constructors defined on your classes.
• IOclService. Provides routines for evaluating OCL expressions and returning the results.
• IVersionService. Provides routines for creating and storing versions of objects in the ECO Space.
Versioned objects are tagged with version numbers starting with 1. Versioned objects support
multiple, read-only instances, and one "live" instance. The latest version, tagged with the special
version number CurrentVersion, is always the live, writeable instance.
• IUndoService. Provides routines for supporting Undo/Redo functionality on objects in the ECO
Space.
These services are available using methods on the subclass of EcoSpace that the IDE generates for
you when you start a new ECO application (or when you add a new EcoSpace subclass to an existing
application).
Borland.Eco.Handles
The Borland.Eco.Handles namespace contains the components used for defining and interacting
with ECO Spaces. The components in this namespace fall into three categories:
• The DefaultEcoSpace class
• The OclVariables class
• The ElementHandle abstract class
You cannot instantiate a DefaultEcoSpace object directly; instead the IDE will generate a subclass for
you when you use either the ECO Application wizard, or the ECO Space wizard.
The ElementHandle class is the abstract superclass of all model elements. ElementHandle objects
represent values. The handle might represent a single value, or a collection of values. Subclasses of
ElementHandle implement the IListSource interface, making them suitable for use as data sources in
the .NET databinding architecture.
The OclVariables class allows you to create variables for use in OCL expressions. These variables are
then bound to, and reflect the value of the actual element handles in the ECO Space.
Borland.Eco.ObjectRepresentation
There are two ways to access the objects in your application's ECO Space. The most direct way is to
simply access the class' properties and methods through the source code generated from your class
diagrams. The Borland.Eco.ObjectRepresentation namespace provides a second, more
generic way that does not involve direct use of the types defined in the generated source code.
Instead, access to the objects is accomplished through a set of interfaces that all ECO classes implement
indirectly (i.e. they are implemented for you by the class diagramming and code generation tools). As
the name ObjectRepresentation implies, these interfaces expose the model the way it is
represented internally within the ECO Space. One reason for accessing objects through the
54
ObjectRepresentation interfaces is if you are creating user interface controls that are designed to
work with objects in an arbitrary ECO Space.
The second, more common use of the ObjectRepresentation interfaces is when you work with ECO
services such as persistence, Undo/Redo, object versioning, and OCL evaluation; these services are
defined in the Borland.Eco.Services namespace, and are available through your application's ECO
Space object. The Services APIs take the ObjectRepresentation interfaces as parameters, and
return references to them, which you can then use to call methods on the interface, or to cast to a type
defined in your model. The following diagram shows the interfaces defined in the
ObjectRepresentation namespace.
The root interface, IElement, contains a property called ContentType that you can examine to determine
how to cast the interface reference. IElement represents all runtime elements, including the objects
themselves, their attributes, the associations between classes in your model, and primitive types such
as strings and collections. The key to linking your model as it exists on the class diagrams with its
representation within the ECO framework, is to think of a single class as a generic container of elements.
These elements might be primitive types, or objects, or they might be collections themselves, which in
turn contain more elements.
For example, suppose you had a Person class in your model. You can think of this class as a container
for a set of properties, such as personName, home, and ownedBuildings. The property personName is
a string (a primitive type), the property home is an object of another class (which has its own set of
properties), and ownedBuildings is a collection of objects. The following diagram shows how the
mapping is made from the source code declaration to the interfaces implemented by the object's
representation within the ECO Space.
55
Borland.Eco.Subscription
The Borland.Eco.Subscription namespace contains interfaces and classes related to the ECO
framework's subscription service. With subscriptions, you can tell the framework to send a notification
when a particular event occurs within the object space. For example, you can have the framework send
a notification when a certain object (or more likely, a collection of objects) within the ECO Space
changes. The interfaces and classes provided in this namespace are:
• ISubscriber. You must implement this interface on all classes that you want to act as subscribers.
• SubscriberAdapterBase. This is an abstract base class you can use where you don't want to
implement the ISubscriber interface directly.
Typically you will use the SubscriberAdapterBase class, overriding its DoReceive method, as shown in
the following example:
uses
Borland.Eco.Subscription,
Borland.Eco.Services;
type
TMySubscribingClass = class
private
type
TMySubscriberAdapter = class(SubscriberAdapterBase)
strict protected
procedure DoReceive(sender: System.Object; e: EventArgs); override;
end;
private
FmyAdapter: TMySubscribingClass.TMySubscriberAdapter;
FextentService: IExtentService;
procedure ExtentServiceChanged;
procedure RespondToEvent;
end;
56
procedure TMySubscribingClass.TMySubscriberAdapter.DoReceive(sender:
System.Object; e: EventArgs);
begin
// ActualSubscriber is a property of SubscriberAdapterBase.
(ActualSubscriber as TMySubscribingClass).RespondToEvent;
end;
procedure TMySubscribingClass.ExtentServiceChanged;
begin
// Drop old subscriptions if any...
if FmyAdapter <> nil then
FmyAdapter.Deactivate();
FmyAdapter := TMySubscriberAdapter.Create(self);
// Place a subscription
FextentService.SubscribeToObjectAdded(FmyAdapter, nil);
end;
procedure TMySubscribingClass.RespondToEvent;
begin
// Add code to handle the event
end;
Borland.Eco.Persistence
The Borland.Eco.Persistence namespace contains classes related to saving objects in an ECO
Space out to disk. The ECO framework supports persistence to either a relational database, or an XML
file. There are three components of primary interest in this namespace:
• PersistenceMapperBdp. This component is used for saving objects to a relational database using
the Borland Data Provider classes for database connectivity.
• PersistenceMapperSqlServer. This component uses the SqlConnection component for database
connectivity.
• PersistenceMapperXML. This component is used to store objects in an XML file.
These components are used by dropping them onto the ECO Space designer, and then connecting
them to the PersistenceMapper property of the application's ECO Space.
In addition, the Borland.Eco.Persistence namespace contains three interfaces that are used to
map a single class attribute to one or more database columns. These interfaces are:
• IAttributeMapping
• ISingleColumnAttributeMapping
• INonBooleanBooleanMapping
The namespaces nested within Borland.Eco.Persistence contain the classes that implement the
default attribute mappings for the supported databases.
57
Borland.Eco.UmlRt
The Borland.Eco.UmlRt namespace implements of a subset of the foundation package in the UML
metamodel version 1.4. The interfaces in this namespace are used to access the UML elements that
comprise the model's type system. Data pertaining to the types within the type system are accessed
through the interfaces in the UmlRt namespace. For example, this namespace contains the interfaces
IClassifier, IClass, and IAttribute; these interfaces can be used to access the type system's metadata.
The top-level interface is IEcoTypeSystem. There are two ways to access the type system:
• The TypeSystem property of the EcoSpace class.
• Through the GetEcoService method, available on classes that implement the IEcoServiceProvider
interface.
Once you have the IEcoTypeSystem, you can examine its properties, such as AllClasses, which returns
a collection of metadata on all of the UML classes defined in the model.
Code generated by these wizards will include all of the necessary ECO-related .NET attributes and
default interface implementations.
58
view of the logical relationships in your code. Because it gives you an unfiltered view by design, code
visualization will naturally expose some implementation details behind the ECO framework.
A notable example of this is the fact that UML packages are actually implemented as classes, as
opposed to .NET namespaces as one might expect. On a code visualization diagram, you will see UML
packages represented as classes within your project's namespace. On an ECO class diagram however,
you will see the true, logical representation of the UML package. When developing ECO applications,
a general rule to follow is to use code visualization diagrams to view the non-ECO elements of your
project.
In the Model View window, all of your ECO-enabled UML packages and classes will be grouped under
a top-level root package in the project tree. The default name of the root UML package is
CoreClassesPackage. The root UML package node (and all UML packages underneath it) is
distinguished from an ordinary .NET namespace node by its icon. The icon, , represents a .NET
namespace discovered by code visualization. The icon, , represents a UML package.
Similarly, ECO classes are distinguished from their code visualization counterparts by a different icon.
The icon, , represents a class discovered by code visualization. The icon, , represents an ECO-
enabled class.
The class diagram itself is another type of designer surface. You can add new UML elements to the
diagram, including associations and notes, using the Tool Palette. You can select UML elements on the
diagram and set their properties in the Object Inspector. As you work on the class diagram, Delphi 8
for .NET generates the ECO-enabled source code that implements the model.
Class diagrams are opened from the Model View window. Each UML package you create has its own
primary class diagram, and this diagram cannot be deleted (it can be renamed, however). The class
diagram nodes are grouped underneath their UML package in the Model View tree. The primary class
diagram for a UML package always shows the entire contents of the package; it displays all of the sub-
packages, classes, and relationships that exist within that package. When you add a new element to a
UML package it is automatically represented on the primary class diagram.
You can also create secondary class diagrams within a UML package, if you want to show a subset of
the classes within the package. Unlike with the primary class diagram, new elements you add to the
59
package are not automatically added to secondary diagrams. Secondary diagrams can be renamed and
deleted as needed.
Any UML elements you add to a primary or secondary class diagram will be contained within the UML
package that owns the diagram. To show elements in other UML packages, you must create a
shortcut to the element. You can do this through the context menu of the class diagram. Shortcuts are
displayed on the diagram with a small arrow icon in their lower left corner. Once a shortcut has been
created, you can add associations between it and the classes in the UML package that owns the
diagram.
60
Working with Unmanaged Code
Borland's Delphi 8 for .NET provides the capability to work with the .NET features that support
unmanaged code. If you have COM or ActiveX components that you want to use within the .NET
framework, you can use the .NET COM Interop capabilities from within Delphi 8 for .NET while building
your applications.
61
Using COM Interop in Managed Applications
COM Interop is a .NET service that allows seamless interoperation between managed and unmanaged
code. The COM Interop service is a two-way bridge: It allows you to leverage existing COM servers and
ActiveX Controls in new .NET applications, as well as to expose .NET components in legacy,
unmanaged applications.
The Delphi 8 for .NET IDE features tools that will help you integrate your legacy COM servers and
ActiveX Controls into managed applications. Within the IDE, you can add references to unmanaged
DLLs to your project, and then browse the types contained in them, just as you can with managed
assemblies. You can add ActiveX Controls to the Tool Palette, and then drop them on your forms as
you would with any .NET component.
Seamless interoperability is achieved through stand-in objects called Runtime Callable Wrappers
(RCW). The RCW is a layer of communication between your managed application, and the actual
unmanaged COM server.
62
COM Interop Terminology
The .NET Framework has a rich collection of terms and three-letter acronyms. This section will help you
understand the terminology you will encounter when reading other COM Interop literature.
Metadata
In the context of .NET and COM, metadata is a term used to mean type information. In COM, type
information can be stored in a variety of ways. For instance, a C++ header file is a language-specific
container for type information. A type library is also a container for type information, but being a binary
format, type libraries are language neutral. Unlike the COM development model where type libraries are
not required, language neutral metadata is mandatory for all .NET assemblies. Every assembly is self-
describing; its metadata contains complete type information, including private types and private class
members.
Custom Attributes
Developers often tag program entities (such as classes and their methods) with descriptive attributes
such as static, private, protected, and public. In the .NET Framework, you can tag any entity, including
classes, properties, methods, and even assemblies themselves, with an attribute of your own design
and meaning. Custom attributes are expressed in source code, and are processed by the compiler. At
the end of the build process, custom attributes are emitted into the output assembly just like all metadata.
Reflection
A unique characteristic of the .NET Framework is that type information is not lost during the compilation
process. Instead, all metadata, including custom attributes, is emitted by the compiler into the final output
assembly. Metadata is available at runtime, through .NET Reflection services. The .NET Framework
SDK provides a reflection tool called ildasm that allows the developer to open any .NET assembly, and
inspect the types declared therein. Such reflection tools often allow programmers to directly view the IL
code generated by the compiler. The Delphi 8 for .NET IDE contains its own integrated reflection tool,
in the form of the meta data explorer tool that appears when you open a .NET assembly.
The .NET programming model drastically simplifies deployment of applications and components. On
the .NET platform, non-shared components are deployed directly into the application's local installation
directory; no registration is required. Alternatively, a non-shared component can be deployed in a
63
directory specified in the application's configuration file. Again, registration is not required for this
deployment scenario.
Shared components are installed into a special location called the Global Assembly Cache (GAC). The
GAC is an evolution of the system registry (though it is a completely separate mechanism and is not
associated with the registry at all). The GAC exists in the file system in a folder called \Windows
\Assembly. The .NET Framework supports simultaneous, or "side-by-side" deployment of different
versions of the same component. When you view the Global Assembly Cache folder using Windows
Explorer, you are actually looking at the GAC through a special shell extension. The shell extension
presents all of the assemblies that have been installed into the GAC, with their version, culture, and
public key information.
There are three ways to install a .NET component into the GAC. The first way is to use the Framework
SDK command-line tool called gacutil, which is discussed below. Another way is to install a component
into the GAC is to navigate to the \Windows\Assembly folder using Windows Explorer, and then simply
drag and drop the assembly into the directory listing pane. Finally, you can also use the .NET
Configuration management tool, which is accessible through the Windows Control Panel.
Strong Names
The concept of a strong name is similar to that of the 128-bit Globally Unique Identifier (GUID) in COM
programming. A GUID is a name that is guaranteed to be globally unique. Every .NET assembly has a
basic name, which consists of a text string, a version number, and optional culture information. For
shared assemblies installed into the GAC, the basic name alone is not enough to guarantee the
assembly is uniquely identified. To generate a globally unique name, an encryption key with public and
private components is used to generate a digital signature. The signature is then applied to the assembly
using the .NET Framework SDK Assembly Linker (al.exe), or by using assembly attributes in source
code.
When you use a COM object in a managed application, the Common Language Runtime (CLR) creates
an RCW, which is the interface between managed and unmanaged code. The complexities of data
marshaling and reference counting are handled by the RCW. In fact the RCW does not even expose
the IUnknown and IDispatch interfaces.
When you use a .NET component in an unmanaged application, the system creates a stand-in called a
COM Callable Wrapper (CCW).
64
Primary Interop Assembly
In the COM programming model, once a GUID is assigned to a type, the GUID always refers to that
specific type no matter where the type appears. For example, a common interface might be defined in
many different type libraries, but each separate type library would have to define the interface with the
same GUID, so the duplication is not a problem. However, if you generate COM Interop assemblies for
these separate type libraries, a new and distinct assembly would be created for each type library. Each
of these separate assemblies would contain distinct types (as far as the CLR is concerned). The strong
identity and self-describing nature of .NET assemblies is actually working against you in this case. Here,
it is leading to a GAC that is cluttered with interop assemblies that all contain RCWs for the same type
library. Worse yet, to the CLR each assembly contains distinct and incompatible types, because each
one has a different strong name.
To avoid this proliferation of assemblies and potential type incompatibilities, the framework gives you
the ability to designate one assembly as the primary interop assembly for a type library. A primary interop
assembly is always signed with a strong name, by the original publisher of the type library.
The .NET Framework SDK contains another command-line tool called tlbexp that is used to create a
type library from a .NET assembly. Such an exported type library would then be used to expose
the .NET component as a COM server, for use within an unmanaged application.
The aximp tool will generate both interop assemblies (as with tlbimp, this includes dependent
assemblies), and the ActiveX wrapper assembly. Like tlbimp, aximp has command-line switches to sign
65
the assemblies produced with a strong name. Unlike tlbimp, aximp cannot generate a primary interop
assembly.
When using delay signing, the assembly is signed with the public portion of the key file at build time.
Before shipping the assembly, the sn tool is used again to sign the assembly with the private key.
The IDE creates one interop assembly for each imported type library or DLL. The assemblies are named
Interop.LibraryName.dll, where LibraryName is the name of the type library. The name of the library is
specified in the library statement in IDL source code, so the file name of the generated assembly might
be different from that of the original DLL or type library. Each interop assembly (and all of its dependent
assemblies) are added to your project as referenced assemblies. The types contained in the interop
66
assembly are added to a namespace with the same name as the type library; Again, this is derived from
the library statement in IDL source code.
If the assembly you reference has a primary interop assembly, the IDE will recognize this and avoid
generating a new interop assembly. In this case, the IDE will add a reference to the primary interop
assembly in the GAC, and it will not copy the assembly to your local project directory.
Once on your form, the ActiveX Control can be treated as any other .NET component. You can select
the control, and set its properties and event handlers in the Object Inspector. The ActiveX Control
wrapper will expose the properties of the Windows.Forms.Control class, while properties exposed by
the ActiveX Control will be grouped under the Misc category.
The list of referenced assemblies (including those that are not interop assemblies) is an attribute of your
project. If the COMImports folder (or one of the interop assemblies contained therein) does not exist
when you open a project, the IDE will attempt to recreate it. If the IDE cannot create an interop assembly,
it will still be shown as a referenced assembly in the Project Manager; the IDE will highlight such an
assembly so that you know it currently does not exist (or is not registered) on the machine.
67
registering the COM server on the end-user's machine. Note the registration requirement also applies
during the development of your managed application.
As with any other .NET assembly, an interop assembly can be deployed alongside the managed
executable in the installation folder, or it can be deployed in the GAC. If you deploy the interop assembly
into the GAC, you must give it a strong name during development. Primary interop assemblies are
always deployed into the GAC; however, just because an assembly is deployed to the GAC, does not
automatically make it a primary interop assembly. An interop assembly is designated as a primary
interop assembly by using the /primary command-line option of the tlbimp utility. The IDE currently has
no built-in support for creating primary interop assemblies. Unmanaged COM servers can be deployed
anywhere on the end-user's machine, however, as noted previously, you must still register unmanaged
components when your application is installed.
68
Using Platform Invoke with Delphi 8 for .NET
This topic describes the basic techniques of using unmanaged APIs from Delphi 8 for .NET. Some of
the common mistakes and pitfalls are pointed out, and a quick reference for translating Delphi data types
is provided. This topic does not attempt to explain the basics of platform invoke or marshaling data.
Please refer to the links at the end of this topic for more information on platform invoke and marshaling.
Understanding attributes and how they are used is also highly recommended before reading this
document.
The Win32 API is used for several examples. For further details on the API functions mentioned, please
see the Windows Platform SDK documentation.
BOOL SystemParametersInfo(
UINT uiAction, // system parameter to retrieve or set
UINT uiParam, // depends on action to be taken
PVOID pvParam, // depends on action to be taken
UINT fWinIni // user profile update option
);
Depending on the value of uiAction, pvParam can be one of dozens of different structures or simple
data types. Since there is no way to represent this with one single managed declaration, multiple
overloaded versions of the function must be declared (see Borland.Vcl.Windows.pas), where each
overload covers one specific case. The parameter pvParam can also be given the generic declaration
IntPtr. This places the burden of marshaling on the caller, rather than the built in marshaler. Note that
the data types used in a managed declaration of an unmanaged function must be types that the default
marshaler supports. Otherwise, the caller must declare the parameter as IntPtr and be responsible for
marshaling the data.
69
Data Types
Most data types do not need to be changed, except for pointer and string types. The following table
shows commonly used data types, and how to translate them for managed code:
IntPtr can also represent all pointer and string types, in which case you need to manually marshal data
using the Marshal class. When working with functions that receive a text buffer, the StringBuilder class
provides the easiest solution. The following example shows how to use a StringBuilder to receive a text
buffer:
70
end;
end;
An unmanaged buffer is allocated, and the string copied into it by calling StringToHGlobalAuto. The
buffer must be freed once it’s no longer needed. To marshal a pointer to a structure, use the Marshal.
StructureToPtr method to copy the contents of the structure into the unmanaged memory buffer.
The following example shows how to receive a text buffer and marshal the data into a string:
Advanced Techniques
When working with unmanaged API’s, it is common to pass parameters as either a pointer to something,
or NULL. Since the managed API translations don’t use pointer types, it might be necessary to create
an additional overloaded version of the function with the parameter that can be NULL declared as IntPtr.
Special Cases
There are cases where a StringBuilder and even the Marshal class will be unable to correctly handle
the data that needs to be passed to an unmanaged function. An example of such a case is when the
string you need to pass, or receive, contains multiple strings separated by NULL characters. Since the
default marshaler will consider the first NULL to be the end of the string, the data will be truncated (this
also applies to the StringToHGlobalXXX and PtrToStringXXX methods). In this situation
TBytes can be used (using the PlatformStringOf and PlatformBytesOf functions in Borland.Delphi.
System to convert the byte array to/from a string). Note that these utility functions do not add or remove
terminating NULL characters.
When working with COM interfaces, the UnmanagedType enumeration (used by the MarshalAsAttribute
class) has a special value, LPStruct. This is only valid in combination with a System.Guid class,
causing the marshaler to convert the parameter into a Win32 GUID structure. The function
CoCreateInstance that is declared in Delphi 7 as:
71
function CoCreateInstance([MarshalAs(UnmanagedType.LPStruct)] clsid:
TCLSID;
[MarshalAs(UnmanagedType.IUnknown)] unkOuter: TObject;
dwClsContext: Longint;
[MarshalAs(UnmanagedType.LPStruct)] iid: TIID;
[MarshalAs(UnmanagedType.Interface)] out pv
): HResult;
This is currently the only documented use for UnmanagedType.LPStruct.
Structures
The biggest difference between calling unmanaged functions and passing structures to unmanaged
functions is that the default marshaler has some major restrictions when working with structures. The
most important are that dynamic arrays, arrays of structures and the StringBuilder class cannot be used
in structures. For these cases IntPtr is required (although in some cases string paired with various
marshaling attributes can be used for strings).
Data Types
The following table shows commonly used data types, and how to “translate” them for managed code:
When working with arrays and strings in structures, the MarshalAs attribute is used to describe additional
information to the default marshaler about the data type. A record declared in Delphi 7, for example:
type
TMyRecord = record
72
IntBuffer: array[0..31] of Integer;
CharBuffer: array[0..127] of Char;
lpszInput: LPTSTR;
lpszOutput: LPTSTR;
end;
Would be declared as follows in Delphi 8 for .NET:
type
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
TMyRecord = record
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
IntBuffer: array[0..31] of Integer;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
CharBuffer: string;
[MarshalAs(UnmanagedType.LPTStr)]
lpszInput: string;
lpszOutput: IntPtr;
end;
The above declarations assume that the strings contain platform dependant TChar’s (as commonly used
by the Win32 API). It is important to note that in order to receive text in lpszOutput, the Marshal.
AllocHGlobal method needs to be called before passing the structure to an API function.
A structure can contain structures, but not pointers to structures. For such cases an IntPtr must be
declared, and the Marshal. StructureToPtr method used to move data from the managed structure into
unmanaged memory. Note that StructureToPtr does not allocate the memory needed (this must be done
separately). Be sure to use Marshal. SizeOf to determine the amount of memory required, as Delphi’s
SizeOf is not aware of the MarshalAs attribute (in the example above, CharBuffer would be 4 bytes
using Delphi’s SizeOf when it in fact should occupies 128 bytes on a single byte system). The following
examples show how to send messages that pass pointers to a structure:
73
Buffer: IntPtr;
begin
Buffer := Marshal.AllocHGlobal(Marshal.SizeOf(TypeOf(TRect)));
try
SendMessage(Handle, EM_GETRECT, 0, Buffer);
Rect := TRect(Marshal.PtrToStructure(Buffer, TypeOf(TRect)));
finally
Marshal.DestroyStructure(Buffer, TypeOf(TRect));
end;
end;
It is important to call DestroyStructure rather than FreeHGlobal if the structure contains fields where the
marshaling layer needs to free additional buffers (see the documentation for DestroyStructure for more
details).
Advanced topics
Working with unmanaged API’s it is not uncommon to need to convert a byte array into a structure (or
retrieve one or more fields from a structure held in a byte array), or vice versa. Although the Marshal
class contains a method to retrieve the offset of a given field, it is extremely slow and should be avoided
in most situations. Informal performance tests show that for a structure with eight or nine numeric fields,
it is much faster to allocate a block of unmanaged memory, copy the byte array to the unmanaged
memory and call PtrToStructure than finding the position of just one field using Marshal. OffsetOf and
converting the data using the BitConverter class. Borland.Vcl.WinUtils contains helper functions
to perform conversions between byte arrays and structures (see StructureToBytes and
BytesToStructure).
Special cases
There are cases where custom processing is required, such as sending a message with a pointer to an
array of integers. For situations like this, the Marshal class provides methods to copy data directly to
the unmanaged buffer, at specified offsets (so you can construct an array of a custom data type after
allocating a buffer). The following example shows how to send a message where the LParam is a pointer
to an array of Integer:
74
end;
end;
Callback Functions
When passing a function pointer for a managed function to an unmanaged API, a reference must be
maintained to the delegate or it will be garbage collected. If you pass a pointer to your managed function
directly, a temporary delegate will be created, and as soon as it goes out of scope (at the end of
MyFunction in the example below), it is subject to garbage collection. Consider the following Delphi 7
code:
const
MyCallbackDelegate: TFNMyCallback = @MyCallback;
Data types
The same rules apply for callbacks as any other unmanaged API function.
Special cases
Any parameters used in an asynchronous process must be declared as IntPtr. The marshaler will free
any memory it has allocated for unmanaged types when it returns from the function call. When using an
IntPtr, it is your responsibility to free any memory that has been allocated.
75
a given event. This can still be accomplished in .NET, but special care needs to be taken to ensure a
reference is kept to all objects (otherwise they can and will be garbage collected).
Data types
The following table shows
The GCHandle provides the primary means of passing an object references to unmanaged code, and
ensuring garbage collection does not happen. A GCHandle needs to be allocated, and later freed when
no longer needed. There are several types of GCHandle, GCHandleType.Normal being the most useful
when an unmanaged client holds the only reference. In order pass a GCHandle to an API function once
it is allocated, type cast it to IntPtr (and optionally onwards to LongInt, depending on the unmanaged
declaration). The IntPtr can later be cast back to a GCHandle. Note that IsAllocated must be called
before accessing the Target property, as shown below:
procedure MyProcedure;
var
Ptr: IntPtr;
Handle: GCHandle;
begin
...
if Ptr <> nil then
begin
Handle := GCHandle(Ptr);
if Handle.IsAllocated then
DoSomething(Handle.Target);
end;
...
end;
Advanced techniques
The use of a GCHandle, although relatively easy, is fairly expensive in terms of performance. It also has
the possibility of resource leaks if handles aren’t freed correctly. If object references are maintained in
the managed code, it is possible to pass a unique index, for example the hash code returned by the
GetHashCode method, to the unmanaged API instead of an object reference. A hash table can be
maintained on the managed side to facilitate retrieving an object instance from a hash value if needed.
76
An example of using this technique can be found in the TTreeNodes class (in Borland.
Vcl.ComCtrls).
IAutoComplete = interface(IUnknown)
['{00bb2762-6a77-11d0-a535-00c04fd7d062}']
function Init(hwndEdit: HWND; punkACL: IUnknown;
pwszRegKeyPath: LPCWSTR; pwszQuickComplete: LPCWSTR): HRESULT; stdcall;
function Enable(fEnable: BOOL): HRESULT; stdcall;
end;
In Delphi 8 for .NET it is declared as follows:
[ComImport, GuidAttribute('00BB2762-6A77-11D0-A535-00C04FD7D062'),
InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
IAutoComplete = interface
function Init(hwndEdit: HWND; punkACL: IEnumString;
pwszRegKeyPath: IntPtr; pwszQuickComplete: IntPtr): HRESULT;
function Enable(fEnable: BOOL): HRESULT;
end;
Note the custom attributes used to describe the GUID and type of interface. It is also essential to use
the ComImportAttribute class. There are some important notes when importing COM interfaces. You do
not need to implement the IUnknown/IDispatch methods, and inheritance is not supported.
Data types
The same rules as unmanaged functions apply for most data types, with the following additions:
77
Variant TObject TObject
SafeArray (of type) array of <type> array of <type>
BSTR String String
Using the MarshalAsAttribute custom attribute is required for some of the above uses of TObject,
specifying the exact unmanaged type (such as UnmanagedType.IUnknown,
UnmanagedType.IDispatch or UnmanagedType.Interface). This is also true for certain array types. An
example of explicitly specifying the unmanaged type is the Next method of the IEnumString interface.
The Win32 API declares Next as follows:
HRESULT Next(
ULONG celt,
LPOLESTR * rgelt,
ULONG * pceltFetched
);
In Delphi 8 for .NET the declaration would be:
Advanced techniques
When working with safearrays, the marshal layer automatically converts (for example) an array of bytes
into the corresponding safearray type. The marshal layer is very sensitive to type mismatches when
converting safearrays. If the type of the safearray does not exactly match the type of the managed array,
an exception is thrown. Some of the Win32 safearray API’s do not set the type of the safearray correctly
when the array is created, which will lead to a type mismatch in the marshal layer when used from .NET.
The solutions are to either ensure that the safearray is created correctly, or to bypass the marshal layer’s
automatic conversion. The latter choice may be risky (but could be the only alternative if you don’t have
the ability to change the COM server that is providing the data). Consider the following declaration:
78
type
TSafeByteArrayData = packed record
VType: Word;
Reserved1: Word;
Reserved2: Word;
Reserved3: Word;
VArray: IntPtr; { This is a pointer to the actual SafeArray }
end;
Special cases
Although it is preferred to use Activator.CreateInstance when creating an instance, it is not fully
compatible with CoCreateInstanceEx. When working with remote servers, CreateInstance will
always try to invoke the server locally, before attempting to invoke the server on the remote machine.
Currently the only known work-around is to use CoCreateInstanceEx.
Since inheritance isn’t supported, a descendant interface needs to declare the ancestor’s methods.
Below is the IAutoComplete2 interface, which extends IAutoComplete.
[ComImport, GuidAttribute('EAC04BC0-3791-11d2-BB95-0060977B464C'),
InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
IAutoComplete2 = interface(IAutoComplete)
// IAutoComplete methods
function Init(hwndEdit: HWND; punkACL: IEnumString;
pwszRegKeyPath: IntPtr; pwszQuickComplete: IntPtr): HRESULT;
function Enable(fEnable: BOOL): HRESULT;
//
function SetOptions(dwFlag: DWORD): HRESULT;
function GetOptions(var dwFlag: DWORD): HRESULT;
end;
79
Building Web Applications with ASP.NET
ASP.NET is the programming model for building Web applications using the .NET Framework. This
section provides the conceptual background for building ASP.NET applications using Delphi 8 for .NET.
In addition to supporting data access components within the .NET Framework, Delphi 8 for .NET
includes DB Web Controls. DB Web Controls work with .NET Framework providers and Borland Data
Providers for .NET (BDP.NET) to accelerate Web application development.
80
ASP.NET Overview
ASP.NET is the .NET programming environment for building Internet applications with an HTML front
end. The ASP.NET architecture is designed for seamless integration with a number of .NET
programming models, including its own Web Forms for the front-end interface, Web Services as an
optional approach to xml-based messaging, and ADO.NET for optional, back-end data access. Use
ASP.NET to build forms-based applications in HTML that run on the Web.
Borland provides tools to simplify rapid ASP.NET development. If you are familiar with rapid application
development (RAD) and object oriented programming (OOP) using properties, methods, and events,
you will find the ASP.NET model for building rich Web applications familiar.
The following topic provides the conceptual background 1) to understand the major components of the
ASP.NET architecture and 2) to understand how ASP.NET integrates with other programming models
in the .NET framework.
This section introduces:
• ASP.NET architecture.
• Web Forms components.
• Web Forms data access.
• Web Services.
• ASP.NET namespace.
• ASP.NET application deployment.
ASP.NET Architecture
81
The major components of the ASP.NET architecture are the Web Forms page, ASP.NET Server
Controls, code-behind logic, and compiled DLL files. Web Form pages contain HTML elements, text,
and server controls. Code-behind files contain application logic for the Forms page. Compiled DLL files
render dynamic HTML on the web server.
Code-behind File
The code-behind file contains the application logic that interacts with the visual components and
ASP.NET server controls in your Web Forms page.
Web Services
Web Services provide application components to any number of distributed systems using XML-based
messaging. A Web Service can be as simple as an XML message updating values in a remote
82
application (perhaps a stock quote in a web client) to an integral part of a sophisticated ASP.NET or
ADO.NET application. Web Services and ASP.NET share common .NET infrastructure that allows for
seamless integration.
ASP.NET Namespace
See the Microsoft .NET Framework SDK for information pertaining to the System.Web namespaces and
ASP.NET.
When deploying a Delphi 8 for .NET ASP.NET application, you need to deploy the Delphi 8 for .NET
compiler as well, with the application. This allows the Delphi 8 for .NET scripts in your ASP.NET
application to be compiled as needed. For more information, refer to the DEPLOY document included
in the Delphi 8 for .NET installation.
83
Borland DB Web Controls
Borland DB Web Controls simplify database development tasks in conjunction with BDP.NET and .NET
Framework data access components. DB Web Controls are data-aware controls that provide advanced
functionality, including data-aware grid, navigator, calendar, combobox, and other popular components.
84
8. In several places where you would otherwise need to write code with DataGrid (for example, paging
using numbers, paging using the previous and next icons, Edit, and Delete columns), no code is
required using DBWebDataGrid.
DB Web ControlsNamespace
The namespace for DB Web Controls is Borland.Data.Web.
85
Building Web Services with ASP.NET
Web Services is a programmable entity that provides a particular element of functionality, such as
application logic, and is accessible to any number of potentially disparate systems through the use of
Internet standards, such as XML and HTTP. Applications built with ASP.NET Web Services can be
either stand-alone applications or sub-components of a larger web application and can provide
application components to any number of distributed systems using XML-based messaging. Delphi 8
for .NET provides a number of methods that can help you build, deploy, and use applications with ASP.
NET Web Services. For more general information about Web Services, refer to Microsoft's .NET SDK
Documentation.
86
ASP.NET Web Services Overview
Web Services is an Internet-based integration methodology that enables applications, independent of
any platform or language, to connect and exchange information. Web Services are tightly integrated
with the .NET Framework's ASP.NET model. Unlike traditional native Windows applications, ASP.NET
Web Services applications contain objects and methods that are exposed over the Web using simple
messaging protocol stacks. Any client can invoke a Web Services application over HTTP using a
WebMethod. Like any method that can be accessed by way of a simple Windows Form application, a
WebMethod provides some defined functionality, but unlike other types of methods, the WebMethod is
accessed by way of a web browser. For more general information about Web Services, refer to
Microsoft's .NET Framework SDK Documentation.
Borland provides tools to develop and access ASP.NET Web Services using a variety of techniques.
As modular objects, Web Services provide reuse without additional coding.
The following topics provide a brief introduction to the architecture of ASP.NET Web Services, the basic
fundamentals of Web Services communication, and to the files created when you develop ASP.NET
Web Services.
87
The major components of the ASP.NET Web Services architecture include a client application, an ASP.
NET Web Services application, and several files such as code files in the development language, .asmx
files, and compiled .dll files. You need a web server to house both ASP.NET Web Services application
and the client. Optionally, you might include a database server for storage and access of ASP.NET Web
Services data.
You can use Web Services for solutions in the following areas:
88
• Enterprise Application Integration (EAI). A web service could allow multiple business partners
to exchange inventory, order, or financial data, for example, without specifically knowing the precise
data layout in which each partner's data is stored. For instance, many CRM or other front-end
applications store customer data in a format that is not entirely compatible with the way a back-end
ERP system stores its financial or inventory information. Yet, a sales organization may wish to use
its CRM solution to process real-time orders with up-to-date inventory information from the ERP
system. A web service could be a solution to managing the transformation of CRM requests to ERP
storage and from ERP responses to CRM confirmations.
• Business-to-business (B2B) integration. Similar to the EAI solution, a B2B solution could take
advantage of a web service capability to provide cached data for large orders. B2B transactions,
unlike business-to-consumer (B2C) transactions, often consist of high-volume transactions that
would be prohibitive to execute at the granular level of a B2C transaction. For instance, a consumer
might order one box of pencils from an online stationery store, but a business might order a thousand
boxes monthly, with multiple shipping addresses. The scale and complexity of a B2B transaction
requires the intervention of a web service to help simplify and process the transaction quickly and
with consistency.
• Business-to-consumer (B2C) integration. B2C web services typically manage web-based
transactions. For example, a web service that allows you to look up postal codes eliminates the
need for any given individual to create a new program to perform this task every time they want to
include the service on a web site. Some commerce sites might use web services to help manage
currency conversion when taking international sales orders.
• Mobile (Smart client applications). Because the small footprint of a mobile client requires that
memory usage be reserved for only the most important system functions, and because mobile
clients are, by definition, linked to the Internet by way of their wireless communication protocols,
web services play a vital role in providing lightweight but powerful applications to mobile devices.
Web services allow mobile device users to perform a variety of tasks which require little more than
data input at the device and data display of the results. All processing can occur on a remote web
service, thus decreasing bandwidth requirements on the mobile device itself.
• Distributed/Peer-to-Peer. For certain types of distributed and peer-to-peer applications, web
services play an important role. If using distributed computing over an uncontrolled network (such
as the Internet) rather than over a LAN or corporate network, you might use web services, which
do not require state maintenance, thus offering potentially improved performance, particularly
where a request-response behavior is not unequivocally required. For applications that require strict
request-response behavior and high security, you should consider using an older, more controlled
model, such as COM, CORBA, or .NET remoting.
89
Name of files Description
.asmx When you create an ASP.NET Web Services application, a text file is automatically generated
with the .asmx extension. Additionally, the required Web Services directive is placed at the top
of this file to correlate between the URL address of the Web Services and its implementation.
Within the .asmx file, you add Web Services logic to the methods visible by the client application.
The .asmx file acts as the base URL for clients calling the XML Web Service. This file is compiled
into an assembly, along with other files, for deployment.
code-behind When you create an ASP.NET Web Service application, a code-behind file is generated with a
language-specific extension. You add your Web Services logic to the public method to process
Web Services requests and responses.
compiled DLL filesWeb Services DLL files provide dynamic services on the web server.
.wsdl This file is generated when you click the Add Web Reference feature to add the Web Services
to your client application. It describes the Web Services interface available to the client.
.map This file enables the discovery of a Web Service that is exposed on a given server. It also contains
links to other resources that describe the Web Service.
90
Web Services Protocol Stack
Understanding the Web Services infrastructure requires that you have some exposure to XML, SOAP,
WSDL, and UDDI. As a developer of XML Web Services, you are not creating anything from the ground
up because the infrastructure already exists. Instead, you leverage the existing technology by using
standard Web protocols such as XML and HTTP.
Borland provides an easy way to create, deploy, and use Web Services without the worries of back-end
processing so you can focus more on designing your services.
The following topics provide the conceptual background to understand how the protocol stack
contributes to Web Services functionality.
• How Web Services access and expose their services via the Web.
• How XML passes information through standard SOAP and HTTP.
• How a client can identify a Web Service offering.
• How Web Services are discovered and accessed.
91
Transport Layer
The Transport layer is the first component in the stack and is responsible for moving XML messages
between applications. The Transport protocol most commonly used is the standard HTTP protocol.
Other commonly used Web protocols are SMTP and FTP.
XML Messaging
The messaging layer in the protocol stack is based on an XML model. XML (Extensible Markup
Language) is widely used in Web Services applications and is the foundation for all Web Services. XML
is just one of the standards enabling Web Services to map between technology domains. You will find
many resources on the Web that describe XML messaging. For more information, refer to the World
Wide Web Consortium site on Messaging listed in the link list below.
The XML Messaging specification is a broadly-defined umbrella under which a number of more specific
protocols are defined. One of the more popular standards is known as the Simple Object Access Protocol
(SOAP) and is one of the most significant standards in communicating Web Services over the network.
Just as XML provides a means for communicating over the Web using an XML document that both
requests and responds to information between two disparate systems, SOAP allows the sender and the
receiver of XML documents to support a common data transfer protocol for effective networked
communication. You will find many resources on the Web that describe SOAP. For more information,
refer to the World Wide Web Consortium site for SOAP. See the link list below.
WSDL Layer
This layer represents a way of specifying a public interface for a Web Service. It contains information
on available functions, on data types for XML messaging, binding information about the transport
protocol, and the location of the specific Web Service.
Any client application that wants to know about a service, what data it expects to receive, whether or
not it delivers any results, and the supported transport, uses the Web Services Description Language
(WSDL) to find that information. When you create a Web Service, it must be described and advertised
to its potential customers before it can be used. WSDL provides a common format for describing and
publishing that Web service information. Typically, WSDL is used with SOAP, and the WSDL
specification includes a SOAP binding.
Use Borland's Add Web Reference feature to obtain a WSDL document for your Web Service. The
WSDL document, or proxy file, is copied to the client and is used to call the server. This proxy file is
called References.* and has an extension that reflects its particular language type. For more information
about WSDL, refer to the World Wide Web Consortium site.
92
Service Discovery
Service Discovery is a layer that represents a way to publish and find Web Services over the Web. You
can think of this layer as the White and Yellow Pages of your phonebook. The White pages of Web
Services provides general information about a specific company, for instance, their business name,
description, and address. The Yellow Pages includes the classification of data for the services offered,
for instance, industry type and products.
The protocol you use to publish your Web Services is known as Universal Description, Discovery, and
Integration (UDDI).
With Delphi 8 for .NET, your data automatically gets published to the registry, or a distributed directory
for business and Web Services. The UDDI Business Registry allows anyone to search existing UDDI
data and enables you to register your company and its services.
93
ASP.NET Web Services Support
ASP.NET Web Services support VCL.NET Forms, .NET Windows Forms, and ASP.NET Web Forms,
which can be used to create client applications that access Web Services applications. An Add Web
Reference feature is provided to add the desired ASP.NET Web Services application to the client
application. Also available by way of the Add Web Reference feature, a UDDI Browser helps you locate
Web Services applications you might want to use.
Delphi 8 for .NET provides simple tools to develop and deploy your ASP.NET Web Services applications.
Additionally, Delphi 8 for .NET helps you import WSDL documents that describe particular Web Services
applications and expose their functionality to the client application.
You can use the sample WebMethod provided by Delphi 8 for .NET, which lets you create and access
an ASP.NET Web Services application.
This section includes:
• ASP.NET Web Services Client Support.
• ASP.NET Web Services Server Support.
• ASP.NET Web Services Namespaces.
If you need to provide a rich application that can process complex content on a client workstation, or
that can use a Web Service application as a supporting piece for a rich client application over a secure
network connection, you might consider building a Windows Forms application. If you need to provide
a thin-client application that performs simple data manipulation or satisfies a single-purpose
requirement, consider using ASP.NET Web Forms. Web Forms are platform-independent interfaces
that display in a web browser and invoke Web Services applications over a simple protocol like HTTP.
94
You can also create an ASP.NET Web Services application as a console application which can be
accessed through either a console window, or by another Web Service application, even one without a
client.
Delphi 8 for .NET provides a dialog that lets you specify the name and location of the ASP.NET Web
Services application, and automatically creates the mandatory files that are necessary for deployment.
When you specify the application settings, Delphi 8 for .NET generates the .asmx file that acts as a base
URL for clients calling the ASP.NET Web Services application.
95
Building Windows Applications with Windows Forms
Windows Forms provide a traditional approach to developing user interfaces, client/server applications,
forms, controls, and application logic. Windows Forms fully leverages the .NET Framework. This section
provides an overview of Windows Forms using Delphi 8 for .NET and common steps to building a simple
Windows project.
96
Windows Forms Overview
Windows Forms is the .NET programming environment for building native Windows applications in a
managed environment. Building Windows clients with .NET allows applications to use features
unavailable to browser clients while leveraging the .NET Framework for general infrastructure. Windows
Forms combines features of both traditional and Internet-centric development, presenting a
programming model that takes advantage of a unified .NET Framework (for instance, for security and
dynamic application updates) and the richness of GUI Windows clients.
Windows Forms share common .NET Framework with other programming models, like ASP.NET and
ADO.NET.
Windows Forms
Delphi 8 for .NET provides an IDE for creating a GUI in a RAD environment. Developers drag and drop
controls, dialogs, and components onto the form designer, set properties in Object Inspector, and code
the logic to respond to events.
97
The Windows Form object in Delphi 8 for .NET is called TWinForm and is a descendant of TForm. This
differs from the VCL.NET-based form that you can build using the VCL.NET designer. In effect, the
TWinForm object is a .NET Windows Form that uses Object Pascal as its code-behind.
98
Building Database Applications with ADO.NET
ADO.NET presents a coherent programming model for exposing data access within the .NET
Framework. In addition to supporting MS SQL, Oracle, and OLE DB connection components within the .
NET Framework, Delphi 8 for .NET includes Borland Data Providers for .NET (BDP.NET). BDP.NET
supports access to MS SQL, Oracle, DB2, and Interbase. BDP.NET component designers ease the
generation and configuration of BDP.NET components. This section overviews how to use Delphi 8 for .
NET with the ADO.NET architecture and how to build a simple ADO.NET project.
99
ADO.NET Overview
ADO.NET is the .NET programming environment for building database applications based on native
database formats or XML data. ADO.NET is designed as a back-end data store for all .NET programming
models, including Web Forms, Web Services, and Windows Forms. Use ADO.NET to manage data in
the .NET Framework.
Borland provides tools to simplify rapid ADO.NET development using Borland Data Providers for .NET
(BDP.NET). If you are familiar with rapid application development (RAD) and object oriented
programming (OOP) using properties, methods, and events, you will find the ADO.NET model for
building applications familiar. If you are a traditional database programmer, ADO.NET provides familiar
concepts, such as tables, rows, and columns with relational navigation. XML developers will appreciate
navigating the same data with nodes, parents, siblings, and children.
The following topic provides the conceptual background to understand 1) the major components of the
ADO.NET architecture, 2) how ADO.NET integrates with other programming models in the .NET
Framework, and 3) key Delphi 8 for .NET functionality.
ADO.NET Architecture
100
The two major components of the ADO.NET architecture are the Data Provider and the DataSet. The
data source represents the physical database or XML file, the Data Provider makes connections and
passes commands, and the DataSet represents one or more data sources in memory. For more
information about the general ADO.NET model, see the .NET Framework Developer's Guide.
Data Source
The data source is the physical database, either local or remote, or an XML file. In traditional database
programming, the developer typically works with the data source directly, often requiring complex,
proprietary interfaces. With ADO.NET, the database developer works with a set of components to
access the data source, to expose data, and to pass commands.
Data Providers
Data Provider components connect to the physical databases or XML files, hiding implementation
details. Providers can connect to one or more data sources, pass commands, and expose data to the
DataSet.
The .NET Framework includes providers for MS SQL, OLE DB, and Oracle. In addition to supporting
the .NET providers, this product includes Borland Data Providers for .NET. BDP.NET connects to a
number of industry standard databases, providing a consistent programming environment. For more
information, see the Borland Data Providers for .NET topic.
Delphi 8 for .NET includes support for the .NET data providers by way of the TADONETConnector object.
This object provides access to .NET DataSets either directly or through the Borland Data Providers for .
NET. BDP.NET connects to a number of industry standard databases, providing a consistent
programming environment. For more information, see the Borland Data Providers for .NET topic and
the reference information for TADONETConnector.
TADONETConnector is the base class for Delphi 8 for .NET datasets that access their data using ADO.
NET. TADONETConnector descendants include TCustomADONETConnector. TADONETConnector is
a descendent of TDataSet.
DataSet
The DataSet object represents in-memory tables and relations from one or more data sources. The
DataSet provides a temporary work area or virtual scratch pad for manipulating data. ADO.NET
applications manipulate tables in memory, not within the physical database. The DataSet provides
additional flexibility over direct connections to physical databases. Much like a typical cursor object
supported by many database systems, the DataSet can contain multiple DataTables, which are
representations of tables or views from any number of data sources. The DataSet works in an
asynchronous, non-connected mode, passing update commands by way of the Provider to the data
source at a later time.
Delphi 8 for .NET provides two kinds of DataSets for your use: standard DataSets and typed DataSets.
A standard DataSet is the default DataSet that you get when you define a DataSet object implicitly. This
101
type of DataSet is constructed based on the layout of the columns in your data source, as they are
returned at runtime based on your Select statement.
Typed DataSets provide you more control over the layout of the data you retrieve from a data source.
A typed DataSet derives from a DataSet class. The typed DataSet lets you access tables and columns
by name rather than by way of collection methods. The typed DataSet feature provides better readability,
improved code completion capabilities, and data type enforcement unavailable with standard DataSets.
The compiler checks for type mismatches of typed DataSet elements at compile-time rather than
runtime. When you create a typed dataset, you will see that some new objects are created for you and
are accessible by way of the Project Manager. You will notice two files named after your dataset. One
file is an XML .xsd file and the other is a code file, in the language you are using. All of the data about
your dataset, including the table and column data from the database connection, is stored in the .xsd
file. The program code file is created based on the XML in the .xsd file. If you want to change the structure
of the typed dataset, you can change items in the .xsd file. When you recompile, the program code file
is regenerated based on the modified XML.
For more information about DataSets, see the Microsoft .NET Framework SDK.
Web Forms
Web Forms in ASP.NET provide a convenient interface for accessing databases over the web.
ASP.NET uses ADO.NET to handle data access functions.
.NET and BDP.NET connection components ease integration between Web Forms and ADO.NET. DB
Web Controls support both ADO.NET and BDP.NET components, accelerating web application
development.
Windows Forms
As an alternative to Web Forms, traditional, native-OS clients can function as a front end to ADO.NET
databases.
In Delphi 8 for .NET you can provide two types of Windows Forms: a TWinForm object, which is a
descendant of TForm and acts as the native .NET Windows Form, and a VCL.NET form.
BDP.NET Namespace
BDP.NET classes are found under the Borland.Data namespace.
BDP.NET Namespace
102
Namespace Description
Borland.Data.CommonContains objects common to all Borland Data Providers, including Error and Exceptions
classes, data type enumerations, provider options, and Interfaces for building your own
Command, Connection, and Cursor classes.
Borland.Data.Provider Contains key BDP.NET classes like BdpCommand, BdpConnection, BdpDataAdapter, and
others that provide the means to interact with external data sources, such as Oracle, DB2,
Interbase, and MS SQL Server databases.
Borland.Data.Schema Contains Interfaces for building your own database schema manipulation classes, as well
as a number of types and enumerators that define metadata.
For specific database runtime assemblies, copy them to the following location:
103
Borland Data Providers for Microsoft .NET
In addition to supporting the providers included in the .NET Framework, Delphi 8 for .NET includes
Borland Data Providers for Microsoft .NET (BDP.NET). BDP.NET, an implementation of the .NET
Provider, connects to a number of popular databases. This topic covers:
• Data Provider architecture.
• BDP.NET advantages.
• BDP.NET and ADO.NET components.
• BDP.NET data types.
• BDP.NET interfaces.
BDP.NET provides a high performance architecture for accessing data sources without a COM Interop
layer.
104
BDP.NET Advantages
BDP.NET provides a number of advantages:
• A unified programming model applicable to multiple database platforms.
• A high performance data-access architecture.
• The open architecture supports additional databases easily.
• Portable code to write once and connect to any supported databases.
• Consistent data type mapping across databases where applicable.
• Logical data types mapped to .NET native types.
• Unlike OLE DB, no need for a COM Interop layer.
Delphi 8 for .NET extends .NET support to additional database platforms, providing a consistent
connection architecture and data type mapping.
BDP.NET Interfaces
You can extend BDP.NET to support other DBMS by implementing a subset of the .NET Provider
interface. BDP.NET generalizes much of the functionality required to implement data providers. While
the .NET Framework gives you the capabilities to create individual data providers for each data source,
Borland has simplified the task by offering a generalized set of capabilities. Instead of building separate
providers, along with corresponding DataAdapters, DataReaders, Connection objects, and other
required objects, you can implement a set of BDP.NET interfaces to build your own data source plug-
ins to the Borland Data Provider.
Building plug-ins is a much easier task than building a completely new data provider. You build an
assembly that contains the namespace for your provider, as well as classes that encapsulate provider-
specific functionality. Much of the functionality you need to connect to, execute commands against, and
retrieve data from your data source has already been defined in the Borland Data Provider interfaces.
105
BDP.NET Data Types
BDP.NET data types map to .NET logical types. Dependant upon the database, BDP.NET data types
map to native data types. Where applicable, BDP.NET provides:
• Consistent data type mapping across databases.
• Logical data types mapped to .NET native types.
Data Types
The .NET Framework includes a wide range of logical data types. BDP.NET inherits logical data types,
providing built-in mappings to supported databases. BDP.NET supports logical data type mappings for
DB2, Interbase, MS SQL, MSDE, and Oracle.
DB2
BDP.NET supports the following DB2 type mappings.
BDP.NET logical data types for DB2
106
DECIMAL Decimal NA Decimal
BLOB Blob stBinary Byte[]
CLOB Blob stMemo Char[]
Interbase
BDP.NET supports the following Interbase type mappings.
BDP.NET logical data types for Interbase
107
SMALLMONEY Decimal NA Decimal
FLOAT Double NA Double
REAL Float NA Single
DATETIME DateTime NA DateTime
SMALLDATETIME DateTime NA DateTime
CHAR String stFixed String
VARCHAR String NA String
TEXT Blob stMemo Char[]
BINARY VarBytes NA Byte[]
VARBINARY VarBytes NA Byte[]
IMAGE Blob stBinary Byte[]
TIMESTAMP VarBytes NA Byte[]
UNIQUEIDENTIFIERGuid NA Guid
Oracle
BDP.NET supports the following Oracle type mappings.
BDP.NET logical data types for Oracle
108
ROWID String NA String
109
BDP.NET Component Designers
Almost all distributed applications revolve around reading and updating information in databases.
Different applications you develop using ADO.NET requires different requirements for working with data.
For instance, you might develop an application that simply displays data on a form. Or, you might develop
an application that provides a way to share data information with another company. No matter what your
intent is, you need to have an understanding of certain fundamental concepts about data approach in
ADO.NET.
Delphi 8 for .NET provides BDP.NET component designers to allow data integration in distributed,
scalable applications. By providing a number of these designers, you can work efficiently to access,
expose, and edit data. These designers let you browse and edit database server-specific schema
objects like tables, fields, triggers, and indexes, extensively allow you to use these existing tools to
connect and perform the same type of functionality to a number of industry standard databases, and
many others.
110
Component Designer Relationships
The major components of the database component designers include a Connection Editor to define a
live connection to a data source, a Command Text Editor to construct command text for command
components, a Configure Data Adapter to set up commands for a data adapter, and a Generate
Dataset to build custom datasets. Additionally, you can use the Data Explorer to browse database
server-specific schema objects and use drag and drop techniques to automatically populate data from
a data source to your Delphi for .NET project.
Connection Editor
Delphi 8 for .NET provides a Connection Editor designer that lets you select a connection configuration
or edit the named connections that are stored in an XML configuration file. This editor lets you add,
delete, and test your connection. Display this dialog by dropping the BdpConnection component from
the Tool Palette onto the form. Click the component designer verb at the bottom of the Object Inspector
to launch the dialog .
111
To populate the Tables and Columns list boxes with items and build SQL statements, you must have
defined a live BdpConnection. Otherwise, data will not be retrieved.
Generate DataSets
Delphi 8 for .NET provides a Generate Dataset designer that lets you build a Dataset. Some of the
advantages for using this tool include strong typing, cleaner code, and the ability to use code completion.
A Dataset is first derived from the base Dataset class and then uses information in an XML Schema file
(an .xsd file) to generate a new class. Information from the schema (tables, columns, and so on) is
generated and compiled into this new dataset class as a set of first-class objects and properties. Display
this dialog by dropping a BdpDataAdapter component from the Tool Palette onto the form. Click the
component designer verb at the bottom of the Object Inspector. If this component is not displayed,
choose Component Installed .NET Components to add it to the Tool Palette.
To populate the Tables and Column list boxes with items and build SQL statements, you must have
defined a live BdpConnection. Otherwise, data will not be retrieved.
Data Explorer
Delphi 8 for .NET provides a hierarchical database browser and editing tool which lets you browse
database server-specific schema objects including tables, fields, stored procedure definitions, triggers,
and indexes. The Data Explorer can also be extended to connect to other popular industry standard
databases. Once connected, you can view schema objects from other databases and modify
connections. Display this tool by selecting View Data Explorer. Additionally, the Data Explorer can
be used to drag and drop data from a data source to any Delphi 8 for .NET project. This feature is
especially useful if you have a long connection string and want to automatically populate the connection
string to avoid inputting the string improperly.
112
Delphi 8 for .NET Database Technologies
Delphi 7 provided a number of database technologies to allow you to accomplish different types of
database tasks. Some technologies, like BDE, were included mainly for backward compatibility to older
database systems. Others, like dbExpress, were included as a way to create cross-platform applications.
The move to the .NET Framework creates new opportunities and challenges for the database developer.
In most cases, the .NET Framework's ADO.NET providers or Borland's BDP.NET providers will solve
the challenges of creating new database applications. Sometimes, however, there is little to be gained
by creating an entirely new application when an existing application will serve your purposes. That's
why Borland is providing continued support for existing Delphi database technologies, in addition to
giving you easy access to the newer BDP.NET and ADO.NET.
Delphi 8 for .NET provides a migration path from Delphi database technologies running strictly on Win32
clients to the .NET Framework. In addition to being able to build new database applications using ADO.
NET and BDP.NET, you can migrate existing database applications to take advantage of .NET
capabilities. The Delphi database technologies now supported by Delphi 8 for .NET include:
• TADONETConnector
• dbExpress.NET
• DataSnap .NET Client (DCOM)
• IBX.NET (InterBase for .NET)
• BDE.NET
113
Building .NET Applications with IBX.NET
Delphi 8 for .NET provides you with access to InterBase databases, by way of InterBase Express
controls, in addition to the standard BDP.NET data adapter or the .NET Framework's ADO.NET
providers. IBX.NET controls allow you to connect to InterBase databases, access tables and datasets,
114
Getting Started with InterBase Express
InterBase Express (IBX) is a set of data access components that provide a means of accessing data
from InterBase databases. The InterBase Administration Components, which require InterBase 6, are
described after the InterBase data access components.
IBX components
The following components are located on the InterBase tab of the component palette.
TIBTable
TIBQuery
TIBStoredProc
TIBDatabase
TIBTransaction
TIBUpdateSQL
TIBDataSet
TIBSQL
TIBDatabaseInfo
IBSQLMonitor
TIBEvents
TIBExtract
TIBClientDataSet
Though they are similar to BDE components in name, the IBX components are somewhat different. For
each component with a BDE counterpart, the sections below give a discussion of these differences.
115
There is no simple migration from BDE to IBX applications. Generally, you must replace BDE
components with the comparable IBX components, and then recompile your applications. However, the
speed you gain, along with the access you get to the powerful InterBase features make migration well
worth your time.
IBDatabase
Use a IBDatabase component to establish connections to databases, which can involve one or more
concurrent transactions. Unlike BDE, IBX has a separate transaction component, which allows you to
separate transactions and database connections.
Warning: Tip: You can store the username and password in the IBDatabase
component's Params property by setting the LoginPrompt property to false after logging
in. For example, after logging in as the system administrator and setting the
LoginPrompt property to false, you may see the following when editing the Params
property:
user_name=sysdba
password=masterkey
IBTransaction
Unlike the Borland Database Engine, IBX controls transactions with a separate component,
TIBTransaction. This powerful feature allows you to separate transactions and database connections,
so you can take advantage of the InterBase two-phase commit functionality (transactions that span
multiple connections) and multiple concurrent transactions using the same connection.
Use an IBTransaction component to handle transaction contexts, which might involve one or more
database connections. In most cases, a simple one database/one transaction model will do.
To set up a transaction:
1. Set up an IBDatabase connection as described above.
2. Drop an IBTransaction component onto the form or data module
3. Set the DefaultDatabase property to the name of your IBDatabase component.
4. Set the Active property to true to start the transaction.
There are a variety of dataset components from which to choose with IBX, each having their own
characteristics and task suitability:
116
IBTable
Use an IBTable component to set up a live dataset on a table or view without having to enter any SQL
statements.
IBQuery
Use an IBQuery component to execute any InterBase DSQL statement, restrict your result set to only
particular columns and rows, use aggregate functions, and join multiple tables.
IBQuery components provide a read-only dataset, and adapt well to the InterBase client/server
environment. To set up an IBQuery component:
1. Set up an IBDatabase connection as described above.
2. Set up an IBTransaction connection as described above.
3. Add an IBQuery component to your form or data module.
4. Specify the associated database and transaction components.
5. Enter a valid SQL statement for the IBQuery's SQL property in the String list editor.
6. Set the Active property to true
IBDataSet
Use an IBDataSet component to execute any InterBase DSQL statement, restrict your result set to only
particular columns and rows, use aggregate functions, and join multiple tables. IBDataSet components
are similar to IBQuery components, except that they support live datasets without the need of an
IBUpdateSQL component.
The following is an example that provides a live dataset for the COUNTRY table in employee.gdb:
1. Set up an IBDatabase connection as described above.
2. Specify the associated database and transaction components.
3. Add an IBDataSet component to your form or data module.
4. Enter SQL statements for the following properties:
117
DeleteSQL DELETE FROM Country WHERE Country = :Old_Country
Note: Note: Parameters and fields passed to functions are case-sensitive in dialect
3. For example,
FieldByName(EmpNo)
IBStoredProc
Use IBStoredProc for InterBase executable procedures: procedures that return, at most, one row of
information. For stored procedures that return more than one row of data, or "Select" procedures, use
either IBQuery or IBDataSet components.
IBSQL
Use an IBSQL component for data operations that need to be fast and lightweight. Operations such as
data definition and pumping data from one database to another are suitable for IBSQL components.
In the following example, an IBSQL component is used to return the next value from a generator:
1. Set up an IBDatabase connection as described above.
2. Put an IBSQL component on the form or data module and set its Database property to the name
of the database.
3. Add an SQL statement to the SQL property string list editor, for example:
IBUpdateSQL
Use an IBUpdateSQL component to update read-only datasets. You can update IBQuery output with
an IBUpdateSQL component:
1. Set up an IBQuery component as described above.
2. Add an IBUpdateSQL component to your form or data module.
3. Enter SQL statements for the following properties: DeleteSQL, InsertSQL, ModifySQL, and
RefreshSQL.
4. Set the IBQuery component's UpdateObject property to the name of the IBUpdateSQL component.
118
5. Set the IBQuery component's Active property to true.
IBSQLMonitor
Use an IBSQLMonitor component to develop diagnostic tools to monitor the communications between
your application and the InterBase server. When the TraceFlags properties of an IBDatabase
component are turned on, active IBSQLMonitor components can keep track of the connection's activity
and send the output to a file or control.
A good example would be to create a separate application that has an IBSQLMonitor component and
a Memo control. Run this secondary application, and on the primary application, activate the TraceFlags
of the IBDatabase component. Interact with the primary application, and watch the second's memo
control fill with data.
IBDatabaseInfo
Use an IBDatabaseInfo component to retrieve information about a particular database, such as the
sweep interval, ODS version, and the user names of those currently attached to this database.
For example, to set up an IBDatabaseInfo component that displays the users currently connected to the
database:
1. Set up an IBDatabase connection as described above.
2. Put an IBDatabaseInfo component on the form or data module and set its Database property to the
name of the database.
3. Put a Memo component on the form.
4. Put a Timer component on the form and set its interval.
5. Double click on the Timer's OnTimer event field and enter code similar to the following:
IBEvents
Use an IBEvents component to register interest in, and asynchronously handle, events posted by an
InterBase server.
119
IBEvents->Events->Add("EVENT_NAME"); // C++ Example
If you have InterBase 6 installed, you can use the InterBase 6 Administration components, which allow
you to use access the powerful InterBase Services API calls.
The components are located on the InterBase Admin tab of the IDE and include:
TIBConfigService
TIBBackupService
TIBRestoreService
TIBValidationService
TIBStatisticalService
TIBLogService
TIBSecurityService
TIBLicensingService
TIBServerProperties
TIBInstall
TIBUnInstall
IBConfigService
Use an TIBConfigService object to configure database parameters, including page buffers, async mode,
reserve space, and sweep interval.
IBBackupService
120
Use an TIBBackupService object to back up your database. With IBBackupService, you can set such
parameters as the blocking factor, backup file name, and database backup options.
IBRestoreService
Use an TIBRestoreService object to restore your database. With IBRestoreService, you can set such
options as page buffers, page size, and database restore options.
IBValidationService
Use an TIBValidationService object to validate your database and reconcile your database transactions.
With the IBValidationService, you can set the default transaction action, return limbo transaction
information, and set other database validation options.
IBStatisticalService
Use an TIBStatisticalService object to view database statistics, such as data pages, database log,
header pages, index pages, and system relations.
IBLogService
IBSecurityService
Use an TIBSecurityService object to manage user access to the InterBase server. With the
IBSecurityService, you can create, delete, and modify user accounts, display all users, and set up work
groups using SQL roles.
IBLicensingService
IBServerProperties
IBInstall
Use an TIBInstall component to set up an InterBase installation component, including the installation
source and destination directories, and the components to be installed.
IBUnInstall
121
Building VCL.NET Applications
VCL.NET gives you the capability to provide your Delphi VCL applications and components to Microsoft .
NET Framework users. VCL.NET is an extended set of the VCL components that provide the means to
quickly and easily build advanced applications in Delphi. Now, with Delphi 8 for .NET you get all of the
benefits of the .NET Framework along with the ease-of-use and powerful component-driven application
development you've come to expect from Delphi.
Delphi 8 for .NET provides distinct application types for your use: you can create VCL.NET form
applications that run on the .NET Framework, but which use VCL.NET components and controls; you
can create .NET Windows Forms applications that use the underlying .NET Framework and .NET
controls while offering Delphi 8 for .NET code behind; you can create powerful ASP.NET applications
that use the underlying .NET Framework, ASP.NET controls, and also offer Delphi 8 for .NET code
behind. The following topics provide more information on how to take advantage of the new VCL.NET
provisions in Delphi 8 for .NET.
122
VCL.NET Overview
VCL.NET is the programming framework for building Delphi 8 for .NET applications using VCL
components. Delphi 8 for .NET and VCL.NET are intended to help users leverage the power of Delphi
when writing new applications, as well as for migrating existing Win32 applications to the .NET
Framework.
The aim with these technologies is to allow a Delphi developer to move to .NET, taking their Delphi skills
and much of their current Delphi source code with them. Delphi 8 for .NET supports Microsoft .NET
Framework development with the Delphi language and both Visual Component Library (VCL) for .NET
controls and Windows Forms controls. Delphi 8 for .NET ASP.NET also supports WebForms and SOAP/
XML Web Services application development.
VCL.NET is a large subset of the most common classes in VCL for Win32. Because the .NET Framework
was architected to accommodate any .NET-compliant language, in many cases Delphi source code that
operates on Win32 VCL classes and functions recompiles with minimal changes on .NET. In some
cases, the code recompiles with no changes at all. VCL.NET is a large subset of VCL, therefore it
supports many of the existing VCL classes. However, source code that calls directly to the Win32 API
requires source code changes. Also, dependent third-party Win32 VCL controls need to be available
in .NET versions as well for compatibility.
123
What is VCL?
VCL is an acronym for the Visual Component Library, a set of visual components for building Windows
applications in the Delphi language. VCL.NET is the same library of components, updated for use in
the .NET Framework. As you can see in the preceding illustration, VCL.NET and the .NET Framework
coexist within Delphi 8 for .NET. Both frameworks (VCL.NET and .NET) provide components and
functionality that allow you to build .NET applications:
• VCL.NET provides the means to create VCL Forms applications, which are Delphi forms that are .
NET-enabled, and use VCL.NET components.
• VCL.NET provides VCL non-visual components which have been .NET-enabled to access
databases and also allows you to access databases by way of ADO.NET and BDP.NET providers.
• .NET provides the means to build .NET Windows Forms, Web Forms, and Console applications,
using .NET components, with Delphi code behind.
• .NET provides data access components that allow you to access databases by way of ADO.NET.
You can build VCL Forms applications using VCL.NET components or Windows Forms applications
using .NET components. You can also build ASP.NET Web Forms applications using either VCL.NET
components or .NET components.
124
The Relationship between VCL.NET and the .NET Framework
It's important to understand the relationship between VCL.NET and the .NET Framework. The .NET
Framework provides a library of components, classes, and low-level functionality that supports a more
transparent type of application development, particularly for Web applications. The concept of
transparency, in this context, means that the framework manages much of the common functionality,
from the display of buttons to remoting functionality, without regard to the underlying implementation
language. It would be an understandable mistake to assume that VCL.NET is subordinate to the .NET
Framework, and yet, it is more accurate to view VCL.NET and the .NET Framework as functionally
equivalent. Like the .NET Framework, VCL.NET provides libraries of components, controls, classes,
and lower-level functionality that help you build Windows Forms, Web Forms, and Console applications
that run on the current Windows .NET Framework platform.
Yes and No. You will still need the .NET runtime to use VCL.NET, but you can build complete
applications using VCL.NET components that will run on .NET platform.
Can you build Delphi 8 for .NET applications without using VCL.NET?
Yes, you can create Windows Forms, Web Forms, and Console applications using Delphi 8 for .NET
code.
What it really means for you, as a developer, is that you can use Delphi 8 for .NET to create powerful .
NET applications using .NET components, or that you can use VCL.NET components that have been
migrated from the Delphi VCL. If you have existing Delphi VCL applications that you want to run on
Windows XP or other platforms that support the .NET Framework, you can easily port those applications
by using Delphi 8 for .NET.
VCL.NET Components
As a Visual Component Library for .NET, the heart of VCL.NET consists of a set of components, visual
and non-visual. VCL.NET continues to build on the concept of constructing applications visually, thus
eliminating as much hand-coding as possible.
Visual Components
Delphi 8 for .NET provides a set of visual components, or controls, that you can use to build your
applications. In addition to the common controls such as buttons, text boxes, radio buttons, and check
boxes, you can also find grid controls, scroll bars, spinners, calendar objects, a full-featured menu
designer, and more. There is, however, a major difference in the way these controls are represented as
opposed to the way visual components are represented by other frameworks, such as the .NET
Framework.
In the Code Editor of Delphi 8 for .NET you will not see a code representation of your visual components.
In IDEs for other languages, such as C# or Java, it is common to see code-centric representations of
125
forms and other visual components. These representations include physical definitions, such as size,
height, and other properties, as well as constructors and destructors for the components.
Delphi 8 for .NET is a resource-centric type of system, which means that the primary code-behind
representations are of event handlers that you fill in with your program logic. Visual components are
declared and defined in textual files, given the extension of .dfm (for Delphi Forms) or .nfm (for Delphi
8 for .NET forms). The .nfm files are created by Delphi 8 for .NET as you design your VCL Forms on
the Forms Designer, and are listed in the resource list in the Project Manager for the given project.
Non-Visual Components
You can use non-visual components to implement functionality that is not necessarily exposed visually.
For example, you can accomplish data access by using non-visual components like the BDP.NET
components, which provide database connectivity and dataset access. Even though these components
don't have a visual runtime behavior, they are represented by components in the Tools Palette at
designtime. VCL.NET provides a variety of non-visual components for data access, server functions,
and more.
Classes
VCL.NET also includes classes that are not components. In other words, they descend from TObject
but not from TComponent, and are typically used for accessing system objects such as a file, or for
transient tasks. Many of these tasks have parallel classes in the .NET Framework.
Classes, whether components or not, contain a number of characteristics, which can be categorized as
properties, methods, and events. In addition, most elements of a class are of a particular type, for
instance, simple data types like integers and strings, or complex data types, such as specific class types.
These elements, properties, methods, events, and types, allow you to make controlled modifications to
your classes, generally by way of the Object Inspector in the IDE. The use of these elements is just one
more way to simplify application development and to encourage the visual approach.
VCL Forms
Delphi 8 for .NET provides a combined Code Editor and VCL Forms Designer, which gives you access
to a rich set of VCL.NET controls and non-visual components for drag-and-drop user interface design,
code-behind code generation, integrated debugging, and project building capabilities. VCL.NET Forms
are descendants of TObject and accept any VCL.NET controls or VCL controls that have been updated
to conform to .NET Framework requirements.
Windows Forms
Windows Forms and their available components are provided by the .NET Framework. Delphi 8
for .NET includes an integrated Code Editor and Windows Form Designer that allows you to build fully-
compliant .NET Windows applications from the ground up, using only .NET components. In Delphi 8
126
for .NET the Windows Form object is created as a TWinForm, which is a descendant of the TForm class
and accepts any fully-compliant .NET components.
Web Forms
Web Forms and their available components are provided by the .NET Framework. Delphi 8 for .NET
includes an integrated Code Editor and Web Form Designer that allows you to build fully-
compliant .NET ASP.NET applications from the ground up, using only .NET components. In Delphi 8
for .NET the Web Form object accepts any fully-compliant .NET components. Delphi 8 for .NET also
includes a full set of data aware database components for ASP.NET.
Database Providers
Most of the current VCL database access technology is also being developed for .NET in addition to full
support for ADO.NET including the Borland Data Providers. You are able to use such .NET-enabled
data access technologies as dbExpress.NET, BDE.NET, the DataSnap Client for .NET, and IBX.NET.
Each of these technologies is exposed as a set of non-visual components, but can also be instantiated
in code.
Object Pascal
All code-behind code generation in Delphi 8 for .NET results in Object Pascal, more commonly known
as Delphi code. When using Delphi 8 for .NET you will see code files created with the extension .pas,
which indicates a pascal file.
Borland.VCL Namespace
VCL.NET classes are found under the Borland.Vcl namespace. Database-related classes are in the
Borland.Vcl.DB namespace. Runtime library classes are in the Borland.Vcl.Rtl namespace.
If you are a user of earlier Delphi versions, you will find that the unit files with which you are familiar,
have been bundled in corresponding Borland.Vcl namespaces. In some cases, units have been moved,
however, every attempt has been made to identify the namespaces in meaningful ways, and in ways
that will assist you in finding the functionality you want.
Source files for all of the Delphi 8 for .NET objects are available in the c:\Program Files\Borland\BDS
\2.0\Source subdirectory.
127
need to be revised to one of the .NET types. Many Delphi objects have been already updated to
accommodate these type restrictions, but your code may include references to pointers or unsupported
types. For more information, refer to the Porting information in this Help system, and to the Language
Guide.
128
Porting VCL Applications
When porting VCL applications from Delphi 7 to Delphi 8 for .NET, there are a number of issues you
need to consider. Along with basic language elements that need to be replaced or modified, there are
strategies that you should follow to make sure that you port your applications fully and reliably.
In addition, there are critical issues in working with the Win32 API, using crackers, migrating char types,
and other topics.
Programming in the garbage collected environment of .NET brings a number of new issues related to
allocating and disposing objects. These issues are discussed in Memory Management Issues on
the .NET Platform.
129
Porting Web Service Client Applications
The .NET Framework employs a major architectural shift in how it handles web services and web service
clients. As such, your existing web service client applications need to be modified to operate on the
framework. Delphi 8 for .NET does not support the RIO components, and uses a more transparent .NET
approach to managing web service client applications. You will need to eliminate RIO components and
modify the way you access WSDL documents.
130
Building Reports in Delphi 8 for .NET
Delphi 8 for .NET ships with the Rave Reports from Nevrona. Using Rave, you can build full-featured
reports for your Delphi 8 for .NET applications, and you can create solutions that include reporting
capabilities which can be used and customized by your own customers. In addition, the ComponentOne
tools that ship with Delphi 8 for .NET also include components for creating and generating reports, which
can be integrated into your applications.
131
Using Rave Reports in Delphi 8 for .NET
The Delphi 8 for .NET environment supports the integration of certain Rave Reports objects in your
applications. This integration allows you to create a report using the Rave Reports Designer or to drag-
and-drop Rave Reports ActiveX components directly onto your Windows Forms and Web Forms in the
Delphi 8 for .NET Designer. You can enable your application users to create and display their own
reports, or to display existing reports they have built using Rave Reports. The Delphi 8 for .NET
integration with Rave Reports allows you to:
• Include new report objects in projects.
• Drag-and-drop Rave Reports ActiveX objects onto Windows Forms and Web Forms.
132
Procedures
133
Getting Started
134
Adding Components to a Form
To add components to a form
1. On the Tool Palette, select a visual or nonvisual component.
2. Double-click the component to place it on the form, or click the component once and then click an
area within the form where you want the component to appear. Alternatively, you can click and drag
the component onto the form.
If you add a nonvisual component to the form, the component tray appears at the bottom of the
designer surface.
135
Adding References
You can integrate your legacy COM servers and ActiveX controls into managed applications by adding
references to unmanaged DLLs to your project, and then browse the types just as you would with
managed assemblies.
To add references
1. From the main menu, choose Project Add References. The Add References dialog appears.
2. Select either a legacy COM server or ActiveX control to integrate into your managed application.
4. Click OK.
Tip: Alternatively, in the Project Manager, right-click the Reference folder and
choose Add Reference.
136
Adding and Removing Files
To add a file to a project
1. Choose Project Add to Project.
The file appears below the Project.exe node of the Project Manager.
137
Adding Templates to the Object Repository
You can add your own objects to the Object Repository as templates to reuse or share with other
developers. Reusing objects lets you build families of applications with common user interfaces and
functionality to reduce development time and improve quality.
3. Enter the project name, description, and author information in the dialog box.
138
Copying References to a Local Path
During runtime, assemblies must be in the output path of the project or in the Global Assembly Cache
(GAC) for deployment. If your project contains a reference to an object that is not in one of the two
locations, the reference must be copied to the appropriate output path.
2. Set the Copy Local option to copy the file to the output directory.
Note: The IDE maintains the Copy Local setting until you change it.
139
Creating a Component Template
You can save selected, preconfigured components on the current form as a reusable component
template accessible from the Tool Palette.
2. In the Object Inspector, set the component properties and events as desired.
3. Select the components that you want to save as a component template. To select several
components, drag the mouse over them. To select all of the components on the form, choose
Edit Select All.
5. Specify a name, a Tool Palette category, and an icon for the template.
6. Click OK.
Your new template appears immediately on the Tool Palette, in the category that you specified.
The components in the component template are added to the form, along with their preconfigured
properties and events. You can reposition the components independently, reset their properties, and
create or modify event handlers for them, just as if you had placed each component in a separate
operation.
140
2. Choose the Delete [template name] Button command.
141
Creating a Project
To add a new project
1. Choose Project Add New Project.
142
Customizing the Form
To customize the form
1. Choose Tools Options.
2. From the Options dialog box, scroll down to Windows Forms Designer.
3. Enable or disable the snap to grid and show grid features by selecting and deselecting the check
boxes.
Tip: The changes will affect only forms created after these options are changed. To
change the settings for existing forms, set the GridSize, DrawGrid, and SnapToGrid
properties of the form.
143
Customizing Toolbars
To arrange your toolbars
1. Click the grab bar on the left side of the toolbar.
2. From the toolbar, not the Customize dialog box, drag the tool from the toolbar until its icon displays
an X and release the mouse button.
4. From the Commands list, drag the selected icon and command onto the toolbar of your choice.
144
Customizing the Tool Palette
To arrange individual components
1. Select the component.
2. Hold and drag the component anywhere within the Tool Palette to rearrange it.
145
Docking Tool Windows
The Auto-Hide feature lets you undock and hide tool windows, such as the Object Inspector, Tool
Palette, and Project Manager, but still have access to them.
The tool window is replaced by one or more tabs at the outer edge of the IDE window.
2. To display the tool window, position the cursor over the tab.
3. To slide the tool window out of view, move the cursor away from the tool window.
4. To redock the tool window, click the push pin until it points down.
2. Select a location to drop the tool window and release the mouse button.
2. Select a location to drop the tool window and release the mouse button.
146
Filtering Searched Components
To filter searched components
1. On the Tool Palette, start typing the name of the component that you want to find in the search text
box.
A list of component names that match what you are typing appears in the Tool Palette.
147
Exploring .NET Assembly Metadata
The Delphi 8 for .NET IDE allows you to open and explore the namespaces and types contained with
a .NET assembly. The assembly metadata is displayed in a Windows Explorer-style presentation, with
a left pane containing a tree of the namespaces and types within the assembly. The right pane displays
specific information on the selected item in the tree. The Call Graph tab shows you a list of the methods
called by the selected method, as well as a list of the methods that call the selected method.
2. In the Open dialog box, open the Files of type drop-down list, and select Assembly Metadata.
3. Navigate to the folder where the .NET assembly is located. Select the assembly and click Open.
You can open multiple .NET assemblies in the metadata explorer. Each open assembly is displayed in
the tree in the left-pane; the top-level node for a .NET assembly is denoted by the icon.
To close a particular .NET assembly, right click on the top-level icon and select Close.
The top half of the Call Graph tab shows you a list of methods that call the method you selected in the
left-pane.
The bottom half of the Call Graph tab shows you the methods called by the method you selected in the
left-pane.
Methods that exist in the same assembly as the currently selected method will appear as clickable links,
and are displayed in blue underlined text. Clicking on a link will cause that method to become selected
in the tree in the left-hand pane.
Tip: You can use the browser buttons on the toolbar to navigate backwards and
forwards to previously selected items in the left-pane.
148
Exploring Windows Type Libraries
The Delphi 8 for .NET IDE allows you to open and inspect the interfaces and other types contained
within a Windows type library. The type library contents are displayed in a Windows Explorer-style
presentation, with a left pane containing a tree of the interface and type definitions within the type library.
The right pane displays specific information on the selected item in the tree. The type library explorer
can open a .TLB file, as well as OCX controls, and .DLL and .EXE files that have type libraries as
embedded resources.
2. In the Open dialog box, open the Files of type drop-down list, and Type Library. This will set the file
filter to display files with extensions of .TLB, .OLB, .OCX, .DLL, and .EXE.
3. Navigate to the folder where the type library is located. Select the file and click Open.
You can open multiple type libraries in the explorer. Each open type library is displayed in the tree in
the left-pane; the top-level node for a type library is denoted by the icon.
To close a particular type library, right click on the top-level icon and select Close.
149
Installing Custom Components
If you create custom components or obtain them from a third-party vendor, you can install them on the
Tool Palette and then use them in your applications.
3. Navigate to the folder that contains the assembly which contains the components to install and
select it.
Alternatively, you can enter the name of the full path to the assembly in the File Name field.
4. Click Open.
The Installed .NET Components dialog displays the components from the assembly.
5. Verify that the components you want to install on the Tool Palette are checked.
6. Click OK.
150
Renaming Files using the Project Manager
Renaming a file changes the name of the file in both the Project Manager and on disk.
To rename a file
1. In the Project Manager, right-click the file that you want to rename. Its context menu is displayed.
2. Choose Rename.
If the file has associated files that appear as child nodes in the Project Manager tree, those files
are automatically renamed.
151
Saving Desktop Layouts
You can switch between multiple desktop layouts whenever you want. Choose a layout from the drop-
down pick list box located on the main menu. Additionally, you can save your desktop or debug desktop
layouts as default.
3. Click OK.
3. Click OK.
152
Setting Component Properties
After you place your components on your designer, set their properties using the Object Inspector. By
setting a component’s properties, you can change the way a component appears and behaves in your
application. Because properties appear during design time, you have more control over a component’s
properties and can easily modify them without having to write additional code.
2. Set the component properties by entering values in the text box or through an editor.
153
Setting Dynamic Properties
Many of the .NET Framework objects support dynamic properties. Dynamic properties provide a way
to change property values without recompiling an application. The dynamic properties and their values
are stored in a configuration file, along with the application's executable file. Changing a property value
in the configuration file causes the change to take effect the next time the applications runs. Dynamic
properties are useful for changing an application after it has been deployed.
2. In the Object Inspector, expand (DynamicProperties) and click (Advanced). If the object does not
support dynamic properties, (DynamicProperties) is not displayed.
Tip: If the Object Inspector is arranged by category, (DynamicProperties) is displayed
under Configurations.
3. Click the ellipsis button next to (Advanced) to display the Dynamic Properties dialog box.
This dialog lists all of the properties that can be stored in the configuration file.
4. Select the properties that you want to store in the configuration file.
5. Optionally, you can override the default key name listed in the Key mapping field.
6. Click OK.
The dynamic properties are marked with an icon in the Object Inspector.
Delphi 8 for .NET creates an XML file named app.config (for a Windows application) or Web.config
(for a Web application) in the project directory. This file lists the dynamic properties and their current
values.
Delphi 8 for .NET creates a file named projectname.exe.config (for a Windows application) or
projectname.dll.config (for a Web application) in the same directory as the application's executable
or DLL file.
154
2. Open the file in a text editor.
3. Locate the add key= statement for the property to be changed and edit the value.
The next time the application runs, the changed property value will be in effect.
155
Setting Project Options
You can manage application and compiler options for your project. Making changes to your project only
affects the current project. However, you can also save your selections as the default settings for new
projects.
2. Select Compiler and set your options to modify how you want your program to compile.
2. Select Application and specify a title and extension for your application.
2. User the Debugger page to pass command-line parameters to your application, specify a host
executable for testing a DLL, or load an executable into the debugger.
3. Use the Environmental Block page to indicate which environment variables are passed to your
application while you are debugging it.
156
Setting Properties and Events
Properties, methods, and events are attributes of a component.
3. If an event handler already exists, select it from the drop-down box. Otherwise, double-click the
event to switch to Code view.
4. Type the code you want to execute when the event occurs.
157
Setting Tool Preferences
You can customize the appearance and behavior of many tools and features, such as the Object
Inspector, Code Editor, and integrated debugger.
2. Review the options in each tool category and customize the settings to suit your needs.
3. Click OK.
158
Using To-Do Lists
A to-do list records and displays tasks that need to be completed for a project.
3. In the Add To-Do Item dialog box, enter a description of the task and adjust the other fields as
necessary.
4. Click OK.
3. In the Add To-Do Item dialog box, select the item that you want to add.
4. Click OK.
The item is added as a comment to your code, beginning with the word TODO.
2. In the To-Do List dialog box, click the checkbox next to the item to indicate completion.
The item remains in the list, but the text is crossed out. If the item was added as a comment to code,
the comment is updated to indicate DONE instead of TODO.
159
2. Right-click the To-Do List dialog box and select Filter.
3. Choose either Categories, Owner, or Item types, depending on which you want to filter.
4. In the Filter To-Do List dialog box, uncheck the items that you want to hide in the to-do list.
5. Click OK.
The to-do list is redisplayed, with the filtered items hidden. The status bar at the bottom of the To-Do
List dialog indicates how many items are hidden due to filtering.
The item is removed from the to-do list. If the item was added as a comment to code, the comment is
also removed.
160
Writing Event Handlers
Your source code usually responds to events that might occur to a component at runtime, such as a
user clicking a button or choosing a menu command. The code that responds to an occurrence is called
an event handler. The event handler code can modify property values and call methods.
2. To create the default event for the component, double-click the component on the form.
To choose another event for the component, click the Events tab in the Object Inspector, locate the
event, and double-click its text box.
3. Type the code that will execute when the event occurs at runtime.
161
ASP.NET
162
Building an ASP.NET Database Application
The following procedure describes the minimum number of steps required to build a simple ASP.NET
database application using BDP.NET. After generating the required connection objects, the project
displays data in a DataGrid.
BDP.NET includes component designers to facilitate the creation of database applications. Instead of
dropping individual components on a designer, configuring each in turn, use BDP.NET designers to
rapidly create and configure database components. The following procedure demonstrates the major
components of ASP.NET, ADO.NET, and BDP.NET at work.
Building an ASP.NET application with BDP.NET components consists of four major steps:
1. Create an ASP.NET project.
2. Configure BDP.NET connection components and a data source.
3. Add a DataBind call.
4. Connect a DataGrid to the connection components.
Tip: For testing purposes, use the employee.gdb database included with Interbase, if
included with your version of the product.
2. Set the various read and write attributes of the project as needed or accept the defaults.
3. Click OK.
163
The Web Forms designer appears.
3. If necessary, select the Command tab. From the Connection drop-down, select New Connection.
To set up a connection
1. In Borland Data Provider: Connections Editor, select the appropriate item from the Connections list.
Note: If referring to a database on the local disk, prepend the path with localhost:.
If using Interbase, for example, you would enter the path to your Interbase
database: localhost:C:\Program Files\Borland\Interbase\Examples\employee.gdb
(or whatever the actual path might be for your system).
3. Complete the UserName and Password fields for the database as needed.
In the Command tab, the areas for Tables and Columns are updated with information from your
connection.
164
To set a command
1. In the Select area, enter an SQL command.
Tip: For Interbase's employee.gdb database, you might enter select * from SALES, as
an example.
3. Click Refresh.
6. Click OK.
8. In Object Inspector, select the Active property drop-down and set the value to True.
2. In Object Inspector, select the DataSource property drop-down. Select the DataSet component that
you generated previously (the default is DataSet1).
3. In Object Inspector, select the DataMember property drop-down. Select the appropriate table.
165
3. Set the Load event to Page_Load.
The code-behind designer appears, cursor in place between event handler brackets.
Self.dataGrid1.DataBind();
Note: If you are using data aware controls, for instance from a third-party provider,
you may not need to code the DataBind call.
The application compiles and the HTTP server displays a Web Form with the datagrid.
While presenting a minimum number of steps required to build a database project, the preceding
procedure demonstrates the major components of the ASP.NET, ADO.NET, and BDP.NET
architectures at work, including: providers, datasets, and adapters. The adapter connects to the physical
data source via a provider, sending a command that will read data from the data source and populate
a dataset. Once populated, a datagrid displays data from the dataset.
Once created, use other BDP.NET designers to modify and maintain the components of your project.
166
Building an ASP.NET "Hello world" Application
Though simple, the ASP.NET "Hello world" application demonstrates the essential steps for creating an
ASP.NET application. The application uses a Web Form, controls, and an event that will display a result
in response to a user action.
Building the ASP.NET "Hello world" application consists of five major steps:
1. Create the ASP.NET project.
2. Accept the default Web server settings.
3. Create the ASP.NET page and add components.
4. Create the application logic.
5. Run the application.
3. Click OK.
5. In the Location field, accept the default or enter [Inetpub]\HelloWorld, where [Inetpub] is the
directory location for IIS projects (for example, C:\Inetpub\wwwroot\HelloWorld).
2. Set the various read and write attributes of the project as needed or accept the defaults.
Tip: For most ASP.NET projects, the default settings will suffice.
3. Click OK.
167
The Web Forms designer appears.
2. If necessary, select the Button control. In Object Inspector, set the button's Text property to Hello,
world!.
The code-behind designer appears, cursor in place between event handler brackets.
The application compiles and the HTTP server displays a Web Form in your default browser with
the "Hello, world!" button.
The server updates the page with the response, "Hello, developer!".
168
Building an ASP.NET Application
The following procedure describes the generic steps required to build a simple ASP.NET project. For
more advanced topics, refer to related information following the procedure.
3. Click OK.
2. Set the various read and write attributes of the project as needed or accept the defaults.
3. Click OK.
169
The Web Forms designer appears.
2. From the Tool Palette, drag and drop components onto the designer to define the user interface.
The code-behind designer appears, cursor in place between event handler brackets.
The application saves and compiles. Once you compile the application, the generated aspx file
displays HTML in the default web browser.
170
Building an Application with DB Web Controls
The following procedure describes the minimum number of steps required to build a simple ASP.NET
database application using DB Web Controls and BDP.NET. After generating the required connection
objects, the project displays data in a DBWebGrid with a DBWebNavigator. Additional information is
provided for other common DB Web Controls.
Users should already be familiar with creating an ASP.NET project using BDP.NET.
Building the simple ASP.NET application with DB Web Controls and BDP.NET consists of three major
steps:
1. Prepare an ASP.NET project with BDP.NET or other connection components.
2. Drag and drop a DBWebDataSource onto the designer and set its DataSource property to a
DataSet, DataView or DataTable.
3. Drag and drop a DBWebGrid and other DB Web Control onto the designer.
Tip: Dragging and dropping web components places them in absolute position. Double
clicking leaves them in flow layout. Flow layout is much easier to manage. For instance,
if the controls are at an absolute position and they change sizes at runtime (as might
happen to a DataGrid when you add and remove rows), the grid may overwrite other
controls.
2. Set up BDP.NET or other data access components, setting the DataSource property to an existing
DataSet, DataView, or DataTable.
Tip: For more information about setting up BDP.NET data access components, see the
related procedure for Building an ASP.NET Database Application. Instead of using a
DataGrid and adding a DataBind call, in the following procedure you'll use DB Web
Controls without a DataBind call.
To configure a DBWebDataSource
1. Drag and drop a DBWebDataSource onto the designer.
2. In Object Inspector, select the DataSource property. Select an existing data source (for example,
DataSet1).
171
To configure DB Web Controls
1. Drag and drop a DBWebNavigator onto the designer.
3. In Object Inspector, select the TableName property drop-down. Select the appropriate Table (for
example, Table1).
Tip: If no TableName is available, verify that the BdpDataAdapter Active property is set
to true.
5. In Object Inspector, select the DBDataSource property drop-down. Select the DBWebDataSource
(for example, DBWebDataSource1).
6. In Object Inspector, select the TableName property drop-down. Select the appropriate Table (for
example, Table1).
The application compiles and the HTTP server displays a Web Form with DBWebGrid displaying
data.
172
Extending DB Web Controls
The following procedure describes the steps required to extend DB Web Controls. The system for
building data-aware web controls is designed to be extensible. To build a new data-aware web control
requires only a single class, that which implements the new control.
using System;
using System.Data;
using System.IO;
using System.Collections;
using System.Collections.Specialized;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Borland.Data.Web; // add reference to this to project
using System.ComponentModel;
using System.ComponentModel.Design;
173
using System.Drawing;
using System.Drawing.Design;
Note: You can use the existing DBWebControlDesigner as it relies only on the
control having implemented one of the link interfaces.
[Designer("Borland.Data.Web.DBWebControlDesigner"),
ToolboxData("<{0}:MyControl runat=server></{0}:MyControl>")]
4. Override OnPreRender so it calls DataBind() before the control is rendered. Also, if the control
is going to update a column value, then RegisterHiddenField needs to be called.
174
Splitter + IColumnLink.ColumnName);
DataBind();
}
void IPostBackDataHandler.RaisePostDataChangedEvent()
{
}
6. Implement IDBWebDataLink, IDBWebColumnLink, and so-on, and provide public access methods
to their properties:
Note: Specify the use of the Borland.Data.Web.TableNamePropEditor and
Borland.Data.Web.ColumnNamePropEditor where appropriate.
string IDBWebDataLink.TableName
{
get
{
return IDataLink.TableName;
}
set
{
IDataLink.TableName = value;
}
}
IDBDataSource IDBWebDataLink.DBDataSource
{
get
{
return IDataLink.DBDataSource;
}
set
{
175
IDataLink.DBDataSource = value;
}
}
[Editor(typeof(Borland.Data.Web.TableNamePropEditor), typeof
(UITypeEditor)), Category("DBWebControl"),
DefaultValue(null)]
public string TableName
{
get
{
return IDataLink.TableName;
}
set
{
IDataLink.TableName = value;
}
}
[Category("DBWebControl"),
DesignerSerializationVisibility(DesignerSerializationVisibility.
Hidden), DefaultValue(null)]
public IDBDataSource DBDataSource
{
get
{
return IDataLink.DBDataSource;
}
set
{
IDataLink.DBDataSource = value;
}
}
7. Implement DataBind().
176
8. Optionally, override the Render method. If all you only need to set the data from
DBDataSource settings, then you don't need to override Render. If you want to code special
behavior, then you can override Render and write the output anyway you wish. Following is a
sample used for the DBWebGrid so that a special grid is generated at design time when the
properties for the IDBWebDataLink interface have not yet been set:
177
Using the HTML Tag Editor
In addition to the aspx code editor that in included every time you create a new ASP.NET Web Forms
application, Delphi 8 for .NET provides an HTML Tag Editor, which is a re-sizable window at the bottom
of the Web Forms Designer. The tag editor allows you to review and modify HTML tags at the same
time you view your controls, rather than forcing you to switch back and forth between the aspx code
editor and the Web Forms Designer.
3. Modify a control.
The editor displays the HTML code, highlighted in a red font, for this control in its gray header bar.
2. To view the individual control's code, click anywhere on the Web Form Designer surface to deselect
the control.
The HTML code appears in the tag editor window, with syntax highlighting. The gray header of the
tag editor now displays the higher level tag, usually the FORM tag that defines this particular Web
Form.
Note: If a control is defined using several lines of HTML code, when you select the
control, the first line of the code is displayed in the gray header of the tag editor.
The additional code appears below in the tag editor window.
178
The editor displays the HTML code for each element, highlighted in a red font, in its gray header,
for each control as you drop them on the Web Form Designer surface.
2. Click anywhere on the Web Form Designer surface to deselect all controls.
This displays the code for all the controls in the tag editor, with syntax highlighting.
To modify a control
1. Click anywhere on the Web Form Designer surface to deselect all controls.
2. Locate the tag that corresponds to the control you want to modify.
3. Modify the code, and the change is immediately reflected in the control on the Web Form Designer
surface.
2. Select Properties.
4. Click OK.
179
2. To zoom in so that you can view only the content within the FORM tags, click the right-hand blue
arrow in the gray header of the tag editor.
Note: You can only use this feature when the insertion pointer is somewhere in the
tag editor, rather than on the Web Form Designer surface.
180
Compiling and Building Applications
181
Adding Languages to a Project
You can add languages to your project by using the Satellite Assembly Wizard. For each language that
you add, the Satellite Assembly Wizard generates a separate satellite assembly project in your project
group. Each satellite assembly project is given an extension based on the language’s locale.
2. With your project open in the IDE, choose Project Languages Add. Alternatively, you can
choose File New Other Current Project Satellite Assembly Wizard.
3. Make sure your project is selected in the list that appears in the dialog and then click Next.
4. Click the checkbox next to the languages that you want to add to your project and then click Next.
5. Review the directory path information that the wizard will use for the language’s satellite assembly.
Tip: To change the path, click the path, and then click the ellipsis button to browse to
a different directory.
When you are satisfied with the path information, click Next.
6. If no satellite assembly for the language exists yet, Create New appears in the Update Mode column.
Click Next.
If a satellite assembly exists for the language in the directory you have specified, click in the Update
Mode column to select Update or Overwrite. Choose Update to keep and modify the existing satellite
assembly project. Choose Overwrite to create a new, empty project and to delete the old project
and any translations it contains. Click Next.
7. Review the summary of what the wizard will do and click Finish to create or update the satellite
assemblies for the languages you have selected.
If the wizard asks to generate a drcil file, click Yes. Any project that uses its own resource strings
(instead of previously compiled rc files) needs a drcil file.
If you are sure that no new drcil files are needed (because your project does not introduce any
resource strings of its own), select Skip drcil files that are not found in the final dialog. This prevents
the wizard from generating, or asking to generate, drcil files.
8. Click Yes to compile. Click Yes again to save your project group.
182
The generated projects contain untranslated copies of the resource strings in your original project. By
default, the Translation Manager is displayed, enabling you to begin translating the resource files.
3. Check the languages that you want to remove and then click Next.
4. Click Finish.
The Satellite Assembly Wizard removes the selected satellite assemblies from your project file, but does
not delete the assemblies, the source of the assemblies, or the directories in which they reside.
2. Specify the directory path of the old satellite assembly in the appropriate dialog.
If a satellite assembly already exists for the language (in the directory you have specified), click in
the Update Mode column to select Update or Overwrite. Choose Update to keep and modify the
existing assembly project. Choose Overwrite to create a new, empty project and to delete the old
project and any translations it contains.
183
Building Packages
You can create packages easily in Delphi 8 for .NET and include them in your projects.
This creates a new, empty package and makes an entry for it in the Project Manager, along with
two folders: one marked Contains and one marked Requires.
Note: If you want to add required files to the package, you must add compiled
packages (.dcpil, .dll) to the Required folder. Add uncompiled code files (.pas) to
the Contains folder.
5. Select Add....
8. Click OK.
4. Choose Add....
184
5. Browse to locate a package file.
7. Click OK.
3. Click Add....
4. Locate the package file you want to add to the Tool Palette.
5. Click Open.
6. Click OK.
185
Editing Resource Files in the Translation Manager
After you have added languages to your project by using the Satellite Assembly Wizard, you can use
the Translation Manager to view and edit your resource files. Within the Translation Manager, you can
edit resource strings directly, add translated strings to the Translation Repository, or get strings from
the Translation Repository.
4. Expand the project tree view to display the resource files that you want to edit. The resource files
are listed under the .NET Resources node.
Tip: Use the expand and collapse icons on the toolbar above the tree view.
5. Click the resource file you want to edit. The resource strings in the file are displayed in a grid in the
right pane.
6. Click the field that you want to edit and do one of the following:
8. Optionally, set the translation status for the string by using the drop-down list in the Status field.
9. Click the Save Translation icon on the toolbar above the grid to update the resource file.
Tip: To display the original form or translated form, click the Show original form and
Show translated form icons in the toolbar above the grid.
186
2. Choose Repository Add strings to repository.
The resource string is added to the Translation Repository and can be viewed by closing the Translation
Manager and choosing Tools Translation Repository.
2. Expand the project tree view to display the resource files that you want to edit. The .resx files are
listed under the .NET Resources node. The .nfm files are listed under the Forms node.
3. Click the resource file you want to edit. The resource strings in the file are displayed in a grid in the
right pane.
4. Right-click the field that you want to update and choose Repository Get strings from repository.
If the Translation Repository contains only one translation that matches the selected source string, it
copies that translation into the target language column. If the Repository contains more than one match
for the selected resource, its default behavior is to retrieve the first matching translation it finds.
Tip: To change this behavior, close the Transaction Manager and choose Tools
Translation Tools Options, click the Repository tab, and change the Multiple Find
Action setting.
3. Double-click the resource file that you want to update. The file opens in a text editor.
Tip: To change the text editor used by the Translation Manager, choose Tools
Translation Tools Options and change executable file specified in the External Editor
field.
187
Setting Up the External Translation Manager
If you are localizing an application and do not have the Delphi 8 for .NET IDE, you can use the External
Translation Manager (ETM) to perform the translations. The ETM provides the same basic functionality
as the Translation Manager in the product. To use the ETM, the developer must provide you with the
required ETM and project files. After you have finished the translations, you can send the translated files
back to the developer to add to the project.
1. Obtain from the developer copies of the files listed in the table below.
188
*71.bpl
A translation kit or zipped file containing the following project files:
satellite assembly created using the Satellite Assembly Wizard for each language to be translated
.bdsproj project file generated using File Save as in the ETM project
standalone translation repository (*.tmx) files
Note: The developer saves each satellite assembly in a separate root directory below
the project directory. When you return the translated files to the developer, the
developer copies the translated project into each language's root directory.
For the files parameter, you can enter the project group files or the project files.
189
Database
190
Adding a New Connection to the Data Explorer
You can add new connections to the Data Explorer, which persist as long as the connection object exists.
6. Click OK.
Tip: If you need to modify your new connection settings, right-click on your new
connection and scroll down to modify a connection. A Connection Editor dialog appear.
Enter your connection settings and click OK.
191
Browsing a Database in the Data Explorer
Once you have a live connection, you can use the Data Explorer to browse database objects.
This operation returns a result set into a tabbed Data Explorer page in the Code Editor.
Tip: You can also select a table in the Data Explorer and right-click to display a pop-up
menu with a Retrieve Data From Table command.
192
Building a Windows Forms Database Application
The following procedure describes the minimum number of steps required to build a simple ADO.NET
application using Windows Forms and BDP.NET. After generating the required connection objects, the
project displays data in a DataGrid.
BDP.NET includes component designers to facilitate the creation of database applications. Instead of
dropping individual components on a designer, configuring each in turn, use BDP.NET designers to
rapidly create and configure database components. The following procedure demonstrates the major
components of Windows Forms, ADO.NET, and BDP.NET at work.
Tip: For testing purposes, use the employee.gdb database included with Interbase, if
included with your version of the product.
2. Drag and drop a BdpDataAdapter component onto the designer. If necessary, select
BdpDataAdapter.
4. If necessary, select the Command tab. From the Connection drop-down, select New Connection.
Tip: Alternatively, use Data Explorer to drag and drop a table on to the designer surface.
Data Explorer sets the connection string automatically.
To set up a connection
1. In Borland Data Provider: Connections Editor, select the appropriate item from the Connections list.
193
Tip: If using Interbase, you would enter the path to your Interbase database.
3. Complete the UserName and Password fields for the database as needed.
In the Command tab, the areas for Tables and Columns are updated with information from your
connection.
To set a command
1. In the Select area, enter an SQL command.
Tip: For Interbase's employee.gdb database, you might enter select * from SALES, as
an example.
3. Click Refresh.
6. Click OK.
8. In Object Inspector, select the Active property drop-down and set the value to True.
194
2. In Object Inspector, select the DataSource property drop-down. Select the DataSet component that
you generated previously (the default is DataSet1).
3. In Object Inspector, select the DataMember property drop-down. Select the appropriate table.
While presenting a minimum number of steps required to build an ADO.NET project, the preceding
procedure demonstrates the major components of the Windows Forms, ADO.NET, and BDP.NET
architectures at work, including: providers, datasets, and adapters. The adapter connects to the physical
data source via a provider, sending a command that will read data from the data source and populate
a dataset. Once populated, a datagrid displays data from the dataset.
Once created, use other BDP.NET designers to modify and maintain the components of your project.
195
Creating Database Projects from the Data Explorer
You can drag and drop data from the Data Explorer to any forms such as Windows Forms or Web Forms,
and Global.asax files. to populate datasets and quickly build a database project. This allows you to
automatically hook up database components to your project and eliminates the need to provide a
connection string, which can be prone to errors if entered manually.
3. Choose File New Other and select a Delphi for .NET project.
4. Expand the Data Explorer Tree by drilling down to the Table or View level.
6. Specify the appropriate database properties for each database component, for instance, set the
Active property to True.
Note: A DataGrid will not appear automatically so make sure you drop a DataGrid
component onto your form to appropriately display data, when necessary.
196
Creating Table Mappings
Using the TableMappings property, you can map columns between a data source and an in-memory
dataset. This allows you to use different, often more descriptive names for your dataset columns. You
can also map a column in a database table to a column in the dataset different from that which is selected
by default. The TableMappings property also allows you to create a dataset that contains fewer or more
columns than those retrieved from the database schema.
Note: This procedure assumes you are using BDP.NET database components.
To create an application
1. Choose File New Windows Forms Application.
4. Drag-and-drop a table name onto your Windows Form to add a data source to your application.
You should see two objects in the Component Tray: a BdpDataAdapter and a BdpConnection.
For more information about how to create database applications, refer to the additional ADO.NET and
database topics in this Help system.
2. Click the Configure Data Adapter... designer verb to open the Data Adapter Configuration dialog.
197
5. Click OK.
This creates a new dataset and displays an icon for it in the Component Tray.
2. Double-click the Collections field for the TableMappings property in the Object Inspector.
3. If you want to use an existing dataset as a model for the columns, check the Use a dataset to
suggest table and column names checkbox.
This provides you with a list of column names from an existing dataset based on the schema of that
dataset. The column names are not linked to anything when you use this process.
4. If you checked the Use a dataset to suggest table and column names checkbox, you can choose
the dataset from the DataSet drop down list.
5. Select the source table from the Source table drop down list.
If there is more than one table in the data source, their names appear in the drop down list.
6. If you chose to use a dataset to suggest table and column names, and that dataset contains more
than one table, you can select the table you want to use from the Dataset table drop down list.
The column names from the source table and from the dataset should appear in the Column
mappings grid. As they are displayed by default, they represent the mapping from source to dataset;
in other words, the data adapter reads data from each column named on the left side of the grid
and stores the data in the dataset column named in the corresponding field on the right side of the
grid. You can change the names on either side by typing new names or by selecting different tables.
This allows you to store queried data into different dataset columns than the ones created in the
dataset by default.
7. If you want to modify a mapping, type a new name in the Dataset table column next to the target
Source table column.
This results in the data from the Source table column being stored in the new dataset column.
Note: If you want to reset the column names so that the dataset columns match
the data source columns, you can click the Reset button.
198
To delete a mapping
1. Select the grid row that you want to delete.
2. Click Delete.
This will cause the query to ignore that column in the source table and to not fill the dataset column
with any data.
199
Executing SQL in the Data Explorer
You can write, edit, and execute SQL in an SQL Window, which is available from within the Data
Explorer.
2. Select a connection.
To execute SQL
1. Enter a valid SQL statement or stored procedure name in the multi-line text box at the top of the
SQL Window.
2. Click Execute.
If the SQL statement or stored procedure is valid, the result set appears in the bottom pane of the
SQL Window.
Note: The SQL statement or stored procedure must operate against the current
connection and its target database. You cannot execute SQL against a database
to which you are not connected.
3. Click Clear to clear the SQL statement or stored procedure from the multi-line text box.
200
Handling Errors in Table Mapping
Whenever you perform any type of comparison function between a data source and an in-memory data
representation, there is potential for error. Errors can occur when a data source and its corresponding
dataset do not share uniform numbers of columns, or when column types in a data source do not
correspond to the column types in the dataset. In addition, other, internal errors can occur for which
there is no designtime workaround. You can use both the MissingMappingAction property and the
MissingSchemaAction property to respond to errors in your table mapping operations. Use the
MissingMappingAction when you want to specify how the adapter should respond when the mapping is
missing. Use the MissingSchemaAction when you want to specify how the adapter should respond when
it tries to write data to a column that isn't defined in the dataset.
2. Select Passthrough if you want the adapter to load the data source column data into a dataset
column of the same name, or, if there is no corresponding dataset column, if you want the adapter
to perform the action specified in the MissingSchemaAction property.
3. Select Ignore if you want to keep data from being loaded when data source columns are not properly
mapped to dataset columns.
This could occur if mapped columns are of incompatible data types, lengths, or have other errors.
4. Select Error if you want the adapter to raise an error that you can trap.
2. Select AddWithKey if you want the data source table or column added to the dataset and its schema
along with the table's or column's primary key information.
3. Select Ignore if you don't want a table or column added to the dataset, when that table or column
aren't already represented in the dataset schema.
201
Specify Ignore when you want the dataset loaded only with data explicitly specified in the table
mappings. This may be necessary if your adapter calls a stored procedure or a user-defined SQL
statement that returns more columns than are defined in the dataset.
4. Select Error if you want the adapter to raise an error that you can trap.
202
Modifying Connections in the Data Explorer
You can modify connections in a variety of ways from the Data Explorer.
To modify connections
1. Choose View Data Explorer.
2. Select a provider.
To refresh a connection
1. Choose View Data Explorer.
2. Select a provider.
4. Choose Refresh.
This operation reinitializes all connections defined for the selected provider.
To delete a connection
1. Choose View Data Explorer.
2. Select a connection.
This displays a confirmation message that asks if you want to delete the connection.
5. Click OK.
203
To modify a connection
1. Choose View Data Explorer.
2. Select a connection.
6. Click OK.
To close a connection
1. Choose View Data Explorer.
2. Select a connection.
To rename a connection
1. Choose View Data Explorer.
2. Select a connection.
204
5. Enter a new name.
6. Click OK.
The Data Explorer displays the connection with its new name.
205
Passing Parameters in a Database Application
The following procedures describe a simple application that allows you to pass a parameter value at
runtime to a DataSet. Parameters allow you to create applications at design time without knowing
specifically what data the user will enter at runtime. This example process assumes that you already
have your sample Interbase Employee database set up and connected. For purposes of illustration, this
example uses the default connector IBConn1, which is set to a standard location. Your database location
may differ.
To pass a parameter
1. Create a data adapter and connection to the Interbase employee.gdb database.
2. Add a text box control, a button control, and a data grid control to your form.
2. Click on the Data Explorer tab and drill down to find the IBConn1 connection under the Interbase
node.
3. Drag and drop the EMPLOYEE table onto the Windows Form.
This creates a BdpDataAdapter and BdpConnection and displays their icons in the Component
Tray.
4. Select the data adapter icon, then click the Configure Data Adapter... designer verb in the Designer
Verb area at the bottom of the Object Inspector.
5. Rewrite the SQL statement that is displayed in the Select tab of the dialog to:
206
SELECT EMP_NO, FIRST_NAME, LAST_NAME, SALARY FROM EMPLOYEE WHERE
FIRST_NAME = ?;
As you can see, this statement is limiting the number of fields. It also contains a ? character as part
of the Where clause. The ? character is a wildcard that represents the parameter value that your
application passes in at runtime. There are at least two reasons for using a parameter in this way.
The first reason is to make the application capable of retrieving numerous instances of the data in
the selected columns, while using a different value to satisfy the condition. The second reason is
that you may not know the actual values at design time. You can imagine how limited the application
might be if we retrieved only data where FIRST_NAME = 'Bob'.
8. Click OK.
You should be able to see your Select statement in the SelectCommand property drop down list box.
6. Set BdpType to String, DbType to Object, Direction to Input, Source Column to FIRST_NAME, and
ParameterName to emp.
7. Click OK.
8. In the Object Inspector, set the Active property under Live Data to True.
207
To add controls to the form
1. Drag and drop a TextBox control onto the form.
5. Arrange the controls how you want them to appear, making sure that the DataGrid is long enough
to display four fields of data.
2. Set the DataSource property to the name of the DataSet (dataSet1 by default).
This should display the column names of the columns specified in the SQL statement that you
entered into the data adapter.
Self.bdpSelectCommand1.Close();
/* This closes the command to make sure that we will pass the parameter
to */
/* the most current
bdpSelectCommand.
*/
Self.BdpDataAdapter1.Active := false;
/* This clears the data adapter so that we don't maintain old
data */
208
Self.bdpSelectCommand1.Parameters['emp'].Value := textBox1.
Text;
/* This sets the parameter value to whatever value is in the text
field. */
Self.BdpDataAdapter1.Active := true;
/* This re-activates the data adapter so the refreshed data appears in
the data grid. */
If you have changed the names of any of these items, you need to update these commands to
reflect the new names.
3. Type one of the names John, Robert, Roger, Kim, Terri, Katherine, or Ann into the text box.
This displays the employee number, first name, last name, and salary of the employee with that
name in the data grid. If there is more than one person with the same first name, the grid displays
all occurrences of employees with that name.
209
Using the Data Adapter Preview
Borland Delphi 8 for .NET provides a tool that enables communication between a data source and a
dataset. You can use the Data Adapter Preview to specify what data to move into and out of the dataset
either in the form of SQL statements or stored procedures that are invoked to read or write a database.
3. To limit the number of rows fetched, click the Limit rows check box.
4. Enter the number of rows you want the result set to contain, in the Rows to fetch text box.
5. Click Refresh to re-execute the query and to refill the list box with the new number of rows.
210
Using the CommandText Designer
In order to create a DataSet, your BdpDataAdapter needs to have at least a SQL Select statement
defined for the CommandText property. This statement, once built, appears as the CommandText
property of the BdpCommand object for the BdpDataAdapter. You can enter this Select statement
manually, or you can use the CommandText designer to construct the statement, along with Update,
Insert, and Delete statements, using a simple point-and-click mechanism. Using this method, once you
have a connection to a live data source, you will be able to see the names of tables and columns in the
CommandText designer. You can pick from listboxes to build the statement. Also, if you create your
BdpDataAdapter using the Data Explorer and a live connection to a data source, a boilerplate Select
statement is created for you in the form select * from tablename. You can use this statement to
return all rows from the named data source, or you can modify the statement prior to generating the
DataSet.
This populates the Tables and Columns list boxes with data from the database.
3. Select each column that you want to appear in your SQL statements.
As you select the column names, they appear in the SQL text box.
4. Select the check box next to each statement type you want to generate.
211
Using the Data Adapter Designer
The Data Adapter contains, at a minimum, a SQL Select statement of the SELECT command property.
You can enter this statement yourself, or using the Data Adapter designer you can construct the Select,
along with the Update, Insert, and Delete statements. The BdpCommandBuilder constructs the Update,
Insert, and Delete statements based on the tables and columns you have selected. The Data
Adapter designer uses a live connection to retrieve metadata from which you can build the appropriate
SQL statements for manipulating the data you want to move from a DataSet back into your database.
This populates the Tables and Columns list boxes with data from the database.
3. Select each column that you want to appear in your SQL statements.
4. Select the check box next to each statement type you want to generate.
6. Edit the generated text if desired, or reselect different columns and click Generate SQL again.
7. Click OK.
Note: Command components are automatically created as needed based on the
selections in the dialog.
212
Using the Connection Editor Designer
Each connection object can support multiple named connections. These connections can represent
connections to multiple databases and database types.
2. Click the component designer verb at the bottom of the Object Inspector to display the Connection
Editor dialog.
5. Enter a new name for the connection in the Connection Name text box.
6. Click OK.
8. Click OK.
To remove a connection
1. Select the connection type until it is highlighted.
2. Click Remove.
3. Click Yes.
To rename a connection
1. Right-click on the connection and choose Rename.
213
3. Click OK.
214
Using Standard DataSets
The standard DataSet provides an in-memory representation of one or more tables or views retrieved
from a connected data source. Because of the level of indirection used in coding the underlying data
structure, you are only able to see the column names from your data source at runtime. When you
generate a DataSet, it retrieves everything you specified in your Select statement in the Data Adapter
Configuration dialog. You can limit your columns by changing the Select statement and creating a new
DataSet.
To use DataSets
1. Generate a DataSet.
To generate a DataSet
1. From the Data Explorer, select a data source.
2. Drill down in the tree, then drag and drop the name of a table onto your Windows Form or Web Form.
This creates the BdpDataAdapter and BdpConnection for that data source and displays icons for
those objects in the Component Tray.
Note: You can also drag a data source only onto the form, rather than a table, but
in that case, Delphi 8 for .NET creates only a connection object for you. You must
still create and configure the BdpDataAdapter object explicitly.
4. Click the Configure Data Adapter... designer verb in the Designer Verb area at the bottom of the
Object Inspector.
215
5. If the SQL statement that is pre-filled on the dialog is acceptable, click the DataSet tab, otherwise,
modify the SQL statement, then click the DataSet tab.
Tip: You can accept the default name or change the name of the DataSet.
A DataSet icon appears in the Component Tray indicating that your DataSet has been created.
Note: By reviewing the code for the DataSet in the Code Editor, you can see that
the columns are defined as generic dataColumns, whose columnName properties
are assigned the value of the column name from the database table. This differs
from how a typed DataSet is constructed, wherein the object name is constructed
from the actual database column name, rather than assigned as a property value.
2. Drill down in the tree, then drag and drop the names of multiple tables, one at a time, onto your
Windows Form or Web Form.
This creates the BdpDataAdapter for each table and one BdpConnection for that data source and
displays icons for those objects in the Component Tray.
4. Click the Configure Data Adapter... designer verb in the Designer Verb area at the bottom of the
Object Inspector.
5. If the SQL statement that is pre-filled on the dialog is acceptable, click the DataSet tab, otherwise,
modify the SQL statement, then click the DataSet tab.
Tip: You can accept the default name or change the name of the DataSet.
A DataSet icon appears in the Component Tray indicating that your DataSet has been created.
216
8. Repeat the Data Adapter configuration for each of the other data adapters, but select Existing Data
Set on the DataSet tab when generating the DataSets for all data adapters except the first one you
configure.
This generates a DataTable for each data adapter and stores them all in one DataSet.
Note: It is also possible to generate multiple DataSets, either one for each data
adapter, or combinations of DataTables.
3. In the Object Inspector, in the Tables property, click the ellipsis (...) button.
This displays the Tables Collection Editor. If you have set all of the data adapters' Active properties
to True, the Tables Collection Editor will contain one member for each DataTable stored in the
corresponding DataSet.
5. In the Primary Key field in the Table Properties, click on the DataColumn[] entry to display a pop-
up list of column names.
6. Click the gray checkbox next to the column name of the column or columns that comprise the
Primary Key.
7. Define Column properties and Constraints for your Primary Key columns.
This displays the Columns Collection Editor for the selected column.
217
3. Repeat the process for each column.
This displays the Constraints Collection Editor for the selected column.
3. If you selected Unique Constraint, the Unique Constraint dialog appears. Select one or more of the
displayed column names. You can also select the Primary Key checkbox if you want to set the
column as a primary key.
By setting the Unique Constraint on a column, you are enforcing the rule that all values in the column
must be unique. This is useful for columns that contain identification numbers, such as employee
numbers, social security numbers, part numbers, and so on.
Note: If you have already defined a primary-foreign key relationship between two
tables, you may not be able to set a column as a primary key, based on the fact
that it may already be set as the primary key, or based on a conflict with another
relationship.
4. If you selected Foreign Key Constraint, the Foreign Key Constraint dialog appears. Select the tables
you want to relate by choosing them from the Parent table and Child table drop down lists.
5. Click Key Columns to select the primary key column from the list.
6. Click Foreign Key Columns to select the foreign key column from the list.
Warning: The primary key and foreign key columns must have the same data type and
must contain unique values. Columns that can contain duplicates are not good choices
for primary or foreign keys. It is common to choose the same column name from each
table for your primary-foreign key relationship.
2. Click the ellipsis (...) button next to the Relations property in the Object Inspector.
218
This displays the blank Relations Collection Editor dialog.
3. Click Add.
4. From the Parent table and Child table dropdown lists, choose the tables you want to relate.
5. Click the Key Columns field to choose a Primary Key column from the list of column names from
the parent table.
6. Click the Foreign Key Columns field to choose a Foreign Key column from the list of column names
from the child table.
Note: If you have already performed this procedure while setting constraints for
your DataTables, you may find that all of the appropriate values are already
established.
Warning: The primary key and foreign key columns must have the same data type and
must contain unique values. Columns that can contain duplicates are not good choices
for primary or foreign keys. It is common to choose the same column name from each
table for your primary-foreign key relationship.
7. Click OK.
8. Repeat the process to define additional relations between the same DataTables.
219
Using Typed DataSets
Typed DataSets provide certain advantages over standard DataSets. For one thing, they are derived
from an XML hierarchy of the target database table. The XML file containing the DataSet description
allows the system to provide extensive code-completion capabilities not available when using standard
DataSets. Strong typing of DataSet methods, properties, and events allows compile-time type checking,
and can provide a performance improvement in some applications.
2. Drag and drop the name of the database table you want to use onto your form.
This displays a BdpConnection icon and a BdpDataAdapter icon in the Component Tray.
4. Click the Configure Data Adapter... designer verb in the Designer Verb area beneath the Object
Inspector.
6. Click OK.
Note: Do not create a DataSet by selecting the DataSet tab in the Configure Data
Adapter... dialog. That tab applies only to standard DataSets.
7. Click the Generate Typed Dataset... designer verb in the Designer Verb area beneath the Object
Inspector.
9. Click OK.
This creates an instance of the typed DataSet and displays an icon <DataSet Name>1 in the
Component Tray. For example, if your DataSet is DataSet1, the new instance will be named
dataSet11. You will also see that an XML .xsd file and a new program file appear in the Project
Manager under your project.
220
To modify how columns appear
1. After you have created a new typed DataSet, drop a DataGrid component onto your form.
2. Set the DataSource property to point to the typed DataSet and the DataMember property to point
to the target table.
Note: By default the item is created as a Text Box Column. You can also expand
the Add button and select the BoolColumn if you want a boolean.
8. Click the MappingName property, select the column you want to display in your grid, then change
any additional properties you want, including the header name that will appear as the column header
in the runtime grid.
9. Click OK twice.
Note: When you build and run the application, only the columns that you explicitly
defined by following the steps in this procedure appear.
2. Edit the XML file to reflect how you want the dataset to be structured.
You can change data types, names, and anything else about the structure.
3. If you have the program code file (<dataset>.pas) open in the Code Editor, close it now.
221
If you re-open the program code file, you will see that the file contains the changes you made to
the XML in the .xsd file.
http://www.changeme.now/DataSet1.xsd
to a relevant namespace.
4. If you have the program code file (<dataset>.pas) open in the Code Editor, close it now.
If you re-open the program code file, you will see that the InitClass() class now contains the new
namespace.
222
Debugging Applications
223
Adding a Watch
Add a watch to track the values of program variables or expressions as you step over or trace into code.
Each time program execution pauses, the debugger evaluates all the items listed in the Watch List
window and updates their displayed values.
You can organize watches into groups. When you add a watch group, a new tab is added to the Watch
List window and all watches associated with that group are shown on that tab. When a group tab is
displayed, only the watches in that group are evaluated during debugging. By grouping watches, you
can also prevent out-of-scope expressions from slowing down stepping.
To add a watch
1. Choose Run Add Watch to display the Watch Properties dialog box.
An expression consists of constants, variables, and values contained in data structures, combined
with language operators. Almost anything you can use as the right side of an assignment operator
can be used as a debugging expression, except for variables not accessible from the current
execution point.
3. Optionally, enter a name in the Group Name field to create the watch in a new group, or select a
group name from the list of previously defined groups.
4. Specify other options as needed (click Help on the Watch Properties dialog for a description of the
options). For example, you can request the debugger to evaluate the watch, even if doing so causes
function calls, by selecting the Allow Function Calls option.
5. Click OK.
224
Attaching to a Running Process
You can attach to a process that is running on your computer or on a remote computer. This is useful
for debugging a program that was not created with Delphi 8 for .NET.
2. If the process is running on a remote computer, enter the name the computer in the Remote
Machine field.
Note: The remote debug server must be running on the remote computer.
4. Click Attach.
Note: If you want the process to pause after you have attached to it, check the
Pause After Attach checkbox.
225
Setting and Modifying Breakpoints
Breakpoints pause program execution at a certain location or when a particular condition occurs. You
can set breakpoints in the Code Editor before and during a debugging session. During a debugging
session, any line of code that is eligible for a breakpoint is marked with a blue dot in the left gutter of
the Code Editor.
To set a breakpoint
1. Click the left gutter of the Code Editor next to the line of code where you want to pause execution.
2. To add a breakpoint and set options for the breakpoint, choose Run Add Breakpoint Source
Breakpoint to display the Add Source Breakpoint dialog box.
Tip: To widen the Code Editor gutter, choose Tools Options Editor Options
Display and increase the Gutter width option.
The following icons are used to represent breakpoints in the Code Editor gutter.
Icon Description
The breakpoint is invalid and enabled. The breakpoint is set at an invalid location, such as a comment, a
blank line, or invalid declaration.
The breakpoint is valid and disabled. The debugger is inactive.
The breakpoint is invalid and disabled. The breakpoint is set at an invalid location.
To modify a breakpoint
1. Right-click the breakpoint icon and choose Breakpoint Properties.
2. Set the options in the Source Breakpoint Properties dialog box to modify the breakpoint.
For example, you can set a condition, create a breakpoint group, or determine what action occurs
when execution reaches the breakpoint.
3. Click Help for more information about the options on the dialog box.
226
4. Click OK.
2. Enter a group name in the Group field, or select a name from the drop down list box to add the
breakpoint to an existing group.
3. Click OK.
2. To enable or disable all breakpoints, right-click a blank area (not on a breakpoint) in the Breakpoint
List window and choose Enable All or Disable All.
3. To enable or disable a breakpoint group, right-click a blank area (not on a breakpoint) in the
Breakpoint List window and choose Enable Group or Disable Group.
Disabling a breakpoint or breakpoint group prevents it from pausing execution, but retains the breakpoint
settings, so that you can enable it later.
2. In the Line number field, enter the line in the Code Editor where you want set the breakpoint.
To pre-fill the Line number field click a line in the Code Editor to pre-fill the Line number field prior
to opening the Add Source Breakpoint dialog box.
3. In the Condition field, enter a conditional expression to be evaluated each time this breakpoint is
encountered during program execution.
227
4. Click OK.
Conditional breakpoints are useful when you want to see how your program behaves when a variable
falls into a certain range or what happens when a particular flag is set.
If the conditional expression evaluates to true (or not zero), the debugger pauses the program at the
breakpoint location. If the expression evaluates to false (or zero), the debugger does not stop at the
breakpoint location.
Alternatively, right-click the breakpoint icon and choose Breakpoint Properties to display the Source
Breakpoint Properties dialog box.
2. Click Advanced to display additional options at the bottom the dialog box.
3. Check the actions that you want to occur when the breakpoint is encountered. For example, you
can specify an expression to be evaluated and write the result of the evaluation to the Event Log.
4. Click OK.
2. In the code sample window, select the appropriate language tab. For example, to change the
breakpoint color for Delphi 8 for .NET code, select the Delphi 8 for .NET tab.
3. Scroll the code sample window to display the execution and breakpoint icons in the left gutter of
the window.
4. Click anywhere on the execution point or breakpoint line that you want to change.
5. Use the Foreground Color and Background Color drop-down lists to change the colors associated
with the selected execution point or breakpoint.
228
6. Click OK.
229
Inspecting and Changing the Value of Data Elements
The Debug Inspector lets you inspect data elements by automatically formatting the type of data it is
displaying. The Debug Inspector is especially useful for examining compound data objects, such as
arrays and linked lists. Because you can inspect individual items displayed in the Debug Inspector, you
can “walk” through compound data objects by opening a Debug Inspector on a component of the
compound object.
Note: The Debug Inspector is only available when the process is stopped in the
debugger.
2. In the Inspect dialog box, type the expression you want to inspect.
Unlike watch expressions, the scope of a data element in the Debug Inspector is fixed at the time you
evaluate it. If you use the Inspect command from the Code Editor, the debugger uses the location of the
insertion point to determine the scope of the expression you are inspecting. This makes it possible to
inspect data elements that are not within the current scope of the execution point.
If you use Run Inspect from the menu, the data element is evaluated within the scope of the execution
point. If the execution point is in the scope of the expression you are inspecting, the value appears in
the Debug Inspector. If the execution point is outside the scope of the expression, the value is undefined
and the Debug Inspector becomes blank.
2. Click the ellipsis (…), or right-click the element and choose Change.
230
3. Type a new value, then click OK.
2. Click the Data tab to view strings, boolean values, and other values for such things as variable
name, expression, and owner.
Tip: If you want to drill down even deeper, to see the hexadecimal representation of a
string, for instance, double-click the string value in the Debug Inspector.
3. Click the Methods tab to view all of the methods that have executed up to this point in the code.
Tip: If you want to see the return type for any method, select the method and look at
the status bar of the Debug Inspector, where the syntax line for the method, including
the return type is displayed.
4. Click the Properties tab to view all of the properties for the active object, for instance, the form.
5. Click any property name to see its type displayed in the status bar of the Debug Inspector.
6. Click the question mark (?) icon to see the actual value for that property at this point of the execution
of the application.
231
Resolving internal errors
The error message, "Internal Error: X1234" indicates that the compiler has encountered a condition,
other than a syntax error, that it cannot successfully process.
Tip: Internal error numbers indicate the file and line number in the compiler where the
error occurred. This information may help Technical Support services track down the
problem. Be sure to jot down this information and include it with your internal error
description.
2. If you can undo or comment out the change and then recompile your application successfully, it is
possible that the programming construct that you introduced exposed a problem with the compiler.
If so, skip to "Review the code" below.
2. Close your project completely using FileClose All, then reopen your project, this will clear the unit
cache maintained in the IDE. Alternatively, you can close the IDE and restart.
3. Another option is to try and recompile your application using the ProjectBuild option so that the
compiler will regenerate all of your dcuils.
4. If the error is still present, exit the IDE and try to compile your application using the command line
version of the compiler (dccil.exe) from a command prompt. This will remove the unit caching of
the IDE from the picture and could help to resolve the problem.
Typically, most internal errors can be reproduced with only a few lines of code and frequently the
code involves syntax or constructs that are rather unusual or unexpected. If this is the case, try
232
modifying the code to do the same thing in a different way. For example, if you are typecasting a
value, try declaring a variable of the cast type and do an assignment first.
Examples
begin
if Integer(b) = 100 then...
end;
var
a: Integer;
begin
a := b;
if a = 100 then...
end;
Here is an example of unexpected code that you can correct to resolve the error:
var
A : Integer;
begin
{ Below the second cast of A to Int64 is unnecessary; removing it
can avoid the Internal Error. }
if Int64(Int64(A))=0 then
end;
2. In this case, the second cast of A to an Int64 is unnecessary and removing it corrects the error. If
the problem seems to be a "while...do" loop, try using a "for...do" loop instead. Although this does
not actually solve the problem, it may help you to continue work on your application.
If this resolves the problem, it does not mean that either "while" loops or "for" loops are broken but
more likely it means that the manner in which you wrote your code was unexpected.
3. Once you have identified the problem, we ask that you create the smallest possible test case that
still reproduces the error and submit it to Borland.
233
2. If it uses a nested function or procedure (a procedure/function contained within a procedure/
function) try "unnesting" them.
3. If it occurs on a typecast look for alternatives to typecasting like using a local variable of the type
you need.
4. If the problem occurs within a with statement try removing the with statement altogether.
2. If you've tried your code on the latest release of the compiler and it is still reproducible, create the
smallest possible test case that will still reproduce the error and submit it to Borland. If it is not
reproducible on the latest version, it is likely that the problem has already been fixed.
2. The key here is that you always want to be using the most up-to-date versions of your .dcuil
and .dcpil files. Otherwise, you may encounter internal errors that are easily avoidable.
234
Modifying Variable Expressions
After you have evaluated a variable or data structure item, you can modify its value. When you modify
a value through the debugger, the modification is effective for the program run only. Changes you make
through the Evaluate/Modify dialog box do not affect your source code or the compiled program. To
make your change permanent, you must modify your source code in the Code Editor, then recompile
your program.
To modify a component property, specify the property name, for example, this.
button1.Height Self.button1.Height.
The expression must evaluate to a result that is assignment-compatible with the variable you want
to assign it to. Typically, if the assignment would cause a compile or runtime error, it is not a legal
modification value.
You cannot undo a change to a variable after you choose Modify. To restore a value, however, you
can enter the previous value in the Expression box and modify the expression again.
Note: You can change individual variables or elements of arrays and data structures,
but you cannot change the contents of an entire array or data structure with a single
expression.
Warning: Modifying values (especially pointer values and array indexes), can have
undesirable effects because you can overwrite other variables and data structures. Use
caution whenever you modify program values from the debugger.
235
Preparing a Project for Debugging
To activate the integrated debugger
1. Choose Tools Options Debugger Options.
3. Click OK.
2. Select Directories/Conditionals.
4. Click OK.
The program database file (.pdb), which contains the debugging information for the application, will be
created in the source directory the next time you compile the project.
236
Debugging Remote Applications
Remote debugging lets you debug a Delphi 8 for .NET application running on a remote computer. Once
the remote debug server is running on the remote computer, you can use Delphi 8 for .NET to connect
to that computer and begin debugging.
Warning: The connection between Delphi 8 for .NET and the remote debug server is
a simple TCP/IP socket, with neither encryption nor authentication support. Therefore,
the remote debug server should not be run on a computer that can be accessed over
the network by untrusted clients.
2. Copy dbkwmc71.exe and dbkpro71.dll from the Delphi 8 for .NET \bin directory on your local
computer to the directory of your choice on the remote computer.
3. On the remote computer, register dbkpro71.dll by running the regsvr32.exe registration utility. For
example, on Windows XP, enter C:\Windows\System32\regsvr32.exe dbkpro71.dll at the command
prompt.
where:
hostname is an optional host name or TCP/IP address for binding to a particular host, for example,
somehost or 127.0.0.1. If you specify hostname, you must also specify :port.
port is an optional (required if hostname is specified) port number or standard protocol name, for
example, 8000 or ftp. If omitted, 64447 is used as the port number.
Examples:
237
dbkwmc71.exe
dbkwmc71.exe -listen 8000
dbkwmc71.exe -listen somehost:8000
dbkwmc71.exe -listen 127.0.0.1:8000
After the remote debug server is started, its icon appears in the Windows taskbar.
3. In the Remote Path field, enter the path for the application's executable file on the remote computer.
The remote debug server will use this path to find the executable, so specify a path relative to the
directory that contains dbkwmc71.exe.
4. In the Remote Host field, enter the host name or TCP/IP address of the remote computer.
If a port was specified when starting dbkwmc71.exe, enter a colon after the host name, followed by
the port, for example, somehost:8000 or 127.0.0.1:8000. Otherwise, the default port 64447 will be
used.
5. Check the Debug project on remote computer checkbox and click OK.
6. Choose Run Run to begin the remote debugging session. This will require more time than starting
a local debugging session.
8. Optionally, if you want to prevent other computers from connecting to the remote computer while
you are debugging, the remote debug server can be shut-down without affecting your connection.
See the following section.
9. When you are done debugging, choose Run Program Reset to end the debugging session and
terminate your connection to the remote computer.
238
To shut down the remote debug server
1. On the remote computer, in the Windows taskbar, right-click the Borland Remote Debugger
Listener icon.
Shutting down the remote debug server does not affect active debugging sessions.
239
Editing Code
240
Using Code Folding
Code folding lets you collapse (hide) and expand (show) your code to make it easier to navigate and
read.
Tip: To turn off code folding for the current edit session, press and hold Ctrl+Shift and
then K and then O. To collapse the nearest code block, press and hold Ctrl+Shift and
then K and E. To expand the nearest code block, press and hold Ctrl+Shift and then K
and U. To expand all code, press and hold Ctrl+Shift and then press K and A.
241
Customizing Code Editor
Borland Delphi 8 for .NET lets you customize your Code Editor by using the available settings to modify
keystroke mappings, fonts, margin widths, colors, syntax highlighting, and indentation styles.
3. Select any of the customization options to make modifications to the Code Editor.
242
Recording a Keystroke Macro
You can record a series of keystrokes as a macro while editing code. After you record a macro, you can
play it back to repeat the keystrokes during the current IDE session.
To record a macro
1. In the Code Editor, click the record macro button at the bottom of the code window to begin
recording.
3. When you have finished typing the keystroke sequence, click the stop recording button .
The macro is now available to use during the current IDE session.
To run a macro
1. In the Code Editor, position the cursor in the code where you want to run the macro.
243
Using Code Insight
Code Insight is a set of features in the Code Editor that provide code completion, display code parameter
lists, and tool tips for expressions and symbols.
4. Click OK.
3. On the Code Editor, type an object or class name followed by a dot (.) to display a list of types,
properties, methods, and events.
4. Select the one appropriate for the class and press ENTER.
244
3. In the Code Editor, type a method name and an open parenthesis to display the syntax for the
method's arguments.
2. Select Code Insight and enable the ToolTip expression evaluation checkbox.
3. On the Code Editor, point to any variable to display its current value while your program has paused
during debugging.
3. On the Code Editor, point to any identifier to display its declaration while editing your code.
245
Using Code Snippets
Code snippets are reusable code statements that are accessible from the Tool Palette. While using the
Code Editor, you can insert predefined code snippets into your code or add your own code snippets to
the Tool Palette.
2. Double-click the selected code snippet or drag and drop the code snippet onto the Code Editor to
include as part of your code.
3. Press and hold the ALT key and drag and drop the code onto the Tool Palette.
246
Modeling
247
Adding Columns to a Component
There may be situations in which you want to add columns to components at runtime, for instance, to
accommodate computed or derived values that have no corresponding designtime field in a class or in
an underlying data source. The ECO framework provides the means to add columns at designtime,
which will then appear in your component, such as a datagrid at runtime. This procedure assumes you
have read the Modeling Tools Overview.
2. Click the ellipsis (...) button on the Column field in the Object Inspector.
4. Click the ellipsis (...) button on the Expression field in the properties pane of the dialog.
5. Create your OCL expression by double-clicking objects in the right-hand textbox of the OCL
Expression Editor and adding elements.
For instance, double-click a class name, then double-click the allInstances expression to add it.
6. Click OK.
7. Click OK.
If you are using a datagrid that references the expression handle for which you created a new
column, the new column should appear in the designtime datagrid.
248
Building an ECO-Enabled User Interface
It is assumed that you have a model with two classes, with a master/detail relationship between them.
A common example of this relationship includes Customer and Order classes, where one Customer can
have zero or many Orders. The procedure demonstrates the creation of two grids: one that displays
records on the master side of the association, and a second grid that displays the associated detail
records. The grids will be linked so that the detail grid always shows the objects associated with the
current selection in the master grid. It is also assumed that you have configured your application’s ECO
Space with either an RDBMS persistence component, or an XML file persistence component. For more
information on using the integrated class diagramming tools, and on configuring the application’s ECO
Space, please refer to the links at the end of this document.
In the ECO framework, the term “handle” means a value. A handle could represent a single value, such
as the value of one attribute of an object. Or, it could represent a collection of values, such as a list of
objects returned by an OCL expression. Handles implement the System.ComponentModel.IListSource
interface, making them suitable for use as data sources in user interface components.
The ECO Enabled Windows Form wizard creates a basic Windows Form with two additional properties:
A RootHandle (default name rhRoot), and an ExpressionHandle (default name ehRoot). The form’s
constructor is written to take a reference to your application’s ECO Space as a parameter, and it includes
code to set the ECO Space property of the RootHandle.
2. Select the ExpressionHandle (ehRoot), and set its RootHandle property to rhRoot using the
Object Inspector.
3. While the ExpressionHandle is still selected, set its Expression property using the OCL expression
editor. Using the example above, you might enter an OCL expression that results in a collection of
Customer objects, such as Customer.allInstances.
4. Drag a DataGrid component from the Tool Palette onto the form.
If you are using an RDBMS as the persistence method, the DataGrid will display column headings that
correspond to the attributes you have defined on the Customer class. The column headings will not
display at designtime when persisting objects to an XML file, but they will appear at runtime.
249
Continuing with the Customer example, to populate the grid with a new instance of the Customer
class, execute code such as the following: Customer newCustomer = new Customer
(EcoSpace); Customer := Customer.Create(EcoSpace);
Where the EcoSpace parameter is the instance of your application’s ECO Space that is allocated in
your application’s main WinForm. The grid is automatically synchronized with the ECO Space; when
you create a new instance within the ECO Space, a new row is added to the grid.
Using the ECO framework it is quite simple to construct two grids that are linked together in a master/
detail relationship as described above. Linking grids together requires the use of a
CurrencyManagerHandle object, and a second ExpressionHandle object to retrieve the objects on the
detail side of the relationship.
The CurrencyManagerHandle manages the synchronization between the ExpressionHandle for the
master objects, and the ExpressionHandle for the detail objects. It is helpful to think of the
CurrencyManagerHandle object as sitting between the two ExpressionHandles:
2. Select the CurrencyManagerHandle component, and drag it to the ECO-enabled Windows Form.
3. Select the ExpressionHandle component, and drag it to the form. This ExpressionHandle will
manage the detail side of the relationship.
4. Select the CurrencyManagerHandle component on the designer, and set its RootHandle property
to the ExpressionHandle on the master side of the relationship.
6. Select the detail ExpressionHandle and set its RootHandle property to the CurrencyManagerHandle
object.
7. Set the Expression property of the detail ExpressionHandle to an OCL expression that completes
the expression on the master side of the relationship.
250
expression for the ExpressionHandle is simply orders. The complete OCL expression can be
thought of as Customer.allInstances.orders.
Finally, you can complete the user interface by adding a second DataGrid to display the detail objects.
251
Building Applications with the ECO Framework
Building an ECO-enabled application consists of a number of steps, each with its own set of procedures.
This topic presents a grand overview of the entire process from start to finish.
2. Create or edit your model using the integrated UML diagramming tools: The Model View Window,
integrated UML class diagram, Object Inspector, and Tool Palette.
3. Configure your application's ECO Space. The ECO Space will contain instances of the classes in
your model; it is a middle layer between your application's front-end, and the persistence layer.
4. Build a user interface for your application. You can connect data-aware .NET components to the
objects in your ECO Space through element handles such as ExpressionHandles. The element
handles implement the interfaces required in order to render their values in a data-aware
component. You can use the OCL Expression Editor to enrich the specification of your model by
adding invariant constraints and derived attributes.
252
Deploying an ECO-Enabled Application
To deploy an ECO application
1. Open your project and select View Project Manager to display the Project Manager window if it
is not already open.
2. Select the appropriate compiler settings in the Project Options dialog box.
Note: You must set the appropriate build settings on each project in your
application's project group.
3. Select Project Build <Project Name> where <Project Name> is the actual name of your project
to build your application.
The build targets for each project in your application's project group will be generated per their own
respective project settings. Referenced assemblies that have their Copy Local setting checked will
be copied to the output directory of the project that references them.
In addition to the other assemblies your project references, there are five ECO-specific assemblies that
must be deployed with all ECO applications (these assemblies are displayed under your project's
References node in the Project Manager window):
Borland.Eco.Core.dll
Borland.Eco.Handles.dll
Borland.Eco.Interfaces.dll
Borland.Eco.Ocl.ParserCore.dll
Borland.Eco.Persistence.dll
The Delphi 8 for .NET installer deploys these five assemblies into the .NET Global Assembly Cache
(GAC). The GAC cannot be viewed or manipulated directly however, and copies of these files are kept
with other shared assemblies in Delphi 8 for .NET's Common Files folder. The default path to this location
is \Program Files\Common Files\Borland Shared\BDS\Shared Assemblies\<version>, where <version>
is the version number of Delphi 8 for .NET that is installed on the development machine.
On the end-user's machine, you can deploy the ECO assemblies into the GAC, or you can choose to
deploy them into the application's installation directory. If you will be deploying multiple ECO applications
however, it's best to deploy them as shared assemblies.
253
Exporting a Code Visualization Diagram to an Image
You can export a code visualization diagram to an image and then open the image in any graphic viewer
that supports the Windows bitmap (.bmp) file format.
8. Click Save.
254
Importing and Exporting a Model Using XMI
Delphi 8 for .NET supports the XML Metadata Interchange (XMI), version 1.1. Please see the link to the
OMG website at the end of this document for more information on XMI or to download the complete
specification.
2. In Delphi 8 for .NET, start a new ECO application by selecting File New Other, and choosing
ECO Application from the New Items dialog box.
3. Open the Model View Window, and right-click on the top-level project node in the tree. Choose
Import Project from XMI.
4. In the XMI Import dialog box, click the Browse... button to navigate to the XMI file you exported in
step one.
Delphi 8 for .NET will generate ECO-enabled, Delphi source code for the model elements on the class
diagrams in the XMI file.
2. In the XMI Export dialog box, select the XMI version, and XMI encoding appropriate for the tool
you will ultimately use to open the model file.
3. Click the Browse... button to navigate to the destination folder and enter a target file name for the
exported file.
255
Using the Model View Window and Code Visualization
Diagram
Code Visualization allows you view and navigate the logical structure of your application, as opposed
to the file-centric view of the Project Manager.
The Model View window will open, showing the elements of your project in a tree view.
View or hide nested elements within a UML package, Click the plus sign (+) next to the element's icon to view
class, or interface... nested items, or click the minus sign (-) to hide nested
items.
View a Code Visualization diagram for a .NET
Expand the namespace icon ( ) and double-click the
namespace...
diagram icon ( ) in the Model View tree.
View a Code Visualization diagram for the entire project...
Expand the project icon ( ) in the Model View tree,
and double-click the diagram icon.
Open the source code editor on a specific item in the ModelRight-click on the item, and select Open Source from the
View tree... menu. Note that a .NET namespace can span multiple
source files. You can't open a source file for a
namespace directly from the Model View tree.
Open the Code Visualization diagram on a specific item inRight-click on the item, and select Show Element on
the Model View tree... Diagram from the menu.
The Code Visualization diagram has a set of functions that can help you view large models, show or
hide attributes, properties, etc., and to move from the graphical depiction on the diagram, directly to the
source code for that item.
Using the Code Visualization diagram
Rearrange items on the diagram... Click the item and drag it to a new location.
256
View or hide the attributes, operations, properties, and Click the plus sign (+) next to the category (attributes,
nested types for an item on the diagram... operations, etc.) you want to view. Click the minus sign (-)
to hide items of a particular category.
Perform an automatic layout of the items on the diagram...Right-click anywhere in the Code Visualization diagram
window and select Do Full Layout.
Print the diagram... Right-click anywhere in the Code Visualization diagram
window and select Print.
Open the source code editor on a specific item on the Right-click on the item, and select Open Source from the
Code Visualization diagram... menu.
Save the diagram as an image... Right-click anywhere in the Code Visualization diagram
and select Save Image from the menu.
257
Using the Model View Window and ECO Class
Diagram
To Display the Model View Window
1. Start a new ECO project using using the ECO Application wizard, or load an existing one.
Note: The class diagramming and source code generation tools are only available
in ECO applications. In other types of projects, you can use code visualization to
generate a static structure diagram from Delphi source code, but you cannot modify
the source code through the diagram.
The Model View window will open, showing the elements of your project in a tree view.
The Model View window allows you to view both code visualization and ECO-enabled diagrams.
All diagrams in the Model View tree are located within a container. Code visualization diagrams are
contained within .NET namespace nodes (denoted by the icon). ECO-enabled class diagrams
are contained within UML package nodes (denoted by the icon). To open any diagram, double-
click its node in the Model View tree.
There are two ways to add an element to an ECO class diagram: The Tool Palette, or the right-click
context menu. Context menus are available directly on the diagram surface, on elements already on the
diagram, and on nodes in the Model View tree.
2. Click on the diagram surface and drag a rectangle. The graphic representing the new element will
be drawn to the size of the rectangle when you release the mouse button.
Note: To nest a UML package within an existing package, click the mouse once within
the containing package symbol on the diagram (i.e. do not drag a rectangle). Similarly,
to create a class within an existing package, click the mouse once within the UML
package symbol.
258
To add an element using the context menu
1. Right-click the mouse on the element (class diagram surface, UML package, or class) that will
contain the new item.
2. Select either the Add New Item menuitem, or use the Add submenus to add the new element. The
Add New Item selection will open a tabbed dialog in which you can select the kind of element you
wish to add, and set the its name. Using the Add submenus will immediately add the new item to
its container, with a default name that you can change later.
The kinds of elements available to add depends completely on the kind of container you have selected.
For example, you can only add attributes and operations to classes. Similarly, you can only add classes,
packages, and notes to an existing UML package.
Note: When you add an element directly to the diagram surface (as opposed to an
existing package or class on the diagram), it will be contained within the UML package
that owns the diagram.
2. Move the mouse cursor over one of the classes to participate in the association. The class will be
highlighted with a rectangle.
3. Click the mouse within the highlighted class and then move the mouse to the class at the other end
of the association.
4. Again, the class will be highlighted with a rectangle. Click the mouse over the highlighted class.
An association will be drawn between the two classes. Select the association, and use the Object
Inspector to set its properties. Association end properties such as the name and multiplicity are also
configured using the Object Inspector.
2. Right-click the mouse and choose either the Add New Item menuitem, or use the Add submenus
as described above.
259
3. Type the name of the attribute or operation. Use standard UML syntax to set the visibility, name
and type (for attributes), or parameters and return type (for operations). The UML syntax for
declaring attributes and operations is:
• Attributes: {visibility}attrName:type[=expression]
• Operations: {visibility}opName(param1:type, param2:type, ...):returnType
• + : Public visibility
• - : Private visibility
• # : Protected visibility
When declaring attributes you can include an optional expression, which will become the initial
value of the attribute. For example, the declaration:+myAttribute:int=17 declares a public
attribute named myAttribute, of type int, which will have an initial value of 17.
An attribute can be made to be "derived" by prefixing its declaration string with a slash "/", or by
selecting it and changing the Derived property to true in the Object Inspector. When you create
a derived attribute, you will also need to open the source code for that attribute (right click the
attribute and select the Open Source menuitem), and remove the [EcoAutoMaintained] .NET
attribute. Otherwise, any code you write in the attribute's get/set methods will be overwritten when
the IDE regenerates source code.
Note: Like all elements on the class diagram, attributes and operations can be
individually selected and then configured using the Object Inspector.
260
Using the ECO Space Designer
An ECO Space is a container for the runtime instances of the classes in your model. The ECO Space
designer lets you select UML packages from your model, choose the persistence mechanism for objects,
create or evolve the database schema, and perform designtime validation of the model.
You cannot work directly with the class Borland.Eco.Handles.EcoSpace. Instead, the IDE automatically
creates a subclass of the EcoSpace class for you, when you create a new ECO application. If you have
imported a model from another tool, such as Bold for Delphi or Together Control Center, you can add
an ECO Space to your project using the ECO Space Wizard in the New Items dialog box.
Your application's ECO Space is implemented in one source file. The default source file name is
EcoSpace.pas. To open the ECO Space designer and begin work, double-click the source code file in
the Project Manager window, and then click the Design tab. This document first describes the basic
procedure for configuring an ECO Space. Each step is then explained in more detail in the following
sections.
Warning: You must rebuild your application prior to using the ECO Space designer.
The ECO framework makes extensive use of .NET custom attributes, and building your
application ensures that the designer is working with the correct assembly metadata.
2. Choose a persistence method, either an RDBMS, or XML file. Here you will add the required
persistence components to the ECO Space, and then configure the ECO Space to use the chosen
persistence method by setting properties in the Object Inspector.
3. Validate the Model. This will cause the IDE to perform a number of checks to make sure the model
is well-formed. For example, OCL expressions are checked to make sure they are valid.
4. If you are using an RDBMS, create an empty database. The exact procedure will vary depending
on your database vendor. This procedure will use an Interbase database as an example.
5. Add a connection handle component to the ECO Space and configure its connection string.
6. If you are using an RDBMS and you are starting from scratch, create the initial database schema
by clicking on the Create Database Schema button. Otherwise, if you have made changes to the
model, or you want to add or remove a UML package, you can use the Evolve Db button to update
the existing database schema.
261
Note: When a class or attribute is deleted and the database schema evolved, the
corresponding columns are removed from the database and the data is lost. The IDE
will warn you if this is the case, giving you a chance to cancel the operation.
2. A full list of available UML packages is shown in the Available Packages list box. UML packages
that are already managed in the ECO Space are shown in the Selected Packages list box.
3. To add a single UML package, select it in the list, and click the '<' (left arrow) button. To add all
available packages, click the '<<' (double left arrow) button.
4. To remove a single package from the ECO Space, select the package in the Selected Packages
list, and click the '>' (right arrow) button. To remove all selected packages from the ECO space,
click the '>>' button.
Tip: It is often useful to store your objects in an XML file during initial development and
prototyping, and then switch to a relational database as your model becomes more
stable.
2. Drag the appropriate persistence mapper from the Tool Palette to the ECO Space designer surface.
3. Click the mouse on an empty portion of the ECO Space designer surface, so that the Object
Inspector is showing the properties of the ECO Space.
4. Set the PersistenceMapper property to the persistence mapper you created in step 2.
262
that is required is to select the component on the ECO Space designer, and set its
FileName property.
2. Login to the server where you want the new database to be created. Under the Databases node
you will see a list of databases that reside on that server.
4. Type the path and file name of the database. Customize any database parameters you wish to
change, and click OK.
The new database will be displayed under the Databases node in the IBConsole window.
Next, you must go back to Delphi 8 for .NET and add a connection handle, and then connect it to the
persistence mapper component.
Note: If you are using a SQL Server persistence handle, scroll to the Data
components category.
2. Drag a BdpConnection (or SqlConnection) component onto the ECO Space designer surface.
4. Set the Connection property of the persistence mapper component to the connection handle you
created in step 2.
5. Set the default vendor-specific configuration settings of the persistence mapper. Do this by right-
clicking on the persistence mapper component, and selecting the appropriate item from the context
menu. For example, to set the default settings for an Interbase database, select Interbase|dialect
3 setup from the context menu.
6. Right-click the connection handle component, and select Connection Editor from the context menu.
The ConnectionString will vary depending on the database vendor. For an Interbase database, you
will need to edit the connection string to reflect the correct path to the database file. Default, vendor-
263
specific connection strings are available both from the Connections Editor dialog, and from the
ConnectionString property's drop-down list in the Object Inspector.
- or -
2. If you are working with an existing ECO application and you have made changes to the model, click
the Evolve Database button ( ) on the designer.
Note: During creation or evolution of the database schema, the ECO tab in the message
pane will display status messages and results of the operation.
264
Using the ECO Wizards
This topic describes each ECO Wizard available in the New Items dialog box, and how each wizard is
used during the creation and maintenance of an ECO-enabled application.
ECO Wizards
1. Create a new ECO application from scratch.
3. Double-click the ECO Application icon. The New Application dialog box will appear.
4. Type the name of your project, and use the Browse ('...') button to navigate to the folder where you
want the project files to reside.
5. Use the subsequent Save As dialogs to name your project files and save initial copies in the project
folder.
The ECO Application Wizard generates a new project containing the following files:
CoreClasses.pas Contains the source code for the UML packages, interfaces, classes and their
associations, and all other types in your model.
EcoSpace.pas Contains source code for the subclass of Borland.Eco.Handles.EcoSpace.
265
WinForm.pas Contains source code for the application's main form. The main form for an ECO
application provides a property that holds an instance of the application's ECO
Space. The form also contains code to automatically allocate the ECO Space.
Borland.Eco.Core.dll These are the .NET assemblies required by all ECO applications. The ECO
Application Wizard automatically adds references to these assemblies, and they
Borland.Eco.Handles.dll must be distributed with your application along with all other referenced assemblies.
Borland.Eco.Interfaces.dll
Borland.Eco.Ocl.ParserCore.dll
Borland.Eco.Persistence.dll
The ECO Enabled Windows Form Wizard generates a new subclass of System.Windows.
Forms.Form. The new class provides a constructor that is passed an instance of the application's ECO
Space. The constructor then uses this instance to initialize the form's ECO Space property. An ECO
enabled form also provides fields to hold a root handle, an expression handle, and code to connect the
root handle to the ECO Space.
Note: The default name convention for the new class and source code file is
EcoWinFormX.pas, where X is a progressively increasing number. You can change the
default name of the class in the Source Code Editor. The name of the source code file
can be changed in the Project Manager window.
The ECO UML Package Wizard generates a source code file that contains the declarations necessary
for the new UML package. You can populate the UML package with new classes, and draw the
relationships between them on the class diagramming surface. As you design the classes contained in
a particular UML package, the IDE generates source code into the appropriate source code file. The
266
generated source code will be adorned with the necessary ECO attributes to support the services
provided by the ECO framework.
Note: The default name convention for the new UML package is
CoreClassesXPackage, where X is a progressively increasing number. You can change
the default UML package name in the Source Code Editor. You can change the source
code file name in the Project Manager window.
The ECO Space Wizard will generate a source code file that contains a new subclass of the Borland.
Eco.Handles.EcoSpace class.
267
Using the OCL Expression Editor
You can use the OCL Expression Editor to create the OCL expressions that define business logic for
your model and the application that is derived from the model. The OCL Expression Editor is available
from the Object Inspector, when you have created a new ExpressionHandle on your Windows Form.
4. Name the class and add any attributes or operations you want.
6. From the Tools Palette, drag-and-drop an ExpressionHandle onto the Windows Form.
8. Click the EcoSpaceType field in the Object Inspector and set the value to the name of an ECO
Space.
9. Select the ExpressionHandle and double-click the Expression field in the Object Inspector.
Note: A list of valid objects and expressions appears in the right-hand pane, from
which you can choose to build a complex expression. These change based on the
context of what you add to the textbox on the left side of the dialog. If the list does
not appear, make sure that the EcoSpaceType property for the root handle is set
to a valid ECO Space.
2. If you want to view the data types for the attributes of the class whose name you typed, select the
Show Types checkbox.
268
If you are referencing subclasses with your expression handles, they will display the attributes from
their abstract base class, as well as any attributes of their own.
3. Double-click an expression element from the right-hand list, to append it to the entry in the
expression textbox.
5. If you add an element that has tokens, for instance, for data types, you can replace those tokens
with actual values in the expression textbox.
Note: As you add items to the expression, ECO automatically parses the expression
to make sure it is valid. Keep in mind that the expression may be valid without being
logically correct.
3. In the properties list on the right-hand side of the dialog, click the ellipsis (...) button next to the
Expression field.
4. Construct your expression by double-clicking elements from the right-hand pane, until you are
satisfied with the results.
5. Click OK.
At runtime, this adds a new column to any data grid component that is linked to the expression
handle.
269
Using the Overview Window
Sizable, real-world models will probably not fit entirely within the diagram window. To help you view the
diagram, you can use the Overview window.
A miniature view of the entire diagram is displayed in its own sizable window. A smaller rectangle
within the Overview window contains that portion of the model that is currently displayed on the
diagram.
2. Click and drag the floating rectangle within the Overview window to dynamically scroll to the portion
of the diagram you want to view.
Tip: The Overview window has a sizing grip in the upper left corner. Click and drag the
sizing grip to resize the Overview window. As you resize the window, its contents are
scaled to fit the current size. Resizing is useful when the model is large enough that
you cannot read the text on the miniature representation in the Overview window.
270
Source Control
271
Adding Files to the Source Control Project
You must first add a file to your Delphi 8 for .NET project, after the project has already been committed
to the source control repository. You can then commit both the project file and your local working files
to the source control repository.
This displays the Add Files dialog box, with a list of the files included in your project that can be
added to the repository.
4. Click OK.
5. Write a comment. If you want to apply the same comment to all of the files, check the Apply same
comment to all check box. If you leave this unchecked, the Comments dialog displays once for each
file you are adding.
6. Click OK.
Tip: You can select or deselect all of the listed files at once by clicking the Check
All or Uncheck All buttons
272
Checking In Files
When you want to update the repository image with your changed files, you can do so with the check
in operation. While this puts your file into the repository and causes the source control system to version
the file, you need to commit your changes if you want to permanently update the repository image.
2. Select the check boxes next to the filenames of the files you want to check in.
3. If you want also to keep your files checked out, select the Keep checked out check box.
4. Click OK.
5. Write a comment. If you want to apply the same comment to all of the files, check the Apply same
comment to all check box. If you leave this unchecked, the Comments dialog displays once for each
file you are checking in.
6. Click OK.
Tip: You can select or deselect all of your files at once by clicking the Check All or
Uncheck All buttons, respectively.
273
Checking Out Files
When you check files out of the source control system using the Delphi 8 for .NET SCM feature, the
product performs a check out operation and a synchronization at the same time.
2. Select the checkboxes next to the files you want to check out.
3. Click OK.
4. If the check out operation encounters unsynchronized changes between files you already have on
your working system and those that you are checking out, resolve the conflicts that appear in the
Synchronization dialog box.
5. Click OK.
6. Write a comment. If you want to apply the same comment to all of the files, check the Apply same
comment to all check box. If you leave this unchecked, the Comments dialog displays once for each
file you are checking out.
7. Click OK.
Tip: You can select or deselect all of your files at the same time, by clicking the Check
All or Uncheck All buttons, respectively.
274
Using the Commit Browser
The Delphi 8 for .NETCommit Browser provides the capability to browse, select, and commit multiple
files or entire branches from your project to the source control system repository or database. The
Commit Browser provides standard options such as add, remove, check out, check in, undo checkout,
history view, and version differencing.
To commit a file
1. Choose the Commits tab to display a list of all potential commit candidate files.
2. Check the Trim File Path check box to limit the displayed name to the file name only.
3. For each file listed, select the Commit action from the Action drop-down list box.
When you select an action for a file, the Individual Comment tab is activated.
2. From the Action drop list, choose the action you want to perform for each listed file.
Note: If you do not want a file to be affected by any actions, choose the No
activity action.
3. Click Commit.
275
3. If you want the summary comment to override any existing individual comments, check the Use
Summary Comment check box.
4. If you want to apply the comment to multiple, selected files, select the Use Summary Comment
checkbox. By default, Delphi 8 for .NET inserts your summary comment in front of any individual
comment already existing for the file.
2. Add a comment to the Individual Comment window for each selected file.
3. Check the Use Individual Comment check box to override any summary comments that might be
added already.
2. The source code of the selected file is displayed in the lower pane.
2. The source code of the remote copy of the selected file is displayed in the lower pane.
To view history
1. Click the Diff. and History tab in the lower pane of the Commit Browser.
2. Click Show History... to display a report of the history of changes made to the files. This report
contains timestamps of check ins and check outs as well as comments.
276
To view conflicts
1. Click the Diff. and History tab in the lower pane of the Commit Browser.
2. Click Show Difference... to display a report that lists all conflicts between the selected local and
remote sources.
3. Review the conflicts that are displayed in the pane. They might be colored differently, or they might
be marked with a conflict tag, depending on the source control system you are currently using.
Note: If there are no conflicts, the system displays a confirmation alert to that effect.
4. When you have resolved the conflicts in your files, initiate the Commit Browser again and recommit
the files.
277
Configuring Source Control Providers
The Delphi 8 for .NET source control functionality automatically detects your installed source control
provider, assuming it has an SCC API integration component. For example, Borland StarTeam provides
a separately installable StarTeam SCC Integrator, downloadable from the Borland website. If you are
using one of the Windows-enabled CVS products, you might need to download an integrator, such as
the Jalindi Igloo software. Once that software is installed, Delphi 8 for .NET detects its presence. You
can configure the provider by supplying a valid source control system username.
If your provider is installed and includes the SCC API integration, its name appears in the Source
Code Control Providers drop-down list box.
3. If you have multiple providers installed, choose the provider you want to use from the Source Code
Control Providers drop-down list box.
4. Enter the valid source control system user ID in the User Name text box.
5. Click OK.
278
Connecting to the Source Control Repository
You need to connect to a source control system repository, generally on a remote server, before you
can begin managing your source files within the system. If you operate in a very small shop, you might
use a repository that is installed on your local system. Like any database system, you must connect to
the repository before you can view or update its contents. Whenever you attempt one of the Team
operations, you are prompted by Delphi 8 for .NET to log in to the source control system, if you are not
already logged in. For the purpose of describing this process, the following procedure starts with pulling
a project from the repository. However, many of the Team menu commands initiate this process.
To connect to a repository
1. Choose Team Pull Project from Source Control.
If you are not already connected to the source control system, Delphi 8 for .NET displays the
connection dialog box.
4. Enter the repository name in the Database textbox, or click the Browse... button to locate the
repository on your network.
5. Click OK to connect.
279
Placing a Project into Source Control
To place a project into source control
1. Choose Team Place Project into Source Control... to start the wizard.
2. On the first page of the wizard, choose the source control system from which you want to pull the
project. If you have only one source control system, it is listed as the default.
3. Click Next.
If you have not already logged in to your source control system, this displays the connection dialog
for your particular source control system. For more information on how to log on, see the
subtask To select a file if you are not yet logged on to the source control system.
2. When you have located the project and it appears in the text box of this wizard page, click Place.
This pulls the project and places it into your target directory. The results of the operation are
displayed in the Place project Wizard: Status page dialog.
If you enter the correct log on information, the Open Existing Project dialog is displayed with the
available projects displayed.
280
5. Select a project from the list of those displayed in the Project: list box.
Note: Your system may call this a view, a module, a project, or may use some
other terminology to refer to the basic project unit.
2. Select a project from the list of those displayed in the Project: list box.
4. Click OK.
Note: If your system supports the notion of nested branches or folders, you might
be presented with another dialog box, from which you can select the target branch.
281
Pulling a Project from Source Control
You run the Pull Project wizard to retrieve a project from the source control repository into your local
working directory. You can perform this task after you have connected to the source control system
repository, or the wizard prompts you to connect, if it does not detect a connection.
To pull a project
1. Choose Team Pull Project from Source Control... to start the wizard.
2. On the first page of the wizard, choose the source control system from which you want to pull the
project. If you have only one source control system, it is listed as the default.
3. Click Next.
If you enter a new directory name into the field, the wizard creates the new directory for you.
3. Click Next.
4. Click Pull to pull the project from the repository into your target directory.
The wizard displays the status of the operation in a separate Pull project Wizard: Status page dialog
box.
If you have not already logged in to your source control system, this displays the connection dialog
for your particular source control system. For more information on how to log on, see the
subtask To select a file if you are not yet logged on to the source control system.
282
2. When you have located the project and it appears in the text box of this wizard page, click Pull.
This pulls the project and places it into your target directory. The results of the operation are
displayed in the Pull project Wizard: Status page dialog.
If you enter the correct log on information, the Open Existing Project dialog is displayed with the
available projects displayed.
5. Select a project from the list of those displayed in the Project: list box.
2. Select a project from the list of those displayed in the Project: list box.
Note: Your system may call this a view, a module, a project, or may use some
other terminology to refer to the basic project unit.
4. Click OK.
283
Note: If your system supports the notion of nested branches or folders, you might
be presented with another dialog box, from which you can select the target branch.
284
Removing Files from Source Control
When you remove files from your source control system from within Delphi 8 for .NET, you delete the
selected files from the source control repository, from your local workspace, and from the Delphi 8 for .
NET project.
2. Select the check boxes next to the files you want to remove.
3. Click OK.
Delphi 8 for .NET prompts you to confirm that you want to remove the files.
Tip: You can check or uncheck the entire list of files by clicking the Check All or Uncheck
All buttons, respectively.
285
Undoing a Check Out Operation
If you undo the check out of selected files, you void all changes to those files.
Note: You must have Check Out access rights to use this command. Check with your
system administrator to find out more about your SCM privileges.
This displays the Undo Check Out dialog, which lists the files that you checked out.
2. Select the check boxes next to the files for which you want to undo the check out.
Tip: You can check or uncheck all of the files at once by clicking the Check All or
Uncheck All buttons, respectively.
3. Click OK.
Note: If you have left any check boxes unchecked, this does not check in the
corresponding files. It just means that you won't undo the check out of those files
and that you intend to retain any changes to those files. You must still perform a
check in on them at some point.
286
Running an SCC Application
In addition to performing basic source control operations from within Delphi 8 for .NET, you can run a
separate instance of your source control application in its own process from within Delphi 8 for .NET.
This initiates a session of your source control system, assuming it supports SCC API.
3. Exit the session instance prior to shutting down Delphi 8 for .NET.
287
VCL for .NET
288
Building VCL Forms Applications With Graphics
Each of the procedures listed below builds a VCL Form application that uses graphics. Build one or
more of the examples. Then refer to Chapter 12, "Overview of Graphic Programming," in the Delphi 7
Developer's Guide for information on other graphics features that you can add to these basic VCL Form
applications.
3. Draw a polygon.
289
Building a VCL Forms MDI Application Without Using
a Wizard
The basic steps to create a new MDI application with a child
window without using a wizard are
1. Create a main window form (MDI parent window).
3. Have the main window create the child window under user control.
3. Enter a more descriptive name such as frMain for the Name property.
4. Save the unit file with a more descriptive name, such as uMain.pas.
290
3. Enter a more descriptive name such as frChild for the Name property.
2. Select frChild from Auto-create forms: list and click the right-angle button to move it to the Available
forms: list and click OK.
3. Select the frMain form to activate it; then switch to the Code view.
5. Scroll to the private declarations section and enter this procedure declaration:
The Code editor displays with the cursor in the TfrMain.FormClose event handler block.
291
Action := caFree;
The Menu designer (frMain.MainMenu1) displays with the first blank menu item highlighted.
3. In the Object Inspector on the Properties tab, enter mnFile for the Name property and &File for the
Caption property; then press ENTER.
In the Menu designer, File displays as the name of the first menu item.
A blank command field displays in the File group. Select the blank command.
5. In the Object Inspector, enter mnNewChild for the Name property and &New child for the Caption
property; then press ENTER.
In the Menu designer, New child displays as the name of the first file command, and a blank
command field displays just beneath New child.
7. In the Object Inspector, enter mnCloseAll for the Name property and &Close All for the Caption
property; then press ENTER.
In the Menu designer, Close All displays as the name of the second file command.
To add event handlers for the New child and Close All
commands
1. If necessary, open the Menu designer and select New child.
2. In the Object Inspector, double-click the OnClick event on the Events tab.
The Code editor displays with the cursor in the TfrMain.mnNewChildClick event handler block.
292
CreateChildForm('Child '+IntToStr(MDIChildCount+1));
5. In the Object Inspector, double-click the OnClick event on the Events tab.
The Code editor displays with the cursor in the TfmMain.mnCloseAllClick event handler block.
7. Just before the code block in the event handler, declare the local variable i.
The first two lines of the event handler code should appear as shown here when you are done:
2. In the Object Inspector, double-click the OnClose event on the Events tab.
The Code editor displays with the cursor in the TfrChild.FormClose event handler block.
Action := caFree;
293
A child window displays with each New child command.
294
Building a VCL Forms MDI Application Using a Wizard
The Janeva MDI application wizard automatically creates a project that includes the basic files for an
MDI application. In addition to the Main source file, the wizard creates unit files for child and about box
windows, along with the supporting forms files and resources.
3. Navigate to the folder in which you want to store the files for the project.
4. Click OK.
6. Try commands that are automatically set up by the MDI Application wizard.
295
Building a VCL Forms SDI Application
To create a new SDI application
1. Choose File New Other to display the New Items dialog.
3. Click OK.
296
Building a Network Application with Socket
Components
Type introductory text only if necessary, otherwise, start the procedure. Avoid including conceptual
information. For more complex procedures requiring multiple stages, use the initial taskList to outline
the required steps, then use subtasks for each subsequent stage.
2. Do that. Use the appropriate XML tag for IDE elements such as dialog box names and fields.
3. Do this. Save this file in the HowTo directory, using the [TBD] naming convention.
2. Do that.
3. Do the other.
2. Do that.
3. Do the other.
297
Tip: Use a tip to provide optional information, such as a shortcut key.
Note: Use a note to provide important information that might prevent the procedure
from working.
Warning: Use a warning to indicate a serious danger, such as loss of data.
298
Building a VCL Forms dbExpress.NET Database
Application
The following procedure describes how to build a dbExpress database application.
Building a VCL Forms dbExpress.NET application consists of the following major steps:
1. Set up the database connection.
2. Set up the unidirectional dataset.
3. Set up the data provider, client dataset, and data source.
4. Connect a DataGrid to the connection components.
5. Run the application.
2. In the New Items dialog, select Delphi for .NET Projects; then double-click Janeva Application.
3. From the General category of the Tool palette, place a TSQLConnection component on the form.
6. In the Connections Setting box, specify the path to the InterBase database file called
employee.gdb in the Database field.
7. Accept the value in the User_Name field (sysdba) and Password field (masterkey).
8. To test the connection, click the button with the checkmark on it (just above the Connection
Name list).
Note: By default, you are prompted to log in. Use the masterkey password.
A message displays indicating that the connection is successful.
299
To set up the unidirectional dataset
1. From the General category of the Tool Palette, place a TSQLDataSet component at the top of the
form.
2. In the Object Inspector, select the SQLConnection property drop-down list. Set it to
SQLConnection1.
3. Set the CommandText property to an SQL command, for example, Select * from SALES.
For the SQL command, you can either type a Select statement in the Object Inspector or click the
ellipsis to the right of CommandText to display the CommandText Editor where you can build your
own query statement.
Tip: If you need additional help while using the CommandText Editor, click the Help
button.
4. In the Object Inspector, set the Active property to True to open the dataset.
2. In the Object Inspector, select the DataSet property drop-down list. Set it to SQLDataSet1.
3. Set the Active property to True to allow data to be passed to your application.
A data source connects the client dataset with data-aware controls. Each data-aware control must
be associated with a data source component to have data to display and manipulate. Similarly, all
datasets must be associated with a data source component for their data to be displayed and
manipulated in data-aware controls on the form.
300
To add the data source
1. From the Data Access category of the Tool palette, place a TDataSource component to the right
of the ClientDataSet on the form.
2. In the Object Inspector, select the DataSet property drop-down. Set it to ClientDataSet1.
2. In the Object Inspector, select the DataSource property drop-down. Set the data source to
DataSource1.
301
Building a VCL Forms ADO.NET Database Application
The following procedure describes how to build an ADO.NET database application.
2. In the New Items dialog, select Delphi for .NET Projects; then double-click Janeva Application.
3. From the ADO page of the Component palette, place an ADOConnection component on the form.
5. If necessary, select Use Connection String; then click the Build button to display the Link
Properties dialog.
6. On the Provider page of the dialog, select Microsoft Jet 4.0 OLE DB Provider; then click the Next
button to display the Connections page.
7. On the Connections page, click the ellipsis button to browse for the dbdemos.mdb database. The
default path to this database is C:\Program Files\Common Files\Borland Shared\Data.
8. Click Test Connection to confirm the connection. A dialog appears, indicating the status of the
connection.
9. Click OK to close the Data Link Properties dialog. Click OK to close the ConnectionString dialog.
302
2. In the Object Inspector, select the Connection property drop-down list. Set it to ADOConnection1.
3. Set the CommandText property to an SQL command, for example, Select * from orders.
You can either type the Select statement in the Object Inspector or click the ellipsis to the right of
CommandText to display the CommandText Editor where you can build your own query statement.
Tip: If you need additional help while using the CommandText Editor, click the Help
button.
You are prompted to log in. Use admin for the username and no password.
2. In the Object Inspector, select the DataSet property drop-down list. Set it to ADODataSet1.
3. Set the Active property to True to allow data to be passed to your application.
A data source connects the client dataset with data-aware controls. Each data-aware control must
be associated with a data source component to have data to display and manipulate. Similarly, all
datasets must be associated with a data source component for their data to be displayed and
manipulated in data-aware controls on the form.
303
2. In the Object Inspector, select the DataSet property drop-down. Set it to ClientDataSet1.
2. In the Object Inspector, select the DataSource property drop-down. Set the data source to
DataSource1.
3. SelectRun Run.
4. You are prompted to log in. Enter admin for the username and no password.
304
Creating Actions in a VCL Forms Application
Using Delphi 8 for .NET, the following procedure illustrates how to create actions using the ActionList
tool. It sets up a simple application and describes how to create an edit menu with cut and paste actions
that can be used to cut and paste to a memo.
2. In the New Items dialog, select Delphi for .NET Projects; then double-click Janeva Application.
2. From the Standard category of the Tool Palette, add a TMainMenu, TActionList, and TMemo
component to the form.
2. In the editor, select New Standard Action from the drop-down list to display the Standard Action
Classes dialog.
305
4. Select New Standard Action from the drop-down list to display the Standard Action Classes dialog.
To add the cut and paste actions to the edit category in the main
menu
1. Double-click MainMenu1 on the form.
The MainMenu1 editor displays with the first blank command category selected.
2. In the Object Inspector, enter Edit for the Caption property and press ENTER.
3. Click Edit to display a blank action just below it; then click the blank action to select it.
4. In the Object Inspector, select EditCut1 from the drop-down list of actions.
5. Expand the list of Action properties, enter Cut for the Caption property, and enter Edit for the
category; then press ENTER.
6. In the MainMenu1 editor, click the second blank action beneath Cut to select it.
7. In the Object Inspector, select EditPaste from the drop-down list of actions.
8. Expand the list of Action properties if necessary, enter Paste for the Caption property, and enter
Edit for the category; then press ENTER.
The application executes, displaying a form with the main menu bar and the Edit menu.
306
2. In the application, select text in the memo; then choose Edit Cut.
307
Building a VCL Forms "Hello world" Application
Though simple, the Windows Forms "Hello world" application demonstrates the essential steps for
creating a Janeva application. The application uses a VCL Form, a control, an event, and will display a
dialog in response to a user action.
2. In the New Items dialog, select Delphi for .NET Projects; then double-click Janeva Application.
4. From the Standard page of the Tool palette, place a Button component on the form.
2. In the Object Inspector, double-click the OnClick action on the Events tab.
The Code editor displays, with the cursor in the TForm1.Button1Click event handler block.
3. Place the cursor before the begin reserved word; then press return.
4. Insert the cursor on the new line created, and type the following variable declaration:
var s: string;
5. Insert the cursor within the code block, and type the following code:
308
s:= 'Hello world!';
ShowMessage(s);
2. Click Button1.
309
Using ActionManager to Create Actions in a VCL
Forms Application
Using Delphi 8 for .NET, the following procedure illustrates how to create actions using ActionManager.
It sets up a simple user interface with a text area, as would be appropriate for a text editing application,
and describes how to create a file menu item with a file open action.
Building the VCL application with ActionManager actions consists of the following major steps:
1. Create a main window.
2. Add a file open action to the ActionManager.
3. Create the main menu.
4. Add the action to the menu.
5. Build and run the application.
2. In the New Items dialog, select Delphi for .NET Projects; then double-click Janeva Application.
4. Select New Standard Action from the drop-down list to display the Standard Action Classes dialog.
310
Open... displays in the Actions: list box.
2. Open the Action Manager editor, and select the File category from the Categories: list box.
The application executes, displaying a form with the main menu bar and the File menu.
311
Building a VCL Forms Application
The following procedure illustrates the essential steps to building a VCL Forms application using Delphi
8 for .NET.
2. In the New Items dialog, select Delphi for .NET Projects; then double-click Janeva Application.
3. From the Tool palette, place components onto the form to create the user interface.
312
Building an Application with XML Components
This example creates a Janeva application that uses an XMLDocument component to display contents
in an XML file.
3. Place an XMLDocument component on the form, and associate it with the XML file.
<StockHoldings>
<Stock exchange="NASDAQ">
<name>Borland</name>
<price>10.375</price>
<symbol>BORL</symbol>
<shares>100</shares>
</Stock>
<Stock exchange="NYSE">
<name>MyCompany</name>
<price>8.75</price>
<symbol>MYCO</symbol>
313
<shares type="preferred">25</shares>
</Stock>
</StockHoldings>
2. Save the file to your local drive as an XML document. Give it a name such as stock.xml.
Note: In the browser, you can choose View Source to view the source in the
text editor file.
3. In the New Items dialog, select Delphi for .NET Projects; then double-click Janeva Application.
4. From the Internet page on the Component palette, place an XMLDocument component on the form.
5. In the Object Inspector, click the ellipse next to the FileName property, browse to the location of
the XML file you created, and open it.
2. From the Standard page on the Component palette, place two Button components on the form just
above Memo1.
3. In the Object Inspector with Button1 selected, enter Borland for the Caption property.
4. In the Object Inspector with Button2 selected, enter MyCompany for the Caption property.
314
To display child node contents in the XML file
1. In the Object Inspector with Button1 selected, double-click the OnClick event on the Events tab.
The Code displays with the cursor in the TForm1.Button1Click event handler block.
2. Enter the following code to display the stock price for the first child node when the Borland button
is clicked:
BorlandStock:=XMLDocument1.DocumentElement.ChildNodes[0];
Price:= BorlandStock.ChildNodes['price'].Text;
Memo1.Text := Price;
3. Add a var section just above the code block in the event handler, and enter the following local
variable declarations:
var
BorlandStock: IXMLNode;
Price: string;
4. In the Object Inspector with Button2 selected, double-click the OnClick event on the Events tab.
The Code displays with the cursor in the TForm1.Button2Click event handler block.
5. Enter the following code to display the stock price for the second child node when the MyCompany
button is clicked:
MyCompany:=XMLDocument1.DocumentElement.ChildNodes[1];
Price:= MyCompany.ChildNodes['price'].Text;
Memo1.Text := Price;
6. Add a var section just above the code block in the event handler, and enter the following local
variable declarations:
var
MyCompany: IXMLNode;
Price: string;
315
The application form displays two buttons and a memo.
316
Displaying a Bitmap Image in a VCL Forms
Application
This procedure loads a bitmap image from a file and displays it to a VCL form.
1. Create a VCL form with a button control.
2. Provide a bitmap image.
3. Code the button's onClick event handler to load and display a bitmap image.
4. Build and run the application.
2. In the New Items dialog, select Delphi for .NET Projects; then double-click Janeva Application.
3. From the Standard category in the Tool palette, place a button component on the form.
2. Locate a bitmap image on your local drive, and copy it to your project directory.
The Code editor displays with the cursor in the TForm1.Button1Click event handler block.
2. Enter the following event handling code, replacing MyFile.bmp with the name of the bitmap image
in your project directory:
Rect := TRect.Create(0,0,100,100);
Bitmap := TBitmap.Create;
317
try
Bitmap.LoadFromFile('MyFile.bmp');
Form1.Canvas.Brush.Bitmap := Bitmap;
Form1.Canvas.FillRect(Rect);
finally
Form1.Canvas.Brush.Bitmap := nil;
Bitmap.Free;
end;
Tip: You can change the size of the rectangle to be displayed by adjusting the Rect
parameter values.
Bitmap : TBitmap;
Rect : TRect;
2. Click the button to display the image bitmap in a 100 x 100-pixel rectangle in the upper left corner
of the form.
318
Drawing a Polygon in a VCL Forms Application
This procedure draws a polygon in a VCL form.
1. Create a VCL form.
2. Code the form's OnPaint event handler to draw a polygon.
3. Build and run the application.
2. In the New Items dialog, select Delphi for .NET Projects; then double-click Janeva Application.
3. In the form view, click the form, if necessary, to display Form1 in the Object Inspector.
The Code editor displays with the cursor in the TForm1.FormPaint event handler block.
2. The applications executes, displaying a right triangle in the lower left half of the form.
319
Drawing Rectangles and Ellipses in a VCL Forms
Application
This procedure draws a rectangle and ellipse in a VCL form.
1. Create a VCL form.
2. Code the form's OnPaint event handler to draw a rectangle and ellipse.
3. Build and run the application.
2. In the New Items dialog, select Delphi for .NET Projects; then double-click Janeva Application.
3. Click the Design tab, if necessary, to display Form1 in the Object Inspector.
The Code editor displays with the cursor in the TForm1.FormPaint event handler block.
2. The applications executes, displaying a rectangle in the upper left quadrant, and an ellipse in the
same area of the form.
320
Drawing a Rounded Rectangle in a VCL Forms
Application
This procedure draws a rounded rectangle in a VCL form.
1. Create a VCL form.
2. Code the form's OnPaint event handler to draw a polygon.
3. Build and run the application.
2. In the New Items dialog, select Delphi for .NET Projects; then double-click Janeva Application.
3. In the form view, click the form, if necessary, to display Form1 in the Object Inspector.
The Code editor displays with the cursor in the TForm1.FormPaint event handler block.
2. The application executes, displaying a rounded rectangle in the upper left quadrant of the form.
321
Drawing Straight Lines In a VCL Forms Application
This procedure draws two diagonal straight lines on an image in a VCL form.
1. Create a VCL form.
2. Code the form's OnPaint event handler to draw the straight lines.
3. Build and run the application.
2. In the New Items dialog, select Delphi for .NET Projects; then double-click Janeva Application.
3. Click the Design tab, if necessary, to display Form1 in the Object Inspector.
The Code editor displays with the cursor in the TForm1.FormPaint event handler block.
with Canvas do
begin
MoveTo(0,0);
LineTo(ClientWidth, ClientHeight);
MoveTo(0, ClientHeight);
LineTo(ClientWidth, 0);
end;
2. The applications executes, displaying a form with two diagonal crossing lines.
Tip: To change the color of the pen to green, insert this statement following the first
MoveTo() statement in the event handler code: Pen.Color := clGreen;
322
Experiment using other canvas and pen object properties. See "Using the properties of
the Canvas object" in the Delphi 7 Developer's Guide.
323
Building a Multithreaded Application
These are the essential steps to building a VCL Forms multithreaded application with a thread object.
324
Writing Cleanup Code
To clean up after your thread finishes executing
1. Place your cleanup code in the OnTerminate event handler to centralize it.
2. Do not use any thread-local variables. OnTerminate is not run as part of your thread.
Note: You can safely access any objects from the OnTerminate handler.
325
Avoiding Simultaneous Thread Access to the Same
Memory
Use these basic techniques to prevent other threads from accessing the same memory as your thread:
Lock objects.
Use critical sections.
Use a multi-read exclusive-write synchronizer
To lock objects
1. For objects such as canvas that have a Lock method, call the Lock method, as necessary, to
prevent other objects from accessing the object, and call Unlock when locking is no longer required.
2. Call TThreadList.LockList to block threads from using the list object TThreadList, and call
TThreadList.UnlockList when locking is no longer required.
Note: You can safely make calls to TCanvas.Lock and TThreadList.LockList.
2. Call the Acquire method to lock out other threads while accessing global memory.
3. Call the Release method so other threads can access the memory by calling Acquire.
The following code has a global critical section variable LockXY that blocks access to the global
variables X and Y. To use X or Y, a thread must surround that use with calls to the critical section
such as shown here:
LockXY.Acquire;
try
Y := sin(X);
finally
LockXY.Release
end;
326
Warning: Critical sections only work if every thread uses them to access global
memory. Otherwise, problems of simultaneous access can occur.
2. Before any thread reads from the memory, it must call BeginRead.
5. At the completion of writing to the memory, the thread must call EndWrite.
Warning: The multi-read exclusive-write synchronizer only works if every thread uses
it to access the associated global memory. Otherwise, problems of simultaneous
access can occur.
327
Defining the Thread Object
To define the thread object
1. Choose File New Other.
4. Optionally check the Named Thread checkbox, and enter a name for the thread, for example,
MyThreadName.
Tip: Entering a name for Named Thread makes it easier to track the thread while
debugging.
5. Press OK.
The Code Editor displays the skeleton code for the thread object.
The code generated for the new unit will look like this if you named your thread class TMyThread.
unit Unit1;
interface
uses
Classes;
type
TMyThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
end;
implementation
Synchronize(UpdateCaption);
328
and UpdateCaption could look like,
procedure TMyThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }
{ TMyThread }
procedure TMyThread.Execute;
begin
{ Place thread code here }
end;
end.
Adding a name for the thread generates additional code for the unit. It includes the Windows unit,
adds the procedure SetName, and adds the record TThreadNameInfo. The name is assigned to
the ThreadNameInfo.FName field in the record, as shown here:
unit Unit1;
interface
uses
Classes {$IFDEF MSWINDOWS} , Windows {$ENDIF};
type
TMyThread = class(TThread)
private
procedure SetName;
protected
procedure Execute; override;
end;
implementation
Synchronize(UpdateCaption);
procedure TMyThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
329
end; }
{$IFDEF MSWINDOWS}
type
TThreadNameInfo = record
FType: LongWord; // must be 0x1000
FName: PChar; // pointer to name (in user address space)
FThreadID: LongWord; // thread ID (-1 indicates caller thread)
FFlags: LongWord; // reserved for future use, must be zero
end;
{$ENDIF}
{ TMyThread }
procedure TMyThread.SetName;
{$IFDEF MSWINDOWS}
var
ThreadNameInfo: TThreadNameInfo;
{$ENDIF}
begin
{$IFDEF MSWINDOWS}
ThreadNameInfo.FType := $1000;
ThreadNameInfo.FName := 'MyThreadName';
ThreadNameInfo.FThreadID := $FFFFFFFF;
ThreadNameInfo.FFlags := 0;
try
RaiseException( $406D1388, 0, sizeof(ThreadNameInfo) div sizeof
(LongWord), @ThreadNameInfo );
except
end;
{$ENDIF}
end;
procedure TMyThread.Execute;
begin
SetName;
{ Place thread code here }
end;
end.
330
Handling Exceptions
To handle exceptions in the thread function
1. Add a try...except block to the implementation of your Execute method.
procedure TMyThreadExecute;
begin
try
while not Terminated do
PerformSomeTask;
except
{do something with exceptions}
end;
end;
331
Using the Main VCL Thread
Using the main VCL thread consists of the following basic steps:
1. Create a separate routine to handle Windows messages received by components in your
application.
2. Call CheckSynchronize periodically.
3. Declare thread-local variables, as necessary, for exclusive use by your thread.
This is an example.
procedure TMyThread.PushTheButton
begin
Button1.Click;
end;
procedure TMyThread.Execute;
begin
...
Synchronize(PushThebutton);
...
end;
Synchronize waits for the main thread to enter the message loop and then executes the passed
method.
Note: Because Synchronize uses a message loop, it does not work in console
applications. For console applications, use other mechanisms, such as critical
sections, to protect access to VCL objects.
To call CheckSynchronize
1. Call CheckSynchronize periodically within the main thread to enable background threads to
synchronize execution with the main thread.
332
2. To ensure the safety of making background thread calls, call CheckSynchronize when the
application is idle, for example, from an OnIdle event handler.
threadvar
x: integer;
Note: Use the threadvar section for global variables only. Do not use it for Pointer and
Function variables or types that use copy-on-write semantics, such as long strings.
333
Waiting for Threads
To wait for a thread to finish executing
1. Use the WaitFor method of the other thread.
2. Code your logic. For example, the following code waits for another thread to fill a thread list object
before accessing the objects in the list:
if ListFillingThread.WaitFor then
begin
with ThreadList1.LockList do
begin
for I := 0 to Count - 1 do
ProcessItem(Items[I];
end;
ThreadList1.UnlockList;
end;
2. When a thread completes an operation other threads are waiting for, have the thread call TEvent.
SetEvent.
The following example is an OnTerminate event handler that uses a global counter in a critical
section to keep track of the number of terminating threads. When Counter reaches 0, the handler
calls the SetEvent method to signal that all processes have terminated:
334
The main thread initializes Counter, launches the task threads, and waits for the signal that they
are all done by calling the WaitFor method. WaitFor waits a specified time period for the signal
to be set and returns one of the values in the table below.
The following code example shows how the main thread launches the task threads and resumes
when they have completed.
Note: If you do not want to stop waiting for an event handler after a specified time period,
pass the WaitFor method a parameter value of INFINITE. Be careful when using
INFINITE, because your thread will hang if the anticipated signal is never received.
procedure TMyThread.Execute;
begin
while not Terminated do
PerformSomeTask;
end;
Value Meaning
wrSignaled The signal of the event was set.
wrTimeout The specified time elapsed without the signal being set.
wrAbandonedThe event object was destroyed before the timeout period elapsed.
wrError An error occurred while waiting.
335
Writing the Thread Function
The Execute method is your thread function. You can think of it as a program that is launched by your
application, except that it shares the same process space. Writing the thread function is a little trickier
than writing a separate program, because you must make sure that you do not overwrite memory that
is used by other processes in your application. On the other hand, because the thread shares the same
process space with other threads, you can use the shared memory to communicate between threads.
4. Handling exceptions.
336
Placing A Bitmap Image in a Control in a VCL Forms
Application
This procedure adds a bitmap image to a combo box in a VCL forms application.
1. Create a VCL form.
2. Place components on the form.
3. Set component properties in the Object Inspector.
4. Write event handlers for the component's drawing action.
5. Build and run the application.
2. In the New Items dialog, select Delphi for .NET Projects; then double-click Janeva Application.
4. From the Win32 category of the Tool palette, place an ImageList component on the form.
5. From the Standard category of the Tool palette, place a ComboBox on the form.
3. In the Object Inspector, click the ellipsis next to the Items property.
4. Enter a string you would like to associate with the bitmap image, for example, MyImage; then click
OK.
337
6. Click the Add button to display the Add Images dialog.
To locate an image, you can search for *.bmp images on your local drive. Select a very small image
such as an icon. Copy it to your project directory, and click Open.
2. In the Object Inspector, click the Events page, and double-click the OnDrawItem event.
The Code editor displays with cursor in the code block of the ComboBox1DrawItem event handler.
Combobox1.Canvas.FillRect(rect);
ImageList1.Draw(ComboBox1.Canvas, Rect.Left, Rect.Top, Index);
Combobox1.Canvas.TextOut(Rect.Left+ImageList1.Width+2,
Rect.Top, ComboBox1.Items[Index]);
The bitmap image and the text string display as a list item.
338
Creating a VCL Forms ActiveX Active Form
Like a Delphi control, an ActiveX control generates program code when you place the component on a
form or other logical container in the IDE. The main difference between an ActiveX control and a Delphi
control is that an ActiveX control is language independent. You can create ActiveX controls for
deployment to a variety of programming environments on Windows, not just Delphi or C++Builder, for
example.
This procedure uses the VCL forms ActiveX Active Form wizard to create an Active Form containing
two components. To test the control, you can deploy it to the Web. This procedure consists of the
following major steps:
1. Create an ActiveX library project for an ActiveX Active Form.
2. Add controls to the Active Form.
3. Add event handling code for the controls.
4. Deploy the project to the Web.
5. Display the form and test the controls in your Web browser.
2. Create a second directory to contain the ActiveX component and an HTML file for deploying the
Active Form to your Microsoft Internet Explorer Web browser. Name this directory ActiveX_Deploy.
3. Choose FileNewOther and select the ActiveX page in the New Items dialog.
The wizard generates the code needed to implement the ActiveX control and adds the code to the
project. If the project is already an ActiveX library, the wizard adds the control to the current project.
Note: If the project is not already an ActiveX library, a Warning dialog displays and
asks you if you want to start a new ActiveX library project.
339
To add some functionality to the Active Form
1. From the Standard page of the Component palette, add TEdit and TButton components to the form.
3. On the Events tab in the Object Inspector, double-click the OnClick event.
The Code editor opens with the cursor in place in the TActiveFormX.Button1Click event
handler block.
ShowMessage(Edit1.text);
2. On the Project page, use the Browse button to enter the path to the ActiveX_Deploy directory.
5. Click OK.
340
The Active Form displays in the browser window.
341
Creating a VCL Forms ActiveX Button
Like a Delphi control, an ActiveX control generates program code when you place the component on a
form or other logical container in the IDE. The main difference between an ActiveX control and a Delphi
control is that an ActiveX control is language independent. You can create ActiveX controls for
deployment to a variety of programming environments on Windows, not just Delphi or C++Builder, for
example.
This procedure uses the VCL forms ActiveX wizard to create an ActiveX control. To test the control, you
can install it on your machine as a VCL component in the IDE. To install the control, you first need to
create a package for it. This procedure consists of the following major steps:
1. Create an ActiveX library project for an ActiveX button control.
2. Register the ActiveX button so its icon can be displayed in the toolbar.
3. Create a package for the ActiveX button.
4. Install the package.
5. Test the ActiveX button.
2. Choose File New Other and select the ActiveX page in the New Items dialog.
5. By default, ButtonX displays as the New ActiveX Name. Rename ButtonX to the name you want
displayed for your ActiveX button, for example, MyActiveXButton.
Note: Modifications you make to the name update the Implementation Unit and
Project Name. Leave the remaining fields with default values.
6. Click OK.
The wizard generates the code needed to implement the ActiveX control and adds the code to the
project. If the project is already an ActiveX library, the wizard adds the control to the current project.
Note: If the project is not already an ActiveX library, a Warning dialog displays and
asks you if you want to start a new ActiveX library project.
342
7. Click OK to start the new ActiveX Library project.
Dismiss the warning about debugging. The project builds and creates an OCX file in your project
directory.
A dialog box displays a message indicating that registration was successful and it shows the path
to the resulting OCX file.
3. Click OK.
2. Double-click Package on the New page to display the Package - package.dpk dialog and click Add.
3. On the Add unit tab of the Add dialog, browse to your project directory.
5. Click OK to add the file to the package and return to the Package - package.dpk dialog.
The Package - package.dpk dialog displays showing the files in the package and two required files:
rtl.dcp and vcl.dcp.
2. On the Add unit tab of the Add dialog, browse to the Lib directory in Delphi, select the rtl.dcp file,
and click Open; then click OK on the Add dialog.
343
3. In the Package - package.dpk dialog, select vcl.dcp, and click Add.
4. On the Add unit tab of the Add dialog, browse to the Lib directory in Delphi, select the vcl.dcp file,
and click Open; then click OK on the Add dialog.
A dialog displays, indicating that the package has been installed. Click OK.
6. Click the X in the upper right corner of the Package - package.dpk dialog to close it.
2. From the ActiveX page of the Component palette, locate your button and place it on the form.
344
Importing .NET Controls to VCL.NET
There might be cases in which you want to use .NET components on your VCL.NET forms. There is no
direct way to use .NET components. You can, however, wrap the components in an ActiveX wrapper,
which then can be added to your VCL.NET application. Delphi 8 for .NET provides the .NETImport
Wizard to accomplish this task.
\Microsoft.NET\Framework\v1.1.4322\System.Windows.Forms.dll
3. Click Next.
This displays the second page of the Wizard, and lists all of the available components.
Note: If you want to import all components, click the Check All button.
5. Click Next.
This displays the third page of the Wizard, which provides generation options for the units.
This displays the fourth page of the Wizard, which allows you to set a location and a name for the
package file.
7. Click Next.
345
This displays the fifth page of the Wizard, which allows you to overwrite any existing files of the
same name.
8. Click Next.
This initiates the generation process and displays status messages for each file as it creates it,
including the package (.dpk) file.
9. If you want to import additional controls, click New, otherwise, click Finish.
2. Choose File Project Build <Project Name> where <Project Name> is the real name of your
project.
This creates the assembly file containing the package and the units.
5. Click Add.
7. Click OK.
The individual controls appear in the Tool Palette. You can now add the individual controls to your
VCL.NET form applications.
346
Reference
347
Delphi Overview
This chapter provides a brief introduction to Delphi programs, and program organization.
348
Language Overview
Delphi is a high-level, compiled, strongly typed language that supports structured and object-oriented
design. Based on Object Pascal, its benefits include easy-to-read code, quick compilation, and the use
of multiple unit files for modular programming. Delphi has special features that support Borland's
component framework and RAD environment. For the most part, descriptions and examples in this
language guide assume that you are using Borland development tools.
Most developers using Borland software development tools write and compile their code in the integrated
development environment (IDE). Borland development tools handle many details of setting up projects
and source files, such as maintenance of dependency information among units. The product also places
constraints on program organization that are not, strictly speaking, part of the Object Pascal language
specification. For example, Borland development tools enforce certain file- and program-naming
conventions that you can avoid if you write your programs outside of the IDE and compile them from
the command prompt.
This language guide generally assumes that you are working in the IDE and that you are building
applications that use the Borland Visual Component Library (VCL). Occasionally, however, Delphi-
specific rules are distinguished from rules that apply to all Object Pascal programming. This text covers
both the Win32 Delphi language compiler, and the Delphi for .NET language compiler. Platform-specific
language differences and features are noted where necessary.
Program Organization
Delphi programs are usually divided into source-code modules called units. Most programs begin with
a program heading, which specifies a name for the program. The program heading is followed by an
optional uses clause, then a block of declarations and statements. The uses clause lists units that are
linked into the program; these units, which can be shared by different programs, often have uses clauses
of their own.
The uses clause provides the compiler with information about dependencies among modules. Because
this information is stored in the modules themselves, most Delphi language programs do not require
makefiles, header files, or preprocessor "include" directives.
349
• Project files (which end with the .dpr extension)
• Package source files (which end with the .dpk extension)
Unit source files typically contain most of the code in an application. Each application has a single project
file and several unit files; the project file, which corresponds to the program file in traditional Pascal,
organizes the unit files into an application. Borland development tools automatically maintain a project
file for each application.
If you are compiling a program from the command line, you can put all your source code into unit (.pas)
files. If you use the IDE to build your application, it will produce a project (.dpr) file.
Package source files are similar to project files, but they are used to construct special dynamically
linkable libraries called packages.
A VCL form file contains the description of the properties of the form and the components it owns. Each
form file represents a single form, which usually corresponds to a window or dialog box in an application.
The IDE allows you to view and edit form files as text, and to save form files as either text (a format very
suitable for version control) or binary. Although the default behavior is to save form files as text, they
are usually not edited manually; it is more common to use Borland's visual design tools for this purpose.
Each project has at least one form, and each form has an associated unit (.pas) file that, by default, has
the same name as the form file.
In addition to VCL form files, each project uses a resource (.res) file to hold the application's icon and
other resources such as strings. By default, this file has the same name as the project (.dpr) file.
A project options (.dof) file contains compiler and linker settings, search path information, version
information, and so forth. Each project has an associated project options file with the same name as the
project (.dpr) file. Usually, the options in this file are set from Project Options dialog.
Various tools in the IDE store data in files of other types. Desktop settings (.dsk) files contain information
about the arrangement of windows and other configuration options; desktop settings can be project-
specific or environment-wide. These files have no direct effect on compilation.
Compiler-Generated Files
The first time you build an application or a package, the compiler produces a compiled unit file (.dcu on
Win32, .dcuil on .NET) for each new unit used in your project; all the .dcu/.dcuil files in your project are
then linked to create a single executable or shared package. The first time you build a package, the
compiler produces a file for each new unit contained in the package, and then creates both a .dcp and
350
a package file.If you use the GD switch, the linker generates a map file and a .drc file; the .drc file, which
contains string resources, can be compiled into a resource file.
When you build a project, individual units are not recompiled unless their source (.pas) files have
changed since the last compilation, their .dcu/.dpu files cannot be found, you explicitly tell the compiler
to reprocess them, or the interface of the unit depends on another unit which has been changed. In fact,
it is not necessary for a unit's source file to be present at all, as long as the compiler can find the compiled
unit file and that unit has no dependencies on other units that have changed.
Example Programs
The examples that follow illustrate basic features of Delphi programming. The examples show simple
applications that would not normally be compiled from the IDE; you can compile them from the command
line.
The first line declares a program called Greeting. The {$APPTYPE CONSOLE} directive tells the
compiler that this is a console application, to be run from the command line. The next line declares a
variable called MyMessage, which holds a string. (Delphi has genuine string data types.) The program
then assigns the string "Hello world!" to the variable MyMessage, and sends the contents of MyMessage
to the standard output using the Writeln procedure. (Writeln is defined implicitly in the System unit,
which the compiler automatically includes in every application.)
You can type this program into a file called greeting.pas or greeting.dpr and compile it by entering
dcc32 greeting
dccil greeting
to produce a managed .NET executable. In either case, the resulting executable prints the message
Hello world!
Aside from its simplicity, this example differs in several important ways from programs that you are likely
to write with Borland development tools. First, it is a console application. Borland development tools are
most often used to write applications with graphical interfaces; hence, you would not ordinarily call
Writeln. Moreover, the entire example program (save for Writeln) is in a single file. In a typical GUI
application, the program heading the first line of the example would be placed in a separate project file
that would not contain any of the actual application logic, other than a few calls to routines defined in
unit files.
351
A More Complicated Example
The next example shows a program that is divided into two files: a project file and a unit file. The project
file, which you can save as greeting.dpr, looks like this:
The first line declares a program called greeting, which, once again, is a console application. The
uses Unit1; clause tells the compiler that the program greeting depends on a unit called Unit1.
Finally, the program calls the PrintMessage procedure, passing to it the string Hello World! The
PrintMessage procedure is defined in Unit1. Here is the source code for Unit1, which must be
saved in a file called Unit1.pas:
Unit1 defines a procedure called PrintMessage that takes a single string as an argument and sends
the string to the standard output. (In Delphi, routines that do not return a value are called procedures.
Routines that return a value are called functions.) Notice that PrintMessage is declared twice in
Unit1. The first declaration, under the reserved word interface, makes PrintMessage available to
other modules (such as greeting) that use Unit1. The second declaration, under the reserved word
implementation, actually defines PrintMessage.
You can now compile Greeting from the command line by entering
dcc32 greeting
dccil greeting
There is no need to include Unit1 as a command-line argument. When the compiler processes greeting.
dpr, it automatically looks for unit files that the greeting program depends on. The resulting executable
does the same thing as our first example: it prints the message Hello world!
A VCL Application
Our next example is an application built using the Visual Component Library (VCL) components in the
IDE. This program uses automatically generated form and resource files, so you won't be able to compile
it from the source code alone. But it illustrates important features of the Delphi Language. In addition to
multiple units, the program uses classes and objects
The program includes a project file and two new unit files. First, the project file:
Once again, our program is called greeting. It uses three units: Forms, which is part of VCL;
Unit1, which is associated with the application's main form (Form1); and Unit2, which is associated
with another form (Form2).
The program makes a series of calls to an object named Application, which is an instance of the
TApplication class defined in the Forms unit. (Every project has an automatically generated
Application object.) Two of these calls invoke a TApplication method named CreateForm. The first
352
call to CreateForm creates Form1, an instance of the TForm1 class defined in Unit1. The second call
to CreateForm creates Form2, an instance of the TForm2 class defined in Unit2.
Unit1 creates a class named TForm1 (derived from TForm) and an instance of this class, Form1.
TForm1 includes a buttonButton1, an instance of TButton and a procedure named
Button1Click that is called when the user presses Button1. Button1Click hides Form1 and it
displays Form2 (the call to Form2.ShowModal).
Note: In the previous example, Form2.ShowModal relies on the use of auto-created
forms. While this is fine for example code, using auto-created forms is actively
discouraged.
Form2 is defined in Unit2:
unit Unit2;
interface
type
TForm2 = class(TForm)
Label1: TLabel;
CancelButton: TButton;
procedure CancelButtonClick(Sender: TObject);
end;
var
Form2: TForm2;
implementation
uses Unit1;
{$R *.dfm}
end.
Unit2 creates a class named TForm2 and an instance of this class, Form2. TForm2 includes a button
(CancelButton, an instance of TButton) and a label (Label1, an instance of TLabel). You can't see
353
this from the source code, but Label1 displays a caption that reads Hello world! The caption is defined
in Form2's form file, Unit2.dfm.
TForm2 declares and defines a method CancelButtonClick which will be invoked at runtime
whenever the user presses CancelButton. This procedure (along with Unit1's
TForm1.Button1Click) is called an event handler because it responds to events that occur while the
program is running. Event handlers are assigned to specific events by the form files for Form1 and
Form2.
When the greeting program starts, Form1 is displayed and Form2 is invisible. (By default, only the
first form created in the project file is visible at runtime. This is called the project's main form.) When the
user presses the button on Form1, Form2, displays the Hello world! greeting. When the user presses
the CancelButton or the Close button on the title bar, Form2 closes.
354
Programs and Units
This chapter provides a more detailed look at Delphi program organization.
355
Programs and Units
A Delphi program is constructed from source code modules called units. The units are tied together by
a special source code module that contains either the program, library, or package header. Each unit
is stored in its own file and compiled separately; compiled units are linked to create an application. Delphi
8 for .NET introduces hierarchical namespaces, giving you even more flexibility in organizing your units.
Namespaces and units allow you to
• Divide large programs into modules that can be edited separately.
• Create libraries that you can share among programs.
• Distribute libraries to other developers without making the source code available.
This topic covers the overall structure of a Delphi application: the program header, unit declaration
syntax, and the uses clause. Specific differences between the Win32 and .NET platforms are noted in
the text. The Delphi compiler does not support .NET namespaces on the Win32 platform. The Delphi 8
for .NET compiler does support hierarchical .NET namespaces; this topic is covered in the following
section, Using Namespaces with Delphi.
Additionally, a Delphi 8 for .NET program may contain a namespaces clause, to specify additional
namespaces in which to search for generic units. This topic is covered in more detail in the section
Using .NET Namespaces with Delphi.
The compiler, and hence the IDE, expect to find these three elements in a single project (.dpr) file.
356
The Program Heading
The program heading specifies a name for the executable program. It consists of the reserved word
program, followed by a valid identifier, followed by a semicolon. The identifier must match the project
source file name.
The following example shows the project source file for a program called Editor. Since the program is
called Editor, this project file is called Editor.dpr.
program Editor;
{$R *.res}
begin
Application.Title := 'Text Editor';
Application.CreateForm(TMainForm, MainForm);
Application.Run;
end.
The first line contains the program heading. The uses clause in this example specifies a dependency
on three additional units: Forms, REAbout, and REMain. The $R compiler directive links the project's
resource file into the program. Finally, the block of statements between the begin and end keywords are
executed when the program runs. The project file, like all Delphi source files, ends with a period (not a
semicolon).
Delphi project files are usually short, since most of a program's logic resides in its unit files. A Delphi
project file typically contains only enough code to launch the application's main window, and start the
event processing loop. Project files are generated and maintained automatically by the IDE, and it is
seldom necessary to edit them manually.
In standard Pascal, a program heading can include parameters after the program name:
In Delphi 8 for .NET, a the program heading introduces its own namespace, which is called the project
default namespace. This is also true for the library and package headers, when these types of projects
are compiled for the .NET platform.
357
The Program Uses Clause
The uses clause lists those units that are incorporated into the program. These units may in turn have
uses clauses of their own. For more information on the uses clause within a unit source file, see Unit
References and the Uses Clause, below.
The uses clause consists of the keyword uses, followed by a comma delimited list of units the project
file directly depends on.
The Block
The block contains a simple or structured statement that is executed when the program runs. In most
program files, the block consists of a compound statement bracketed between the reserved words begin
and end, whose component statements are simply method calls to the project's Application object.
Most projects have a global Application variable that holds an instance of TApplication,
TWebApplication, or TServiceApplication. The block can also contain declarations of constants, types,
variables, procedures, and functions; these declarations must precede the statement part of the block.
A unit file begins with a unit heading, which is followed by the interface keyword. Following the interface
keyword, the uses clause specifies a list of unit dependencies. Next comes the implementation section,
followed by the optional initialization, and finalization sections. A skeleton unit source file looks like this:
unit Unit1;
interface
implementation
initialization
finalization
358
end.
The unit must conclude with the reserved word end followed by a period.
would occur in a source file called MainForm.pas, and the file containing the compiled unit would be
MainForm.dcu or MainForm.dpu.
Unit names must be unique within a project. Even if their unit files are in different directories, two units
with the same name cannot be used in a single program.
The interface declaration of a procedure or function includes only the routine's signature. That is, the
routine's name, parameters, and return type (for functions). The block containing executable code for
the procedure or function follows in the implementation section. Thus procedure and function
declarations in the interface section work like forward declarations.
The interface declaration for a class must include declarations for all class members: fields, properties,
procedures, and functions.
The interface section can include its own uses clause, which must appear immediately after the keyword
interface.
In addition to definitions of public procedures and functions, the implementation section can declare
constants, types (including classes), variables, procedures, and functions that are private to the unit.
359
That is, unlike the interface section, entities declared in the implementation section are inaccessible to
other units.
The implementation section can include its own uses clause, which must appear immediately after the
keyword implementation. The identifiers declared within units specified in the implementation section
are only available for use within the implementation section itself. You cannot refer to such identifiers in
the interface section.
Finalization sections are executed in the opposite order from initialization sections. For example, if your
application initializes units A, B, and C, in that order, it will finalize them in the order C, B, and A.
Once a unit's initialization code starts to execute, the corresponding finalization section is guaranteed
to execute when the application shuts down. The finalization section must therefore be able to handle
incompletely initialized data, since, if a runtime error occurs, the initialization code might not execute
completely.
Note: The initialization and finalization sections behave differently when code is
compiled for the managed .NET environment. See the chapter on Memory
Management for more information.
360
• the implementation section of a unit
Most project files contain a uses clause, as do the interface sections of most units. The implementation
section of a unit can contain its own uses clause as well.
The System unit and the SysInit unit are used automatically by every application and cannot be listed
explicitly in the uses clause. (System implements routines for file I/O, string handling, floating point
operations, dynamic memory allocation, and so forth.) Other standard library units, such as
SysUtils, must be explicitly included in the uses clause. In most cases, all necessary units are placed
in the uses clause by the IDE, as you add and remove units from your project.
In unit declarations and uses clauses, unit names must match the file names in case. In other contexts
(such as qualified identifiers), unit names are case insensitive. To avoid problems with unit references,
refer to the unit source file explicitly:
uses Myunit;
uses
Forms,
Main;
uses
Windows, Messages, SysUtils,
Strings in 'C:\Classes\Strings.pas', Classes;
Use the keyword in after a unit name when you need to specify the unit's source file. Since the IDE
expects unit names to match the names of the source files in which they reside, there is usually no
361
reason to do this. Using in is necessary only when the location of the source file is unclear, for example
when
• You have used a source file that is in a different directory from the project file, and that directory is
not in the compiler's search path.
• Different directories in the compiler's search path have identically named units.
• You are compiling a console application from the command line, and you have named a unit with
an identifier that doesn't match the name of its source file.
The compiler also relies on the in ... construction to determine which units are part of a project. Only
units that appear in a project (.dpr) file's uses clause followed by in and a file name are considered to
be part of the project; other units in the uses clause are used by the project without belonging to it. This
distinction has no effect on compilation, but it affects IDE tools like the Project Manager.
In the uses clause of a unit, you cannot use in to tell the compiler where to find a source file. Every unit
must be in the compiler's search path. Moreover, unit names must match the names of their source files.
A uses clause need include only units used directly by the program or unit in which the clause appears.
That is, if unit A references constants, types, variables, procedures, or functions that are declared in
unit B, then A must use B explicitly. If B in turn references identifiers from unit C, then A is indirectly
dependent on C; in this case, C needn't be included in a uses clause in A, but the compiler must still be
able to find both B and C in order to process A.
program Prog;
uses Unit2;
const a = b;
// ...
unit Unit2;
interface
uses Unit1;
const b = c;
// ...
unit Unit1;
interface
362
const c = 1;
// ...
In this example, Prog depends directly on Unit2, which depends directly on Unit1. Hence Prog is
indirectly dependent on Unit1. Because Unit1 does not appear in Prog's uses clause, identifiers
declared in Unit1 are not available to Prog.
To compile a client module, the compiler needs to locate all units that the client depends on, directly or
indirectly. Unless the source code for these units has changed, however, the compiler needs only their .
dcu (Win32) or .dcuil (.NET) files, not their source (.pas) files.
When a change is made in the interface section of a unit, other units that depend on the change must
be recompiled. But when changes are made only in the implementation or other sections of a unit,
dependent units don't have to be recompiled. The compiler tracks these dependencies automatically
and recompiles units only when necessary.
In the simplest case of two mutually dependent units, this means that the units cannot list each other in
their interface uses clauses. So the following example leads to a compilation error:
unit Unit1;
interface
uses Unit2;
// ...
unit Unit2;
interface
uses Unit1;
// ...
However, the two units can legally reference each other if one of the references is moved to the
implementation section:
unit Unit1;
interface
uses Unit2;
// ...
unit Unit2;
363
interface
//...
implementation
uses Unit1;
// ...
To reduce the chance of circular references, it's a good idea to list units in the implementation uses
clause whenever possible. Only when identifiers from another unit are used in the interface section is it
necessary to list that unit in the interface uses clause.
364
Using Namespaces with Delphi
In Delphi 8 for .NET, a unit is still the basic container for types. Microsoft's Common Language Runtime
(CLR) introduces another layer of organization called a namespace. In the .NET Framework, a
namespace is a conceptual container of types. In Delphi 8 for .NET, a namespace is a container of
Delphi units. The addition of namespaces gives Delphi the ability to access and extend classes in the .
NET Framework.
Unlike traditional Delphi units, namespaces can be nested to form a containment hierarchy. Nested
namespaces provide a way to organize identifiers and types, and are used to disambiguate types with
the same name. Since they are a container for Delphi units, namespaces may also be used to
differentiate between units of the same name, that reside in different packages.
For example, the class MyClass in MyNameSpace, is different from the class MyClass in
YourNamespace. At runtime, the CLR always refers to classes and types by their fully qualified names:
the assembly name, followed by the namespace that contains the type. The CLR itself has no concept
or implementation of the namespace hierarchy; it is purely a notational convenience of the programming
language.
Declaring Namespaces
In Delphi 8 for .NET, a project file (program, library, or package) implicitly introduces its own namespace,
called the project default namespace. A unit may be a member of the project default namespace, or it
may explicitly declare itself to be a member of a different namespace. In either case, a unit declares its
namespace membership in its unit header. For example, consider the following explicit namespace
declaration:
unit MyCompany.MyWidgets.MyUnit;
First, notice that namespaces are separated by dots. Namespaces do not introduce new symbols for
the identifiers between the dots; the dots are part of the unit name. The source file name for this example
is MyCompany.MyWidgets.MyUnit.pas, and the compiled output file name is MyCompany.MyWidgets.
MyUnit.dcuil.
Second, notice that the dots imply the conceptual nesting, or containment, of one namespace within
another. The example above declares the unit MyUnit to be a member of the MyWidgets namespace,
which itself is contained in the MyCompany namespace. Again, it should be noted that this containment
is for documentation purposes only.
365
A project default namespace declares a namespace for all of the units in the project. Consider the
following declarations:
Program MyCompany.Programs.MyProgram;
Library MyCompany.Libs.MyLibrary;
Package MyCompany.Packages.MyPackage;
These statements establish the project default namespace for the program, library, and package,
respectively. The namespace is determined by removing the rightmost identifier (and dot) from the
declaration.
A unit that omits an explicit namespace is called a generic unit. A generic unit automatically becomes
a member of the project default namespace. Given the preceding program declaration, the following
unit declaration would cause the compiler to treat MyUnit as a member of the
MyCompany.Programs namespace.
unit MyUnit;
The project default namespace does not affect the name of the Delphi source file for a generic unit. In
the preceding example, the Delphi source file name would be MyUnit.pas. The compiler does however
prefix the dcuil file name with the project default namespace. The resulting dcuil file in the current
example would be MyCompany.Programs.MyUnit.dcuil.
Namespace strings are not case-sensitive. The compiler considers two namespaces that differ only in
case to be equivalent. However, the compiler does preserve the case of a namespace, and will use the
preserved casing in output file names, error messages, and RTTI unit identifiers. RTTI for class and
type names will include the full namespace specification.
Searching Namespaces
A unit must declare the other units on which it depends. As with the Win32 platform, the Delphi 8
for .NET compiler must search these units for identifiers. For units in explicit namespaces the search
scope is already known, but for generic units, the compiler must establish a namespace search scope.
unit MyCompany.Programs.Units.MyUnit1;
uses MyCompany.Libs.Unit2, Unit3, Unit4;
These declarations establish MyUnit1 as a member of the MyCompany.Programs.Units
namespace. MyUnit1 depends on three other units: MyCompany.Libs.Unit2, and the generic units,
Unit3, and Unit4. The compiler can resolve identifier names in Unit2, since the uses clause specified
the fully qualified unit name. To resolve identifier names in Unit3 and Unit4, the compiler must
establish a namespace search order.
366
Namespace search order
Search locations can come from four possible sources: Compiler options, the project file's namespaces
clause, the project default namespace, and finally, the current unit's namespace.
A project file (program, library or package) may optionally specify a list of namespaces to be searched
when resolving generic unit names. The namespaces clause must appear in the project file, immediately
after the program, library, or package declaration and before any other clause or block type. The
namespaces clause is a list of namespace identifiers, separated by commas. A semicolon must
terminate the list of namespaces.
Note that if the current unit is generic (i.e. it does not have an explicit namespace declaration in its unit
statement), then resolution begins with the project default namespace.
367
Using Namespaces
Delphi's uses clause brings a module into the context of the current unit. The uses clause must either
refer to a module by its fully qualified name (i.e. including the full namespace specification), or by its
generic name, thereby relying on the namespace resolution mechanisms to locate the unit.
unit MyCompany.Libs.MyUnit1
uses MyCompany.Libs.Unit2, // Fully qualified name.
UnitX; // Generic name.
Once a module has been brought into context, source code can refer to identifiers within that module
either by the unqualified name, or by the fully qualified name (if necessary, to disambiguate identifiers
with the same name in different units). The following writeln statements are equivalent:
uses MyCompany.Libs.Unit2;
begin
writeln(MyCompany.Libs.Unit2.SomeString);
writeln(SomeString);
end.
A fully qualified identifier must include the full namespace specification. In the preceding example, it
would be an error to refer to SomeString using only a portion of the namespace:
writeln(Unit2.SomeString); // ERROR!
writeln(Libs.Unit2.SomeString); // ERROR!
writeln(MyCompany.Libs.Unit2.SomeString); // Correct.
writeln(SomeString); // Correct.
It is also an error to refer to only a portion of a namespace in the uses clause. There is no mechanism
to import all units and symbols in a namespace. The following code does not import all units and symbols
in the MyCompany namespace:
368
Unit aliases
Namespaces can become quite long and cumbersome to type. Delphi for .NET allows you to declare a
local unit alias to introduce an alias for a long namespace. The following uses clause introduces a local
unit alias:
uses MyCompany.AVeryLongNamespaceDesignation.VeryDescriptiveUnitName as
aUnit;
Given a unit alias, your source code can refer to an identifier by the shorter name:
369
Fundamental Syntactic Elements
This section describes the fundamental syntactic elements, or the building blocks of the Delphi language.
370
Fundamental Syntactic Elements
This topic introduces the Delphi language character set, and describes the syntax for declaring:
• Identifiers
• Numbers
• Character strings
• Labels
• Source code comments
Fundamental syntactic elements, called tokens, combine to form expressions, declarations, and
statements. A statement describes an algorithmic action that can be executed within a program. An
expression is a syntactic unit that occurs within a statement and denotes a value. A declaration defines
an identifier (such as the name of a function or variable) that can be used in expressions and statements,
and, where appropriate, allocates memory for the identifier.
Size:=20;Price:=10;
is perfectly legal. Convention and readability, however, dictate that we write this as
Size := 20;
Price := 10;
Tokens are categorized as special symbols, identifiers, reserved words, directives, numerals, labels,
and character strings. A separator can be part of a token only if the token is a character string. Adjacent
identifiers, reserved words, numerals, and labels must have one or more separators between them.
Special Symbols
Special symbols are non-alphanumeric characters, or pairs of such characters, that have fixed
meanings. The following single characters are special symbols:
371
#$&'()*+,-./:;<=>@[]^{}
[ (.
] .)
{ (*
} *)
The left bracket [ is equivalent to the character pair of left parenthesis and period (.
The right bracket ] is equivalent to the character pair of period and right parenthesis .)
The left brace { is equivalent to the character pair of left parenthesis and asterisk (*.
The right brace } is equivalent to the character pair of right parenthesis and asterisk *)
Note: %, ?, \, !, " (double quotation marks), _ (underscore), | (pipe), and ~ (tilde) are
not special characters.
Identifiers
Identifiers denote constants, variables, fields, types, properties, procedures, functions, programs, units,
libraries, and packages. An identifier can be of any length, but only the first 255 characters are significant.
An identifier must begin with a letter or an underscore (_) and cannot contain spaces; letters, digits, and
underscores are allowed after the first character. Reserved words cannot be used as identifiers.
Since the Delphi Language is case-insensitive, an identifier like CalculateValue could be written in
any of these ways:
CalculateValue
calculateValue
calculatevalue
CALCULATEVALUE
Since unit names correspond to file names, inconsistencies in case can sometimes affect compilation.
For more information, see the topic, Unit References and the Uses Clause.
Qualified Identifiers
When you use an identifier that has been declared in more than one place, it is sometimes necessary
to qualify the identifier. The syntax for a qualified identifier is
372
identifier1.identifier2
where identifier1 qualifies identifier2. For example, if two units each declare a variable called
CurrentValue, you can specify that you want to access the CurrentValue in Unit2 by writing
Unit2.CurrentValue
Qualifiers can be iterated. For example,
Form1.Button1.Click
calls the Click method in Button1 of Form1.
If you don't qualify an identifier, its interpretation is determined by the rules of scope described in Blocks
and scope.
Reserved Words
The following reserved words cannot be redefined or used as identifiers.
Reserved Words
In addition to the words above, private, protected, public, published, and automated act as reserved
words within class type declarations, but are otherwise treated as directives. The words at and on also
have special meanings, and should be treated as reserved words.
373
Directives
Directives are words that are sensitive in specific locations within source code. Directives have special
meanings in the Delphi language, but, unlike reserved words, appear only in contexts where user-
defined identifiers cannot occur. Hence -- although it is inadvisable to do so -- you can define an identifier
that looks exactly like a directive.
Directives
Numerals
Integer and real constants can be represented in decimal notation as sequences of digits without
commas or spaces, and prefixed with the + or - operator to indicate sign. Values default to positive (so
that, for example, 67258 is equivalent to +67258) and must be within the range of the largest predefined
real or integer type.
Numerals with decimal points or exponents denote reals, while other numerals denote integers. When
the character E or e occurs within a real, it means "times ten to the power of". For example, 7E2 means
7 * 10^2, and 12.25e+6 and 12.25e6 both mean 12.25 * 10^6.
The dollar-sign prefix indicates a hexadecimal numeralfor example, $8F. Hexadecimal numbers without
a preceding - unary operator are taken to be positive values. During an assignment, if a hexadecimal
value lies outside the range of the receiving type an error is raised, except in the case of the Integer (32-
bit integer) where a warning is raised. In this case, values exceeding the positive range for Integer are
taken to be negative numbers in a manner consistent with 2's complement integer representation.
For more information about real and integer types, see Data Types, Variables, and Constants. For
information about the data types of numerals, see True constants.
374
Labels
A label is a standard Delphi language identifier with the exception that, unlike other identifiers, labels
can start with a digit. Numeric labels can include no more than ten digits - that is, a numeral between 0
and 9999999999.
Labels are used in goto statements. For more information about goto statements and labels, see Goto
statements.
Character Strings
A character string, also called a string literal or string constant, consists of a quoted string, a control
string, or a combination of quoted and control strings. Separators can occur only within quoted strings.
A quoted string is a sequence of up to 255 characters from the extended ASCII character set, written
on one line and enclosed by apostrophes. A quoted string with nothing between the apostrophes is a
null string. Two sequential apostrophes in a quoted string denote a single character, namely an
apostrophe. For example,
'BORLAND' { BORLAND }
'You''ll see' { You'll see }
'''' { ' }
'' { null string }
' ' { a space }
A control string is a sequence of one or more control characters, each of which consists of the # symbol
followed by an unsigned integer constant from 0 to 255 (decimal or hexadecimal) and denotes the
corresponding ASCII character. The control string
#89#111#117
is equivalent to the quoted string
'You'
You can combine quoted strings with control strings to form larger character strings. For example, you
could use
A character string's length is the number of characters in the string. A character string of any length is
compatible with any string type and with the PChar type. A character string of length 1 is compatible
with any character type, and, when extended syntax is enabled (with compiler directive {$X+}), a
375
nonempty character string of length n is compatible with zero-based arrays and packed arrays of n
characters. For more information, see Datatypes, Variables, and Constants.
A comment that contains a dollar sign ($) immediately after the opening { or (* is a compiler directive.
For example,
{$WARNINGS OFF}
tells the compiler not to generate warning messages.
376
Declarations and Statements
This topic describes the syntax of Delphi declarations and statements.
Aside from the uses clause (and reserved words like implementation that demarcate parts of a unit), a
program consists entirely of declarations and statements, which are organized into blocks.
Declarations
The names of variables, constants, types, fields, properties, procedures, functions, programs, units,
libraries, and packages are called identifiers. (Numeric constants like 26057 are not identifiers.)
Identifiers must be declared before you can use them; the only exceptions are a few predefined types,
routines, and constants that the compiler understands automatically, the variable Result when it occurs
inside a function block, and the variable Self when it occurs inside a method implementation.
A declaration defines an identifier and, where appropriate, allocates memory for it. For example,
var
Size: Extended;
Quantity: Integer;
Description: string
The syntax and placement of a declaration depend on the kind of identifier you are defining. In general,
declarations can occur only at the beginning of a block or at the beginning of the interface or
implementation section of a unit (after the uses clause). Specific conventions for declaring variables,
constants, types, functions, and so forth are explained in the documentation for those topics.
377
Hinting Directives
The 'hint' directives platform, deprecated, and library may be appended to any declaration. These
directives will produce warnings at compile time. Hint directives can be applied to type declarations,
variable declarations, class and structure declarations, field declarations within classes or records,
procedure, function and method declarations, and unit declarations.
When a hint directive appears in a unit declaration, it means that the hint applies to everything in the
unit. For example, the Windows 3.1 style OleAuto.pas unit on Windows is completely deprecated. Any
reference to that unit or any symbol in that unit will produce a deprecation message.
The platform hinting directive on a symbol or unit indicates that it may not exist or that the implementation
may vary considerably on different platforms. The library hinting directive on a symbol or unit indicates
that the code may not exist or the implementation may vary considerably on different library
architectures.
The platform and library directives do not specify which platform or library. If your goal is writing platform-
independent code, you do not need to know which platform a symbol is specific to; it is sufficient that
the symbol be marked as specific to some platform to let you know it may cause problems for your goal
of portability.
In the case of a procedure or function declaration, the hint directive should be separated from the rest
of the declaration with a semicolon. Examples:
var
VersionNumber: Real library;
type
AppError = class(Exception)
...
end platform;
When source code is compiled in the {$HINTS ON} {$WARNINGS ON} state, each reference to an
identifier declared with one of these directives generates an appropriate hint or warning. Use platform
to mark items that are specific to a particular operating environment (such as Windows or .NET),
deprecated to indicate that an item is obsolete or supported only for backward compatibility, and library
to flag dependencies on a particular library or component framework.
The Delphi 8 for .NET compiler also recognizes the hinting directive experimental. You can use this
directive to designate units which are in an unstable, development state. The compiler will emit a warning
when it builds an application that uses the unit.
378
Statements
Statements define algorithmic actions within a program. Simple statements like assignments and
procedure calls can combine to form loops, conditional statements, and other structured statements.
Multiple statements within a block, and in the initialization or finalization section of a unit, are separated
by semicolons.
Simple Statements
A simple statement doesn't contain any other statements. Simple statements include assignments, calls
to procedures and functions, and goto jumps.
Assignment Statements
An assignment statement has the form
variable := expression
where variable is any variable reference, including a variable, variable typecast, dereferenced pointer,
or component of a structured variable. The expression is any assignment-compatible expression (within
a function block, variable can be replaced with the name of the function being defined. See Procedures
and functions). The := symbol is sometimes called the assignment operator.
An assignment statement replaces the current value of variable with the value of expression. For
example,
I := 3;
assigns the value 3 to the variable I. The variable reference on the left side of the assignment can
appear in the expression on the right. For example,
I := I + 1;
increments the value of I. Other assignment statements include
X := Y + Z;
Done := (I >= 1) and (I < 100);
Hue1 := [Blue, Succ(C)];
I := Sqr(J) - I * K;
Shortint(MyChar) := 122;
TByteRec(W).Hi := 0;
MyString[I] := 'A';
SomeArray[I + 1] := P^;
TMyObject.SomeProperty := True;
379
Procedure and Function Calls
A procedure call consists of the name of a procedure (with or without qualifiers), followed by a parameter
list (if required). Examples include
PrintHeading;
Transpose(A, N, M);
Find(Smith, William);
Writeln('Hello world!');
DoSomething();
Unit1.SomeProcedure;
TMyObject.SomeMethod(X,Y);
With extended syntax enabled ({$X+}), function calls, like calls to procedures, can be treated as
statements in their own right:
MyFunction(X);
When you use a function call in this way, its return value is discarded.
For more information about procedures and functions, see Procedures and functions.
Goto Statements
A goto statement, which has the form
goto label
transfers program execution to the statement marked by the specified label. To mark a statement, you
must first declare the label. Then precede the statement you want to mark with the label and a colon:
label: statement
label label;
A label can be any valid identifier or any numeral between 0 and 9999.
The label declaration, marked statement, and goto statement must belong to the same block. (See
Blocks and Scope, below.) Hence it is not possible to jump into or out of a procedure or function. Do not
mark more than one statement in a block with the same label.
For example,
label StartHere;
...
380
StartHere: Beep;
goto StartHere;
creates an infinite loop that calls the Beep procedure repeatedly.
procedure FindFirstAnswer;
var X, Y, Z, Count: Integer;
label FoundAnAnswer;
begin
Count := SomeConstant;
for X := 1 to Count do
for Y := 1 to Count do
for Z := 1 to Count do
if ... { some condition holds on X, Y, and Z } then
goto FoundAnAnswer;
FoundAnAnswer:
... { Code to execute when an answer is found }
end;
Notice that we are using goto to jump out of a nested loop. Never jump into a loop or other structured
statement, since this can have unpredictable effects.
Structured Statements
Structured statements are built from other statements. Use a structured statement when you want to
execute other statements sequentially, conditionally, or repeatedly.
• A compound or with statement simply executes a sequence of constituent statements.
• A conditional statement that is an if or case statement executes at most one of its constituents,
depending on specified criteria.
• Loop statements including repeat, while, and for loops execute a sequence of constituent
statements repeatedly.
• A special group of statements including raise, try...except, and try...finally constructions create and
handle exceptions. For information about exception generation and handling, see Exceptions.
381
Compound Statements
A compound statement is a sequence of other (simple or structured) statements to be executed in the
order in which they are written. The compound statement is bracketed by the reserved words begin and
end, and its constituent statements are separated by semicolons. For example:
begin
Z := X;
X := Y;
X := Y;
end;
The last semicolon before end is optional. So we could have written this as
begin
Z := X;
X := Y;
Y := Z
end;
Compound statements are essential in contexts where Delphi syntax requires a single statement. In
addition to program, function, and procedure blocks, they occur within other structured statements, such
as conditionals or loops. For example:
begin
I := SomeConstant;
while I > 0 do
begin
...
I := I - 1;
end;
end;
You can write a compound statement that contains only a single constituent statement; like parentheses
in a complex term, begin and end sometimes serve to disambiguate and to improve readability. You can
also use an empty compound statement to create a block that does nothing:
begin
end;
With Statements
A with statement is a shorthand for referencing the fields of a record or the fields, properties, and
methods of an object. The syntax of a with statement is
382
withobjdostatement
or
where obj is an expression yielding a reference to a record, object instance, class instance, interface or
class type (metaclass) instance, and statement is any simple or structured statement. Within the
statement, you can refer to fields, properties, and methods of obj using their identifiers alone, that is,
without qualifiers.
type
TDate = record
Day: Integer;
Month: Integer;
Year: Integer;
end;
var
OrderDate: TDate;
you could write the following with statement.
with OrderDate do
if Month = 12 then
begin
Month := 1;
Year := Year + 1;
end
else
Month := Month + 1;
you could write the following with statement.
if OrderDate.Month = 12 then
begin
OrderDate.Month := 1;
OrderDate.Year := OrderDate.Year + 1;
end
else
OrderDate.Month := OrderDate.Month + 1;
If the interpretation of obj involves indexing arrays or dereferencing pointers, these actions are
performed once, before statement is executed. This makes with statements efficient as well as concise.
It also means that assignments to a variable within statement cannot affect the interpretation of obj
during the current execution of the with statement.
383
Each variable reference or method name in a with statement is interpreted, if possible, as a member of
the specified object or record. If there is another variable or method of the same name that you want to
access from the with statement, you need to prepend it with a qualifier, as in the following example.
with OrderDate do
begin
Year := Unit1.Year;
...
end;
When multiple objects or records appear after with, the entire statement is treated like a series of nested
with statements. Thus
is equivalent to
with obj1 do
with obj2 do
...
with objn do
// statement
In this case, each variable reference or method name in statement is interpreted, if possible, as a
member of objn; otherwise it is interpreted, if possible, as a member of objn1; and so forth. The same
rule applies to interpreting the objs themselves, so that, for instance, if objn is a member of both obj1
and obj2, it is interpreted as obj2.objn.
If Statements
There are two forms of if statement: if...then and the if...then...else. The syntax of an if...then statement is
ifexpressionthenstatement
where expression returns a Boolean value. If expression is True, then statement is executed; otherwise
it is not. For example,
ifexpressionthenstatement1elsestatement2
where expression returns a Boolean value. If expression is True, then statement1 is executed; otherwise
statement2 is executed. For example,
if J = 0 then
Exit
384
else
Result := I / J;
The then and else clauses contain one statement each, but it can be a structured statement. For
example,
if J <> o then
begin
Result := I / J;
Count := Count + 1;
end
else if Count = Last then
Done := True
else
Exit;
Notice that there is never a semicolon between the then clause and the word else. You can place a
semicolon after an entire if statement to separate it from the next statement in its block, but the then and
else clauses require nothing more than a space or carriage return between them. Placing a semicolon
immediately before else (in an if statement) is a common programming error.
A special difficulty arises in connection with nested if statements. The problem arises because some if
statements have else clauses while others do not, but the syntax for the two kinds of statement is
otherwise the same. In a series of nested conditionals where there are fewer else clauses than if
statements, it may not seem clear which else clauses are bound to which ifs. Consider a statement of
the form
ifexpression1thenifexpression2thenstatement1elsestatement2;
The compiler always parses in the first way. That is, in real code, the statement
385
if ... {expression2} then
... {statement1}
else
... {statement2}
end;
The rule is that nested conditionals are parsed starting from the innermost conditional, with each else
bound to the nearest available if on its left. To force the compiler to read our example in the second way,
you would have to write it explicitly as
Case Statements
The case statement may provide a readable alternative to deeply nested if conditionals. A case
statement has the form
case selectorExpression of
caseList1: statement1;
...
caseListn: statementn;
end
where selectorExpression is any expression of an ordinal type (string types are invalid) and each
caseList is one of the following:
• A numeral, declared constant, or other expression that the compiler can evaluate without executing
your program. It must be of an ordinal type compatible with selectorExpression. Thus 7, True, 4 +
5 * 3, 'A', and Integer('A') can all be used as caseLists, but variables and most function calls
cannot. (A few built-in functions like Hi and Lo can occur in a caseList. See Constant expressions.)
• A subrange having the form First..Last, where First and Last both satisfy the criterion above and
First is less than or equal to Last.
• A list having the form item1, ..., itemn, where each item satisfies one of the criteria above.
Each value represented by a caseList must be unique in the case statement; subranges and lists cannot
overlap. A case statement can have a final else clause:
case selectorExpression of
386
caseList1: statement1;
...
caselistn: statementn;
else
statements;
end
where statements is a semicolon-delimited sequence of statements. When a case statement is
executed, at most one of statement1 ... statementn is executed. Whichever caseList has a value equal
to that of selectorExpression determines the statement to be used. If none of the caseLists has the same
value as selectorExpression, then the statements in the else clause (if there is one) are executed.
case I of
1..5: Caption := 'Low';
6..9: Caption := 'High';
0, 10..99: Caption := 'Out of range';
else
Caption := '';
end
is equivalent to the nested conditional
if I in [1..5] then
Caption := 'Low';
else if I in [6..10] then
Caption := 'High';
else if (I = 0) or (I in [10..99]) then
Caption := 'Out of range'
else
Caption := '';
Other examples of case statements
case MyColor of
Red: X := 1;
Green: X := 2;
Blue: X = 3;
Yellow, Orange, Black: X := 0;
end;
case Selection of
Done: Form1.Close;
Compute: calculateTotal(UnitCost, Quantity);
else
387
Beep;
end;
Control Loops
Loops allow you to execute a sequence of statements repeatedly, using a control condition or variable
to determine when the execution stops. Delphi has three kinds of control loop: repeat statements, while
statements, and for statements.
You can use the standard Break and Continue procedures to control the flow of a repeat, while, or
for statement. Break terminates the statement in which it occurs, while Continue begins executing
the next iteration of the sequence.
Repeat Statements
The syntax of a repeat statement is
where expression returns a Boolean value. (The last semicolon before until is optional.) The repeat
statement executes its sequence of constituent statements continually, testing expression after each
iteration. When expression returns True, the repeat statement terminates. The sequence is always
executed at least once because expression is not evaluated until after the first iteration.
repeat
K := I mod J;
I := J;
J := K;
until J = 0;
repeat
Write('Enter a value (0..9): ');
Readln(I);
until (I >= 0) and (I <= 9);
While Statements
A while statement is similar to a repeat statement, except that the control condition is evaluated before
the first execution of the statement sequence. Hence, if the condition is false, the statement sequence
is never executed.
whileexpressiondostatement
388
where expression returns a Boolean value and statement can be a compound statement. The while
statement executes its constituent statement repeatedly, testing expression before each iteration. As
long as expression returns True, execution continues.
while I > 0 do
begin
if Odd(I) then Z := Z * X;
I := I div 2;
X := Sqr(X);
end;
For Statements
A for statement, unlike a repeat or while statement, requires you to specify explicitly the number of
iterations you want the loop to go through. The syntax of a for statement is
forcounter := initialValuetofinalValuedostatement
or
forcounter := initialValuedowntofinalValuedostatement
where
• counter is a local variable (declared in the block containing the for statement) of ordinal type, without
any qualifiers.
• initialValue and finalValue are expressions that are assignment-compatible with counter.
• statement is a simple or structured statement that does not change the value of counter.
The for statement assigns the value of initialValue to counter, then executes statement repeatedly,
incrementing or decrementing counter after each iteration. (The for...to syntax increments counter, while
the for...downto syntax decrements it.) When counter returns the same value as finalValue, statement
is executed once more and the for statement terminates. In other words, statement is executed once
for every value in the range from initialValue to finalValue. If initialValue is equal to finalValue,
statement is executed exactly once. If initialValue is greater than finalValue in a for...to statement, or
less than finalValue in a for...downto statement, then statement is never executed. After the for statement
terminates (provided this was not forced by a Break or an Exit procedure), the value of counter is
undefined.
389
For purposes of controlling execution of the loop, the expressions initialValue and finalValue are
evaluated only once, before the loop begins. Hence the for...to statement is almost, but not quite,
equivalent to this while construction:
begin
counter := initialValue;
while counter <= finalValue do
begin
... {statement};
counter := Succ(counter);
end;
end
The difference between this construction and the for...to statement is that the while loop reevaluates
finalValue before each iteration. This can result in noticeably slower performance if finalValue is a
complex expression, and it also means that changes to the value of finalValue within statement can
affect execution of the loop.
for I := 2 to 63 do
if Data[I] > Max then
Max := Data[I];
for I := 1 to 10 do
for J := 1 to 10 do
begin
X := 0;
for K := 1 to 10 do
X := X + Mat1[I,K] * Mat2[K,J];
Mat[I,J] := X;
end;
390
Blocks
A block consists of a series of declarations followed by a compound statement. All declarations must
occur together at the beginning of the block. So the form of a block is
{declarations}
begin
{statements}
end
The declarations section can include, in any order, declarations for variables, constants (including
resource strings), types, procedures, functions, and labels. In a program block, the declarations section
can also include one or more exports clauses (see Libraries and packages).
Scope
An identifier, such as a variable or function name, can be used only within the scope of its declaration.
The location of a declaration determines its scope. An identifier declared within the declaration of a
program, function, or procedure has a scope limited to the block in which it is declared. An identifier
declared in the interface section of a unit has a scope that includes any other units or programs that use
the unit where the declaration occurs. Identifiers with narrower scope, especially identifiers declared in
functions and procedures, are sometimes called local, while identifiers with wider scope are called
global.
The rules that determine identifier scope are summarized below.
391
If the identifier is declared in ... its scope extends ...
the declaration section of a program, function, or from the point where it is declared to the end of the current
procedure block, including all blocks enclosed within that scope.
the interface section of a unit from the point where it is declared to the end of the unit,
and to any other unit or program that uses that unit. (See
Programs and Units.)
the implementation section of a unit, but not within the from the point where it is declared to the end of the unit.
block of any function or procedure The identifier is available to any function or procedure in
the unit, including the initialization and finalization
sections, if present.
the definition of a record type (that is, the identifier is the from the point of its declaration to the end of the record-
name of a field in the record) type definition. (See Records.)
the definition of a class (that is, the identifier is the name from the point of its declaration to the end of the class-
of a data field property or method in the class) type definition, and also includes descendants of the
class and the blocks of all methods in the class and its
descendants. (See Classes and Objects.)
Naming Conflicts
When one block encloses another, the former is called the outer block and the latter the inner block. If
an identifier declared in an outer block is redeclared in an inner block, the inner declaration takes
precedence over the outer one and determines the meaning of the identifier for the duration of the inner
block. For example, if you declare a variable called MaxValue in the interface section of a unit, and then
declare another variable with the same name in a function declaration within that unit, any unqualified
occurrences of MaxValue in the function block are governed by the second, local declaration. Similarly,
a function declared within another function creates a new, inner scope in which identifiers used by the
outer function can be redeclared locally.
The use of multiple units further complicates the definition of scope. Each unit listed in a uses clause
imposes a new scope that encloses the remaining units used and the program or unit containing the
uses clause. The first unit in a uses clause represents the outermost scope and each succeeding unit
represents a new scope inside the previous one. If two or more units declare the same identifier in their
interface sections, an unqualified reference to the identifier selects the declaration in the innermost
scope, that is, in the unit where the reference itself occurs, or, if that unit doesn't declare the identifier,
in the last unit in the uses clause that does declare the identifier.
The System and SysInit units are used automatically by every program or unit. The declarations
in System, along with the predefined types, routines, and constants that the compiler understands
automatically, always have the outermost scope.
You can override these rules of scope and bypass an inner declaration by using a qualified identifier
(see Qualified Identifiers) or a with statement (see With Statements, above).
392
Expressions
This topic describes syntax rules of forming Delphi expressions.
Expressions
An expression is a construction that returns a value. The following table shows examples of Delphi
expressions:
X variable
@X address of the variable X
15 integer constant
InterestRate variable
Calc(X, Y) function call
X * Y product of X and Y
Z / (1 - Z) quotient of Z and (1 - Z)
X = 1.5 Boolean
C in Range1 Boolean
not Done negation of a Boolean
['a', 'b', 'c']set
Char(48) value typecast
The simplest expressions are variables and constants (described in Data types, variables, and
constants). More complex expressions are built from simpler ones using operators, function calls, set
constructors, indexes, and typecasts.
Operators
Operators behave like predefined functions that are part of the the Delphi language. For example, the
expression (X + Y) is built from the variables X and Y, called operands, with the + operator; when X
393
and Y represent integers or reals, (X + Y) returns their sum. Operators include @, not, ^, *, /, div,
mod, and, shl, shr, as, +, -, or, xor, =, >, <, <>, <=, >=, in, and is.
The operators @, not, and ^ are unary (taking one operand). All other operators are binary (taking two
operands), except that + and - can function as either a unary or binary operator. A unary operator always
precedes its operand (for example, -B), except for ^, which follows its operand (for example, P^). A
binary operator is placed between its operands (for example, A = 7).
Some operators behave differently depending on the type of data passed to them. For example, not
performs bitwise negation on an integer operand and logical negation on a Boolean operand. Such
operators appear below under multiple categories.
Except for ^, is, and in, all operators can take operands of type Variant.
The sections that follow assume some familiarity with Delphi data types.
For information about operator precedence in complex expressions, see Operator Precedence Rules,
later in this topic.
Arithmetic Operators
Arithmetic operators, which take real or integer operands, include +, -, *, /, div, and mod.
Binary Arithmetic Operators
394
Integer. If an operand's type is a subrange of an integer type, it is treated as if it were of the integer
type.
• The value of x div y is the value of x / y rounded in the direction of zero to the nearest integer.
• The mod operator returns the remainder obtained by dividing its operands. In other words, x mod
y = x (x div y) * y.
• A runtime error occurs when y is zero in an expression of the form x / y, x div y, or x mod y.
Boolean Operators
The Boolean operators not, and, or, and xor take operands of any Boolean type and return a value of
type Boolean.
Boolean Operators
These operations are governed by standard rules of Boolean logic. For example, an expression of the
form x and y is True if and only if both x and y are True.
Short-circuit evaluation is usually preferable because it guarantees minimum execution time and, in
most cases, minimum code size. Complete evaluation is sometimes convenient when one operand is
a function with side effects that alter the execution of the program.
Short-circuit evaluation also allows the use of constructions that might otherwise result in illegal runtime
operations. For example, the following code iterates through the string S, up to the first comma.
395
Inc(I);
end;
In the case where S has no commas, the last iteration increments I to a value which is greater than the
length of S. When the while condition is next tested, complete evaluation results in an attempt to read
S[I], which could cause a runtime error. Under short-circuit evaluation, in contrast, the second part of
the while condition (S[I] <> ',') is not evaluated after the first part fails.
Use the $B compiler directive to control evaluation mode. The default state is {$B}, which enables short-
circuit evaluation. To enable complete evaluation locally, add the {$B+} directive to your code. You can
also switch to complete evaluation on a project-wide basis by selecting Complete Boolean Evaluation
in the Compiler Options dialog (all source units will need to be recompiled).
Note: If either operand involves a Variant, the compiler always performs complete
evaluation (even in the {$B} state).
Z := X or Y;
assigns the value 101101 to Z.
Logical (Bitwise) Operators
396
example, if x is an integer, x shl 40 is interpreted as x shl 8 because an integer is 32 bits and
40 mod 32 is 8.
String Operators
The relational operators =, <>, <, >, <=, and >= all take string operands (see Relational operators). The
+ operator concatenates two strings.
String Operators
Pointer Operators
The relational operators <, >, <=, and >= can take operands of type PChar and PWideChar (see
Relational operators). The following operators also take pointers as operands. For more information
about pointers, see Pointers and pointer types.
Character-pointer operators
The ^ operator dereferences a pointer. Its operand can be a pointer of any type except the generic
Pointer, which must be typecast before dereferencing.
P = Q is True just in case P and Q point to the same address; otherwise, P <> Q is True.
397
You can use the + and - operators to increment and decrement the offset of a character pointer. You
can also use - to calculate the difference between the offsets of two character pointers. The following
rules apply.
• If I is an integer and P is a character pointer, then P + I adds I to the address given by P; that
is, it returns a pointer to the address I characters after P. (The expression I + P is equivalent
to P + I.) P - I subtracts I from the address given by P; that is, it returns a pointer to the address
I characters before P. This is true for PChar pointers; for PWideChar pointers P + I adds SizeOf
(WideChar) to P.
• If P and Q are both character pointers, then P - Q computes the difference between the address
given by P (the higher address) and the address given by Q (the lower address); that is, it returns
an integer denoting the number of characters between P and Q. P + Q is not defined.
Set Operators
The following operators take sets as operands.
Set Operators
398
• For an ordinal O and a set S, O in S is True just in case O is a member of S.
Relational Operators
Relational operators are used to compare two operands. The operators =, <>, <=, and >= also apply to
sets.
Relational Operators
> greater-than simple, string, packed string, PChar Boolean Len > 0
<= less-than-or-equal-to simple, string, packed string, PChar Boolean Cnt <= I
For most simple types, comparison is straightforward. For example, I = J is True just in case I and
J have the same value, and I <> J is True otherwise. The following rules apply to relational operators.
• Operands must be of compatible types, except that a real and an integer can be compared.
• Strings are compared according to the ordinal values that make up the characters that make up the
string. Character types are treated as strings of length 1.
• Two packed strings must have the same number of components to be compared. When a packed
string with n components is compared to a string, the packed string is treated as a string of length n.
• Use the operators <, >, <=, and >= to compare PChar (and PWideChar) operands only if the two
pointers point within the same character array.
• The operators = and <> can take operands of class and class-reference types. With operands of a
class type, = and <> are evaluated according the rules that apply to pointers: C = D is True just in
case C and D point to the same instance object, and C <> D is True otherwise. With operands of
a class-reference type, C = D is True just in case C and D denote the same class, and C <> D is
True otherwise. This does not compare the data stored in the classes. For more information about
classes, see Classes and objects.
Class Operators
The operators as and is take classes and instance objects as operands; as operates on interfaces as
well. For more information, see Classes and objects and Object interfaces.
399
The relational operators = and <> also operate on classes.
The @ Operator
The @ operator returns the address of a variable, or of a function, procedure, or method; that is, @
constructs a pointer to its operand. For more information about pointers, see Pointers and pointer types.
The following rules apply to @.
• If X is a variable, @X returns the address of X. (Special rules apply when X is a procedural variable;
see Procedural types in statements and expressions.) The type of @X is Pointer if the default
{$T} compiler directive is in effect. In the {$T+} state, @X is of type ^T, where T is the type of X
(this distinction is important for assignment compatibility, see Assignment-compatibility).
• If F is a routine (a function or procedure), @F returns F's entry point. The type of @F is always Pointer.
• When @ is applied to a method defined in a class, the method identifier must be qualified with the
class name. For example,
@TMyClass.DoSomething
points to the DoSomething method of TMyClass. For more information about classes and methods,
see Classes and objects.
Note: When using the @ operator, it is not possible to take the address of an interface
method as the address is not known at compile time and cannot be extracted at runtime.
Operator Precedence
In complex expressions, rules of precedence determine the order in which operations are performed.
Precedence of operators
Operators Precedence
An operator with higher precedence is evaluated before an operator with lower precedence, while
operators of equal precedence associate to the left. Hence the expression
X + Y * Z
multiplies Y times Z, then adds X to the result; * is performed first, because is has a higher precedence
than +. But
X - Y + Z
400
first subtracts Y from X, then adds Z to the result; - and + have the same precedence, so the operation
on the left is performed first.
You can use parentheses to override these precedence rules. An expression within parentheses is
evaluated first, then treated as a single operand. For example,
(X + Y) * Z
multiplies Z times the sum of X and Y.
Parentheses are sometimes needed in situations where, at first glance, they seem not to be. For
example, consider the expression
X = Y or X = Z
The intended interpretation of this is obviously
(X = Y) or (X = Z)
Without parentheses, however, the compiler follows operator precedence rules and reads it as
(X = (Y or X)) = Z
which results in a compilation error unless Z is Boolean.
Parentheses often make code easier to write and to read, even when they are, strictly speaking,
superfluous. Thus the first example could be written as
X + (Y * Z)
Here the parentheses are unnecessary (to the compiler), but they spare both programmer and reader
from having to think about operator precedence.
Function Calls
Because functions return a value, function calls are expressions. For example, if you've defined a
function called Calc that takes two integer arguments and returns an integer, then the function call
Calc(24,47) is an integer expression. If I and J are integer variables, then I + Calc(J,8) is also
an integer expression. Examples of function calls include
Sum(A, 63)
Maximum(147, J)
Sin(X + Y)
Eof(F)
Volume(Radius, Height)
GetValue
TSomeObject.SomeMethod(I,J);
For more information about functions, see Procedures and functions.
401
Set Constructors
A set constructor denotes a set-type value. For example,
[5, 6, 7, 8]
denotes the set whose members are 5, 6, 7, and 8. The set constructor
[ 5..8 ]
could also denote the same set.
where each item is either an expression denoting an ordinal of the set's base type or a pair of such
expressions with two dots (..) in between. When an item has the form x..y, it is shorthand for all the
ordinals in the range from x to y, including y; but if x is greater than y, then x..y, the set [x..y],
denotes nothing and is the empty set. The set constructor [ ] denotes the empty set, while [x] denotes
the set whose only member is the value of x.
Indexes
Strings, arrays, array properties, and pointers to strings or arrays can be indexed. For example, if
FileName is a string variable, the expression FileName[3] returns the third character in the string
denoted by FileName, while FileName[I + 1] returns the character immediately after the one
indexed by I. For information about strings, see String types. For information about arrays and array
properties, see Arrays and Array properties.
Typecasts
It is sometimes useful to treat an expression as if it belonged to different type. A typecast allows you to
do this by, in effect, temporarily changing an expression's type. For example, Integer('A') casts the
character A as an integer.
typeIdentifier(expression)
402
If the expression is a variable, the result is called a variable typecast; otherwise, the result is a value
typecast. While their syntax is the same, different rules apply to the two kinds of typecast.
Value Typecasts
In a value typecast, the type identifier and the cast expression must both be ordinal or pointer types.
Examples of value typecasts include
Integer('A')
Char(48)
Boolean(0)
Color(2)
Longint(@Buffer)
The resulting value is obtained by converting the expression in parentheses. This may involve truncation
or extension if the size of the specified type differs from that of the expression. The expression's sign is
always preserved.
The statement
I := Integer('A');
assigns the value of Integer('A'), which is 65, to the variable I.
A value typecast cannot be followed by qualifiers and cannot appear on the left side of an assignment
statement.
Variable Typecasts
You can cast any variable to any type, provided their sizes are the same and you do not mix integers
with reals. (To convert numeric types, rely on standard functions like Int and Trunc.) Examples of
variable typecasts include
Char(I)
Boolean(Count)
TSomeDefinedType(MyVariable)
You can cast variables to a procedural type. For example, given the declarations
403
type Func = function(X: Integer): Integer;
var
F: Func;
P: Pointer;
N: Integer;
you can make the following assignments.
type
TByteRec = record
Lo, Hi: Byte;
end;
TWordRec = record
Low, High: Word;
end;
var
B: Byte;
W: Word;
L: Longint;
P: Pointer;
begin
W := $1234;
B := TByteRec(W).Lo;
TByteRec(W).Hi := 0;
L := $1234567;
W := TWordRec(L).Low;
B := TByteRec(TWordRec(L).Low).Hi;
B := PByte(L)^;
end;
In this example, TByteRec is used to access the low- and high-order bytes of a word, and TWordRec
to access the low- and high-order words of a long integer. You could call the predefined functions Lo
and Hi for the same purpose, but a variable typecast has the advantage that it can be used on the left
side of an assignment statement.
404
For information about typecasting pointers, see Pointers and pointer types. For information about casting
class and interface types, see The as operator and Interface typecasts.
405
Data Types, Variables, and Constants
This section describes the fundamental data types of the Delphi language.
406
Data Types, Variables, and Constants
This topic presents a high-level overview of Delphi data types.
About Types
A type is essentially a name for a kind of data. When you declare a variable you must specify its type,
which determines the set of values the variable can hold and the operations that can be performed on
it. Every expression returns data of a particular type, as does every function. Most functions and
procedures require parameters of specific types.
The Delphi language is a 'strongly typed' language, which means that it distinguishes a variety of data
types and does not always allow you to substitute one type for another. This is usually beneficial because
it lets the compiler treat data intelligently and validate your code more thoroughly, preventing hard-to-
diagnose runtime errors. When you need greater flexibility, however, there are mechanisms to
circumvent strong typing. These include typecasting, pointers, Variants, Variant parts in records, and
absolute addressing of variables.
simple
ordinal
integer
character
Boolean
enumerated
subrange
real
407
string
structured
set
array
record
file
class
class reference
interface
pointer
procedural
Variant
type identifier
The standard function SizeOf operates on all variables and type identifiers. It returns an integer
representing the amount of memory (in bytes) required to store data of the specified type. For example,
SizeOf(Longint) returns 4, since a Longint variable uses four bytes of memory.
Type declarations are illustrated in the topics that follow. For general information about type declarations,
see Declaring types.
408
Simple Types
Simple types - which include ordinal types and real types - define ordered sets of values.
Ordinal Types
Ordinal types include integer, character, Boolean, enumerated, and subrange types. An ordinal type
defines an ordered set of values in which each value except the first has a unique predecessor and each
value except the last has a unique successor. Further, each value has an ordinality which determines
the ordering of the type. In most cases, if a value has ordinality n, its predecessor has ordinality n-1 and
its successor has ordinality n+1.
• For integer types, the ordinality of a value is the value itself.
• Subrange types maintain the ordinalities of their base types.
• For other ordinal types, by default the first value has ordinality 0, the next value has ordinality 1,
and so forth. The declaration of an enumerated type can explicitly override this default.
Several predefined functions operate on ordinal values and type identifiers. The most important of them
are summarized below.
Ord ordinal expression ordinality of expression's value Does not take Int64 arguments.
Pred ordinal expression predecessor of expression's
value
Succ ordinal expression successor of expression's value
High ordinal type identifier or variable ofhighest value in type Also operates on short-string
ordinal type types and arrays.
Low ordinal type identifier or variable oflowest value in type Also operates on short-string
ordinal type types and arrays.
For example, High(Byte) returns 255 because the highest value of type Byte is 255, and Succ(2)
returns 3 because 3 is the successor of 2.
409
The standard procedures Inc and Dec increment and decrement the value of an ordinal variable. For
example, Inc(I) is equivalent to I := Succ(I) and, if I is an integer variable, to I := I + 1.
Integer Types
An integer type represents a subset of the whole numbers. The generic integer types are Integer and
Cardinal; use these whenever possible, since they result in the best performance for the underlying CPU
and operating system. The table below gives their ranges and storage formats for the Delphi compiler.
Generic integer types
Fundamental integer types include Shortint, Smallint, Longint, Int64, Byte, Word, and Longword.
Fundamental integer types
In general, arithmetic operations on integers return a value of type Integer, which is equivalent to the
32-bit Longint. Operations return a value of type Int64 only when performed on one or more Int64
operand. Hence the following code produces incorrect results.
var
I: Integer;
J: Int64;
...
I := High(Integer);
J := I + 1;
To get an Int64 return value in this situation, cast I as Int64:
410
...
J := Int64(I) + 1;
For more information, see Arithmetic operators.
Note: Some standard routines that take integer arguments truncate Int64 values to 32
bits. However, the High, Low, Succ, Pred, Inc, Dec, IntToStr, and IntToHex
routines fully support Int64 arguments. Also, the Round, Trunc, StrToInt64, and
StrToInt64Def functions return Int64 values. A few routines cannot take Int64 values
at all.
When you increment the last value or decrement the first value of an integer type, the result wraps
around the beginning or end of the range. For example, the Shortint type has the range 128..127; hence,
after execution of the code
var I: Shortint;
...
I := High(Shortint);
I := I + 1;
the value of I is 128. If compiler range-checking is enabled, however, this code generates a runtime
error.
Character Types
The fundamental character types are AnsiChar and WideChar. AnsiChar values are byte-sized (8-bit)
characters ordered according to the locale character set which is possibly multibyte. AnsiChar was
originally modeled after the ANSI character set (thus its name) but has now been broadened to refer to
the current locale character set.
WideChar characters use more than one byte to represent every character. In the current
implementations, WideChar is word-sized (16-bit) characters ordered according to the Unicode
character set (note that it could be longer in future implementations). The first 256 Unicode characters
correspond to the ANSI characters.
The generic character type is Char, which is equivalent to AnsiChar on Win32, and to Char on the .NET
platform. Because the implementation of Char is subject to change, it's a good idea to use the standard
function SizeOf rather than a hard-coded constant when writing programs that may need to handle
characters of different sizes.
Note: The WideChar type also maps to Char on the .NET platform.
A string constant of length 1, such as 'A', can denote a character value. The predefined function Chr
returns the character value for any integer in the range of AnsiChar or WideChar; for example, Chr
(65) returns the letter A.
Character values, like integers, wrap around when decremented or incremented past the beginning or
end of their range (unless range-checking is enabled). For example, after execution of the code
411
var
Letter: Char;
I: Integer;
begin
Letter := High(Letter);
for I := 1 to 66 do
Inc(Letter);
end;
Letter has the value A (ASCII 65).
Boolean Types
The four predefined Boolean types are Boolean, ByteBool, WordBool, and LongBool. Boolean is the
preferred type. The others exist to provide compatibility with other languages and operating system
libraries.
A Boolean variable occupies one byte of memory, a ByteBool variable also occupies one byte, a
WordBool variable occupies two bytes (one word), and a LongBool variable occupies four bytes (two
words).
Boolean values are denoted by the predefined constants True and False. The following relationships
hold.
A value of type ByteBool, LongBool, or WordBool is considered True when its ordinality is nonzero. If
such a value appears in a context where a Boolean is expected, the compiler automatically converts
any value of nonzero ordinality to True.
The previous remarks refer to the ordinality of Boolean values, not to the values themselves. In Delphi,
Boolean expressions cannot be equated with integers or reals. Hence, if X is an integer variable, the
statement
if X then ...;
generates a compilation error. Casting the variable to a Boolean type is unreliable, but each of the
following alternatives will work.
412
if X <> 0 then ...; { use longer expression that returns Boolean value }
Enumerated Types
An enumerated type defines an ordered set of values by simply listing identifiers that denote these
values. The values have no inherent meaning. To declare an enumerated type, use the syntax
where typeName and each val are valid identifiers. For example, the declaration
When you declare an enumerated type, you are declaring each val to be a constant of type
typeName. If the val identifiers are used for another purpose within the same scope, naming conflicts
occur. For example, suppose you declare the type
Thing := MyUnit.Click;
A better solution, however, is to choose constant names that are not likely to conflict with other identifiers.
Examples:
type
413
TSound = (tsClick, tsClack, tsClock);
TMyColor = (mcRed, mcBlue, mcGreen, mcYellow, mcOrange);
Answer = (ansYes, ansNo, ansMaybe)
You can use the (val1, ..., valn) construction directly in variable declarations, as if it were a type name:
An enumerated type is, in effect, a subrange whose lowest and highest values correspond to the lowest
and highest ordinalities of the constants in the declaration. In the previous example, the Size type has
11 possible values whose ordinalities range from 5 to 15. (Hence the type array[Size] of Char
represents an array of 11 characters.) Only three of these values have names, but the others are
accessible through typecasts and through routines such as Pred, Succ, Inc, and Dec. In the following
example, "anonymous" values in the range of Size are assigned to the variable X.
var X: Size;
414
X := Small; // Ord(X) = 5
Y := Size(6); // Ord(X) = 6
Inc(X); // Ord(X) = 7
Any value that isn't explicitly assigned an ordinality has ordinality one greater than that of the previous
value in the list. If the first value isn't assigned an ordinality, its ordinality is 0. Hence, given the declaration
Subrange Types
A subrange type represents a subset of the values in another ordinal type (called the base type). Any
construction of the form Low..High, where Low and High are constant expressions of the same ordinal
type and Low is less than High, identifies a subrange type that includes all values between Low and
High. For example, if you declare the enumerated type
type TColors = (Red, Blue, Green, Yellow, Orange, Purple, White, Black);
you can then define a subrange type like
You can use numeric constants and characters (string constants of length 1) to define subrange types:
type
SomeNumbers = -128..127;
Caps = 'A'..'Z';
When you use numeric or character constants to define a subrange, the base type is the smallest integer
or character type that contains the specified range.
The LowerBound..UpperBound construction itself functions as a type name, so you can use it directly
in variable declarations. For example,
415
The ordinality of each value in a subrange is preserved from the base type. (In the first example, if
Color is a variable that holds the value Green, Ord(Color) returns 2 regardless of whether Color
is of type TColors or TMyColors.) Values do not wrap around the beginning or end of a subrange,
even if the base is an integer or character type; incrementing or decrementing past the boundary of a
subrange simply converts the value to the base type. Hence, while
...
I := 99;
Inc(I);
assigns the value 100 to I (unless compiler range-checking is enabled).
The use of constant expressions in subrange definitions introduces a syntactic difficulty. In any type
declaration, when the first meaningful character after = is a left parenthesis, the compiler assumes that
an enumerated type is being defined. Hence the code
const
X = 50;
Y = 10;
type
Scale = (X - Y) * 2..(X + Y) * 2;
produces an error. Work around this problem by rewriting the type declaration to avoid the leading
parenthesis:
type
Scale = 2 * (X - Y)..(X + Y) * 2;
Real Types
A real type defines a set of numbers that can be represented with floating-point notation. The table below
gives the ranges and storage formats for the fundamental real types on the Win32 platform.
Fundamental Win32 real types
416
Type Range Significant digits Size in bytes
The following table shows how the fundamental real types map to .NET framework types.
Fundamental .NET real type mappings
Real48 Deprecated
Single Single
Double Double
ExtendedDouble
Comp Deprecated
Currency Re-implemented as a value type using the Decimal type from the .NET Framework
The generic type Real, in its current implementation, is equivalent to Double (which maps to Double on .
NET).
Generic real types
Note: The six-byte Real48 type was called Real in earlier versions of Object Pascal. If
you are recompiling code that uses the older, six-byte Real type in Delphi, you may
want to change it to Real48. You can also use the {$REALCOMPATIBILITY ON}
compiler directive to turn Real back into the six-byte type.
The following remarks apply to fundamental real types.
• Real48 is maintained for backward compatibility. Since its storage format is not native to the Intel
processor architecture, it results in slower performance than other floating-point types. The Real48
type has been deprecated on the .NET platform.
• Extended offers greater precision than other real types but is less portable. Be careful using
Extended if you are creating data files to share across platforms.
417
• The Comp (computational) type is native to the Intel processor architecture and represents a 64-
bit integer. It is classified as a real, however, because it does not behave like an ordinal type. (For
example, you cannot increment or decrement a Comp value.) Comp is maintained for backward
compatibility only. Use the Int64 type for better performance.
• Currency is a fixed-point data type that minimizes rounding errors in monetary calculations. On the
Win32 platform, it is stored as a scaled 64-bit integer with the four least significant digits implicitly
representing decimal places. When mixed with other real types in assignments and expressions,
Currency values are automatically divided or multiplied by 10000.
418
String Types
This topic describes the string data types available in the Delphi language. The following types are
covered:
• Short strings.
• Long strings.
• Wide (Unicode) strings.
On the Win32 platform, AnsiString, sometimes called the long string, is the preferred type for most
purposes. WideString is the preferred string type on the .NET platform.
String types can be mixed in assignments and expressions; the compiler automatically performs required
conversions. But strings passed by reference to a function or procedure (as var and out parameters)
must be of the appropriate type. Strings can be explicitly cast to a different string type.
The reserved word string functions like a generic type identifier. For example,
var S: string;
creates a variable S that holds a string. On the Win32 platform, the compiler interprets string (when it
appears without a bracketed number after it) as AnsiString. On the .NET platform, the string type maps
to the String class. You can use single byte character strings on the .NET platform, but you must explicitly
declare them to be of type AnsiString.
On the Win32 platform, you can use the {$H-} directive to turn string into ShortString. The {$H-}
directive is deprecated on the .NET platform.
The standard function Length returns the number of characters in a string. The SetLength procedure
adjusts the length of a string.
Comparison of strings is defined by the ordering of the characters in corresponding positions. Between
strings of unequal length, each character in the longer string without a corresponding character in the
419
shorter string takes on a greater-than value. For example, 'AB' is greater than 'A'; that is, 'AB' > 'A' returns
True. Zero-length strings hold the lowest values.
You can index a string variable just as you would an array. If S is a string variable and i an integer
expression, S[i] represents the ith character - or, strictly speaking, the ith byte in S. For a ShortString
or AnsiString, S[i] is of type AnsiChar; for a WideString, S[i] is of type WideChar. For single-byte
(Western) locales, MyString[2] := 'A'; assigns the value A to the second character of
MyString. The following code uses the standard AnsiUpperCase function to convert MyString to
uppercase.
var I: Integer;
begin
I := Length(MyString);
while I > 0 do
begin
MyString[I] := AnsiUpperCase(MyString[I]);
I := I - 1;
end;
end;
Be careful indexing strings in this way, since overwriting the end of a string can cause access violations.
Also, avoid passing long-string indexes as var parameters, because this results in inefficient code.
You can assign the value of a string constant - or any other expression that returns a string - to a variable.
The length of the string changes dynamically when the assignment is made. Examples:
Short Strings
A ShortString is 0 to 255 characters long. While the length of a ShortString can change dynamically, its
memory is a statically allocated 256 bytes; the first byte stores the length of the string, and the remaining
255 bytes are available for characters. If S is a ShortString variable, Ord(S[0]), like Length(S),
returns the length of S; assigning a value to S[0], like calling SetLength, changes the length of S.
ShortString is maintained for backward compatibility only.
The Delphi language supports short-string types - in effect, subtypes of ShortString - whose maximum
length is anywhere from 0 to 255 characters. These are denoted by a bracketed numeral appended to
the reserved word string. For example,
420
var MyString: string[100];
creates a variable called MyString whose maximum length is 100 characters. This is equivalent to the
declarations
When you assign a value to a short-string variable, the string is truncated if it exceeds the maximum
length for the type.
The standard functions High and Low operate on short-string type identifiers and variables. High returns
the maximum length of the short-string type, while Low returns zero.
Long Strings
AnsiString, also called a long string, represents a dynamically allocated string whose maximum length
is limited only by available memory.
A long-string variable is a pointer occupying four bytes of memory. When the variable is empty - that is,
when it contains a zero-length stringthe pointer is nil and the string uses no additional storage. When
the variable is nonempty, it points a dynamically allocated block of memory that contains the string value.
The eight bytes before the location contain a 32-bit length indicator and a 32-bit reference count. This
memory is allocated on the heap, but its management is entirely automatic and requires no user code.
Because long-string variables are pointers, two or more of them can reference the same value without
consuming additional memory. The compiler exploits this to conserve resources and execute
assignments faster. Whenever a long-string variable is destroyed or assigned a new value, the reference
count of the old string (the variable's previous value) is decremented and the reference count of the new
value (if there is one) is incremented; if the reference count of a string reaches zero, its memory is
deallocated. This process is called reference-counting. When indexing is used to change the value of a
single character in a string, a copy of the string is made if - but only if - its reference count is greater
than one. This is called copy-on-write semantics.
WideString
The WideString type represents a dynamically allocated string of 16-bit Unicode characters. In most
respects it is similar to AnsiString. On Win32, WideString is compatible with the COM BSTR type.
Note: Under Win32, WideString values are not reference-counted.
The Win32 platform supports single-byte and multibyte character sets as well as Unicode. With a single-
byte character set (SBCS), each byte in a string represents one character.
421
In a multibyte character set (MBCS), some characters are represented by one byte and others by more
than one byte. The first byte of a multibyte character is called the lead byte. In general, the lower 128
characters of a multibyte character set map to the 7-bit ASCII characters, and any byte whose ordinal
value is greater than 127 is the lead byte of a multibyte character. The null value (#0) is always a single-
byte character. Multibyte character sets - especially double-byte character sets (DBCS) - are widely
used for Asian languages.
In the Unicode character set, each character is represented by two bytes. Thus a Unicode string is a
sequence not of individual bytes but of two-byte words. Unicode characters and strings are also called
wide characters and wide character strings. The first 256 Unicode characters map to the ANSI character
set. The Windows operating system supports Unicode (UCS-2).
The Delphi language supports single-byte and multibyte characters and strings through the Char, PChar,
AnsiChar, PAnsiChar, and AnsiString types. Indexing of multibyte strings is not reliable, since S[i]
represents the ith byte (not necessarily the ith character) in S. However, the standard string-handling
functions have multibyte-enabled counterparts that also implement locale-specific ordering for
characters. (Names of multibyte functions usually start with Ansi-. For example, the multibyte version
of StrPos is AnsiStrPos.) Multibyte character support is operating-system dependent and based on
the current locale.
Delphi supports Unicode characters and strings through the WideChar, PWideChar, and WideString
types.
For example, the following type declarations could be used to store null-terminated strings.
type
TIdentifier = array[0..15] of Char;
TFileName = array[0..259] of Char;
TMemoText = array[0..1023] of WideChar;
With extended syntax enabled ({$X+}), you can assign a string constant to a statically allocated zero-
based character array. (Dynamic arrays won't work for this purpose.) If you initialize an array constant
with a string that is shorter than the declared length of the array, the remaining characters are set to #0.
422
Using Pointers, Arrays, and String Constants
To manipulate null-terminated strings, it is often necessary to use pointers. (See Pointers and pointer
types.) String constants are assignment-compatible with the PChar and PWideChar types, which
represent pointers to null-terminated arrays of Char and WideChar values. For example,
var P: PChar;
...
P := 'Hello world!'
points P to an area of memory that contains a null-terminated copy of 'Hello world!' This is equivalent to
const
Message: PChar = 'Program terminated';
Prompt: PChar = 'Enter values: ';
Digits: array[0..9] of PChar = ('Zero', 'One', 'Two', 'Three', 'Four',
'Five', 'Six', 'Seven', 'Eight', 'Nine');
Zero-based character arrays are compatible with PChar and PWideChar. When you use a character
array in place of a pointer value, the compiler converts the array to a pointer constant whose value
corresponds to the address of the first element of the array. For example,
var
MyArray: array[0..32] of Char;
MyPointer: PChar;
begin
MyArray := 'Hello';
MyPointer := MyArray;
SomeProcedure(MyArray);
SomeProcedure(MyPointer);
end;
This code calls SomeProcedure twice with the same value.
423
A character pointer can be indexed as if it were an array. In the previous example, MyPointer[0]
returns H. The index specifies an offset added to the pointer before it is dereferenced. (For PWideChar
variables, the index is automatically multiplied by two.) Thus, if P is a character pointer, P[0] is
equivalent to P^ and specifies the first character in the array, P[1] specifies the second character in
the array, and so forth; P[-1] specifies the 'character' immediately to the left of P[0]. The compiler
performs no range checking on these indexes.
The StrUpper function illustrates the use of pointer indexing to iterate through a null-terminated string:
In a binary operation, if one operand is a long string and the other a PChar, the PChar operand is
converted to a long string.
You can cast a PChar value as a long string. This is useful when you want to perform a string operation
on two PChar values. For example,
S := string(P1) + string(P2);
You can also cast a long string as a null-terminated string. The following rules apply.
• If S is a long-string expression, PChar(S) casts S as a null-terminated string; it returns a pointer
to the first character in S. For example, if Str1 and Str2 are long strings, you could call the Win32
API MessageBox function like this: MessageBox(0, PChar(Str1), PChar(Str2),
MB_OK);
• You can also use Pointer(S) to cast a long string to an untyped pointer. But if S is empty, the
typecast returns nil.
424
• PChar(S) always returns a pointer to a memory block; if S is empty, a pointer to #0 is returned.
• When you cast a long-string variable to a pointer, the pointer remains valid until the variable is
assigned a new value or goes out of scope. If you cast any other long-string expression to a pointer,
the pointer is valid only within the statement where the typecast is performed.
• When you cast a long-string expression to a pointer, the pointer should usually be considered read-
only. You can safely use the pointer to modify the long string only when all of the following conditions
are satisfied.
• The expression cast is a long-string variable.
• The string is not empty.
• The string is unique - that is, has a reference count of one. To guarantee that the string is unique,
call the SetLength, SetString, or UniqueString procedure.
• The string has not been modified since the typecast was made.
• The characters modified are all within the string. Be careful not to use an out-of-range index on the
pointer.
The same rules apply when mixing WideString values with PWideChar values.
425
Structured Types
Instances of a structured type hold more than one value. Structured types include sets, arrays, records,
and files as well as class, class-reference, and interface types. Except for sets, which hold ordinal values
only, structured types can contain other structured types; a type can have unlimited levels of structuring.
By default, the values in a structured type are aligned on word or double-word boundaries for faster
access. When you declare a structured type, you can include the reserved word packed to implement
compressed data storage. For example, type TNumbers = packed array [1..100] of Real;
Using packed slows data access and, in the case of a character array, affects type compatibility (for
more information, see Memory management).
Sets
A set is a collection of values of the same ordinal type. The values have no inherent order, nor is it
meaningful for a value to be included twice in a set.
The range of a set type is the power set of a specific ordinal type, called the base type; that is, the
possible values of the set type are all the subsets of the base type, including the empty set. The base
type can have no more than 256 possible values, and their ordinalities must fall between 0 and 255.
Any construction of the form
set of baseType
Because of the size limitations for base types, set types are usually defined with subranges. For
example, the declarations
type
TSomeInts = 1..250;
TIntSet = set of TSomeInts;
create a set type called TIntSet whose values are collections of integers in the range from 1 to 250.
You could accomplish the same thing with
426
var Set1, Set2: TIntSet;
...
Set1 := [1, 3, 5, 7, 9];
Set2 := [2, 4, 6, 8, 10]
You can also use the set of ... construction directly in variable declarations:
set of Byte
set of (Club, Diamond, Heart, Spade)
set of Char;
The in operator tests set membership:
Arrays
An array represents an indexed collection of elements of the same type (called the base type). Because
each element has a unique index, arrays, unlike sets, can meaningfully contain the same value more
than once. Arrays can be allocated statically or dynamically.
Static Arrays
Static array types are denoted by constructions of the form
where each indexType is an ordinal type whose range does not exceed 2GB. Since the indexTypes
index the array, the number of elements an array can hold is limited by the product of the sizes of the
indexTypes. In practice, indexTypes are usually integer subranges.
In the simplest case of a one-dimensional array, there is only a single indexType. For example,
427
A multidimensional array is an array of arrays. For example,
A one-dimensional, packed, static array of Char values is called a packed string. Packed-string types
are compatible with string types and with other packed-string types that have the same number of
elements. See Type compatibility and identity.
An array type of the form array[0..x] of Char is called a zero-based character array. Zero-based
character arrays are used to store null-terminated strings and are compatible with PChar values. See
Working with null-terminated strings.
Dynamic Arrays
Dynamic arrays do not have a fixed size or length. Instead, memory for a dynamic array is reallocated
when you assign a value to the array or pass it to the SetLength procedure. Dynamic-array types are
denoted by constructions of the form
array of baseType
For example,
SetLength(MyFlexibleArray, 20);
428
allocates an array of 20 reals, indexed 0 to 19. Dynamic arrays are always integer-indexed, always
starting from 0.
Dynamic-array variables are implicitly pointers and are managed by the same reference-counting
technique used for long strings. To deallocate a dynamic array, assign nil to a variable that references
the array or pass the variable to Finalize; either of these methods disposes of the array, provided
there are no other references to it. Dynamic arrays are automatically released when their reference-
count drops to zero. Dynamic arrays of length 0 have the value nil. Do not apply the dereference operator
(^) to a dynamic-array variable or pass it to the New or Dispose procedure.
If X and Y are variables of the same dynamic-array type, X := Y points X to the same array as Y. (There
is no need to allocate memory for X before performing this operation.) Unlike strings and static
arrays, copy-on-write is not employed for dynamic arrays, so they are not automatically copied before
they are written to. For example, after this code executes,
var
A, B: array of Integer;
begin
SetLength(A, 1);
A[0] := 1;
B := A;
B[0] := 2;
end;
the value of A[0] is 2. (If A and B were static arrays, A[0] would still be 1.)
In contrast, to make an independent copy of a dynamic array, you must use the global Copy function:
var
A, B: array of Integer;
begin
SetLength(A, 1);
A[0] := 1;
B := Copy(A);
B[0] := 2; { B[0] <> A[0] }
end;
When dynamic-array variables are compared, their references are compared, not their array values.
Thus, after execution of the code
var
A, B: array of Integer;
begin
429
SetLength(A, 1);
SetLength(B, 1);
A[0] := 2;
B[0] := 2;
end;
A = B returns False but A[0] = B[0] returns True.
To truncate a dynamic array, pass it to SetLength, or pass it to Copy and assign the result back to the
array variable. (The SetLength procedure is usually faster.) For example, if A is a dynamic array, A :
= SetLength(A, 0, 20) truncates all but the first 20 elements of A.
Once a dynamic array has been allocated, you can pass it to the standard functions Length, High, and
Low. Length returns the number of elements in the array, High returns the array's highest index (that
is, Length - 1), and Low returns 0. In the case of a zero-length array, High returns 1 (with the
anomalous consequence that High < Low).
Note: In some function and procedure declarations, array parameters are represented
as array of baseType, without any index types specified. For example,function
CheckStrings(A: array of string): Boolean;
This indicates that the function operates on all arrays of the specified base type, regardless of their size,
how they are indexed, or whether they are allocated statically or dynamically.
SetLength(Msgs,I,J);
allocates an I-by-J array, and Msgs[0,0] denotes an element of that array.
You can create multidimensional dynamic arrays that are not rectangular. The first step is to call
SetLength, passing it parameters for the first n dimensions of the array. For example,
SetLength(Ints[2], 5);
430
makes the third column of Ints five integers long. At this point (even if the other columns haven't been
allocated) you can assign values to the third column - for example, Ints[2,4] := 6.
The following example uses dynamic arrays (and the IntToStr function declared in the SysUtils unit)
to create a triangular matrix of strings.
var
A : array of array of string;
I, J : Integer;
begin
SetLength(A, 10);
for I := Low(A) to High(A) do
begin
SetLength(A[I], I);
for J := Low(A[I]) to High(A[I]) do
A[I,J] := IntToStr(I) + ',' + IntToStr(J) + ' ';
end;
end;
var
Int1: array[1..10] of Integer;
array[1..10] of Integer;
...
Int1 := Int2;
To make the assignment work, declare the variables as
431
Records
A record (analogous to a structure in some languages) represents a heterogeneous set of elements.
Each element is called a field; the declaration of a record type specifies a name and type for each field.
The syntax of a record type declaration is
For example, the following declaration creates a record type called TDateRec.
type
TDateRec = record
Year: Integer;
Month: (Jan, Feb, Mar, Apr, May, Jun,
Jul, Aug, Sep, Oct, Nov, Dec);
Day: 1..31;
end;
Each TDateRec contains three fields: an integer value called Year, a value of an enumerated type
called Month, and another integer between 1 and 31 called Day. The identifiers Year, Month, and
Day are the field designators for TDateRec, and they behave like variables. The TDateRec type
declaration, however, does not allocate any memory for the Year, Month, and Day fields; memory is
allocated when you instantiate the record, like this:
You can access the fields of a record by qualifying the field designators with the record's name:
Record1.Year := 1904;
Record1.Month := Jun;
Record1.Day := 16;
Or use a with statement:
with Record1 do
begin
Year := 1904;
432
Month := Jun;
Day := 16;
end;
You can now copy the values of Record1's fields to Record2:
Record2 := Record1;
Because the scope of a field designator is limited to the record in which it occurs, you don't have to worry
about naming conflicts between field designators and other variables.
Instead of defining record types, you can use the record ... construction directly in variable
declarations:
var S: record
Name: string;
Age: Integer;
end;
However, a declaration like this largely defeats the purpose of records, which is to avoid repetitive coding
of similar groups of variables. Moreover, separately declared records of this kind will not be assignment-
compatible, even if their structures are identical.
To declare a record type with a variant part, use the following syntax.
433
• Each variant is a semicolon-delimited list of declarations resembling the fieldList: type constructions
in the main part of the record type. That is, a variant has the form
fieldList1: type1;
...
fieldListn: typen;
where each fieldList is a valid identifier or comma-delimited list of identifiers, each type denotes a type,
and the final semicolon is optional. The types must not be long strings, dynamic arrays, variants (that
is, Variant types), or interfaces, nor can they be structured types that contain long strings, dynamic
arrays, variants, or interfaces; but they can be pointers to these types.
Records with variant parts are complicated syntactically but deceptively simple semantically. The variant
part of a record contains several variants which share the same space in memory. You can read or write
to any field of any variant at any time; but if you write to a field in one variant and then to a field in another
variant, you may be overwriting your own data. The tag, if there is one, functions as an extra field (of
type ordinalType) in the non-variant part of the record.
Variant parts have two purposes. First, suppose you want to create a record type that has fields for
different kinds of data, but you know that you will never need to use all of the fields in a single record
instance. For example,
type
TEmployee = record
FirstName, LastName: string[40];
BirthDate: TDate;
case Salaried: Boolean of
True: (AnnualSalary: Currency);
False: (HourlyWage: Currency);
end;
The idea here is that every employee has either a salary or an hourly wage, but not both. So when you
create an instance of TEmployee, there is no reason to allocate enough memory for both fields. In this
case, the only difference between the variants is in the field names, but the fields could just as easily
have been of different types. Consider some more complicated examples:
type
TPerson = record
FirstName, LastName: string[40];
BirthDate: TDate;
case Citizen: Boolean of
True: (Birthplace: string[40]);
False: (Country: string[20];
EntryPort: string[20];
EntryDate, ExitDate: TDate);
434
end;
type
TShapeList = (Rectangle, Triangle, Circle, Ellipse, Other);
TFigure = record
case TShapeList of
Rectangle: (Height, Width: Real);
Triangle: (Side1, Side2, Angle: Real);
Circle: (Radius: Real);
Ellipse, Other: ();
end;
For each record instance, the compiler allocates enough memory to hold all the fields in the largest
variant. The optional tag and the constantLists (like Rectangle, Triangle, and so forth in the last
example) play no role in the way the compiler manages the fields; they are there only for the convenience
of the programmer.
The second reason for variant parts is that they let you treat the same data as belonging to different
types, even in cases where the compiler would not allow a typecast. For example, if you have a 64-bit
Real as the first field in one variant and a 32-bit Integer as the first field in another, you can assign a
value to the Real field and then read back the first 32 bits of it as the value of the Integer field (passing
it, say, to a function that requires integer parameters).
File Types
A file a sequence of elements of the same type. Standard I/O routines use the predefined TextFile
or Text type, which represents a file containing characters organized into lines. For more information
about file input and output, see Standard routines and I/O.
where fileTypeName is any valid identifier and type is a fixed-size type. Pointer types - whether implicit
or explicit - are not allowed, so a file cannot contain dynamic arrays, long strings, classes, objects,
pointers, variants, other files, or structured types that contain any of these.
For example,
type
PhoneEntry = record
FirstName, LastName: string[20];
PhoneNumber: string[15];
Listed: Boolean;
435
end;
PhoneList = file of PhoneEntry;
declares a file type for recording names and telephone numbers.
You can also use the file of ... construction directly in a variable declaration. For example,
436
Pointers and Pointer Types
A pointer is a variable that denotes a memory address. When a pointer holds the address of another
variable, we say that it points to the location of that variable in memory or to the data stored there. In
the case of an array or other structured type, a pointer holds the address of the first element in the
structure. If that address is already taken, then the pointer holds the address to the first element.
Pointers are typed to indicate the kind of data stored at the addresses they hold. The general-purpose
Pointer type can represent a pointer to any data, while more specialized pointer types reference only
specific types of data. Pointers occupy four bytes of memory.
Overview of pointers
To see how pointers work, look at the following example.
1 var
2 X, Y: Integer; // X and Y are Integer variables
3 P: ^Integer // P points to an Integer
4 begin
5 X := 17; // assign a value to X
6 P := @X; // assign the address of X to P
7 Y := P^; // dereference P; assign the result to Y
8 end;
Line 2 declares X and Y as variables of type Integer. Line 3 declares P as a pointer to an Integer value;
this means that P can point to the location of X or Y. Line 5 assigns a value to X, and line 6 assigns the
address of X (denoted by @X) to P. Finally, line 7 retrieves the value at the location pointed to by P
(denoted by ^P) and assigns it to Y. After this code executes, X and Y have the same value, namely 17.
The @ operator, which we have used here to take the address of a variable, also operates on functions
and procedures. For more information, see The @ operator and Procedural types in statements and
expressions.
The symbol ^ has two purposes, both of which are illustrated in our example. When it appears before
a type identifier
^typeName
it denotes a type that represents pointers to variables of type typeName. When it appears after a pointer
variable
437
pointer^
it dereferences the pointer; that is, it returns the value stored at the memory address held by the pointer.
Our example may seem like a roundabout way of copying the value of one variable to another -
something that we could have accomplished with a simple assignment statement. But pointers are useful
for several reasons. First, understanding pointers will help you to understand the Delphi language, since
pointers often operate behind the scenes in code where they don't appear explicitly. Any data type that
requires large, dynamically allocated blocks of memory uses pointers. Long-string variables, for
instance, are implicitly pointers, as are class instance variables. Moreover, some advanced
programming techniques require the use of pointers.
Finally, pointers are sometimes the only way to circumvent Delphi's strict data typing. By referencing a
variable with an all-purpose Pointer, casting the Pointer to a more specific type, and then dereferencing
it, you can treat the data stored by any variable as if it belonged to any type. For example, the following
code assigns data stored in a real variable to an integer variable.
type
PInteger = ^Integer;
var
R: Single;
I: Integer;
P: Pointer;
PI: PInteger;
begin
...
P := @R;
PI := PInteger(P);
I := PI^;
end;
Of course, reals and integers are stored in different formats. This assignment simply copies raw binary
data from R to I, without converting it.
In addition to assigning the result of an @ operation, you can use several standard routines to give a
value to a pointer. The New and GetMem procedures assign a memory address to an existing pointer,
while the Addr and Ptr functions return a pointer to a specified address or variable.
Dereferenced pointers can be qualified and can function as qualifiers, as in the expression P1^.Data^.
The reserved word nil is a special constant that can be assigned to any pointer. When nil is assigned
to a pointer, the pointer doesn't reference anything.
Pointer Types
You can declare a pointer to any type, using the syntax
438
When you define a record or other data type, it's a common practice also to define a pointer to that type.
This makes it easy to manipulate instances of the type without copying large blocks of memory.
Standard pointer types exist for many purposes. The most versatile is Pointer, which can point to data
of any kind. But a Pointer variable cannot be dereferenced; placing the ^ symbol after a Pointer variable
causes a compilation error. To access the data referenced by a Pointer variable, first cast it to another
pointer type and then dereference it.
Character Pointers
The fundamental types PAnsiChar and PWideChar represent pointers to AnsiChar and WideChar
values, respectively. The generic PChar represents a pointer to a Char (that is, in its current
implementation, to an AnsiChar). These character pointers are used to manipulate null-terminated
strings. (See Working with null-terminated strings.)
Type-checked Pointers
The $T compiler directive controls the types of pointer values generated by the @ operator. This directive
takes the form of:
{$T+} or {$T-}
In the {$T-} state, the result type of the @ operator is always an untyped pointer that is compatible
with all other pointer types. When @ is applied to a variable reference in the {$T+} state, the type of
the result is ^T, where T is compatible only with pointers to the type of the variable.
439
PVarRec TVarRec (declared in System)
PVariant Variant
PWideString WideString
PWordArray TWordArray (declared in SysUtils). Used to typecast dynamically
allocated memory for arrays of 2-byte values.
440
Procedural Types
Procedural types allow you to treat procedures and functions as values that can be assigned to variables
or passed to other procedures and functions.
type
TIntegerFunction = function: Integer;
TProcedure = procedure;
TStrProc = procedure(const S: string);
TMathFunc = function(X: Double): Double;
var
F: TIntegerFunction; { F is a parameterless function that returns an
integer }
Proc: TProcedure; { Proc is a parameterless procedure }
SP: TStrProc; { SP is a procedure that takes a string parameter }
M: TMathFunc; { M is a function that takes a Double (real)
parameter and returns a Double }
type
441
TMethod = procedure of object;
TNotifyEvent = procedure(Sender: TObject) of object;
These types represent method pointers. A method pointer is really a pair of pointers; the first stores the
address of a method, and the second stores a reference to the object the method belongs to. Given the
declarations
type
TNotifyEvent = procedure(Sender: TObject) of object;
TMainForm = class(TForm)
procedure ButtonClick(Sender: TObject);
...
end;
var
MainForm: TMainForm;
OnClick: TNotifyEvent
we could make the following assignment.
OnClick := MainForm.ButtonClick;
Two procedural types are compatible if they have
• the same calling convention,
• the same return value (or no return value), and
• the same number of parameters, with identically typed parameters in corresponding positions.
(Parameter names do not matter.)
Procedure pointer types are always incompatible with method pointer types. The value nil can be
assigned to any procedural type.
Nested procedures and functions (routines declared within other routines) cannot be used as procedural
values, nor can predefined procedures and functions. If you want to use a predefined routine like
Length as a procedural value, write a wrapper for it:
442
var
F: function(X: Integer): Integer;
I: Integer;
function SomeFunction(X: Integer): Integer;
...
F := SomeFunction; // assign SomeFunction to F
I := F(4); // call function; assign result to I
In assignment statements, the type of the variable on the left determines the interpretation of procedure
or method pointers on the right. For example,
var
F, G: function: Integer;
I: Integer;
function SomeFunction: Integer;
...
F := SomeFunction; // assign SomeFunction to F
G := F; // copy F to G
I := G; // call function; assign result to I
The first statement assigns a procedural value to F. The second statement copies that value to another
variable. The third statement makes a call to the referenced function and assigns the result to
I. Because I is an integer variable, not a procedural one, the last assignment actually calls the function
(which returns an integer).
In some situations it is less clear how a procedural variable should be interpreted. Consider the
statement
To get the memory address of a procedural variable (rather than the address stored in it), use @@. For
example, @@F returns the address of F.
The @ operator can also be used to assign an untyped pointer value to a procedural variable. For
example,
443
var StrComp: function(Str1, Str2: PChar): Integer;
...
@StrComp := GetProcAddress(KernelHandle, 'lstrcmpi');
calls the GetProcAddress function and points StrComp to the result.
Any procedural variable can hold the value nil, which means that it points to nothing. But attempting to
call a nil-valued procedural variable is an error. To test whether a procedural variable is assigned, use
the standard function Assigned:
444
Variant Types
This topic discusses the use of variant data types.
Variants Overview
Sometimes it is necessary to manipulate data whose type varies or cannot be determined at compile
time. In these cases, one option is to use variables and parameters of type Variant, which represent
values that can change type at runtime. Variants offer greater flexibility but consume more memory than
regular variables, and operations on them are slower than on statically bound types. Moreover, illicit
operations on variants often result in runtime errors, where similar mistakes with regular variables would
have been caught at compile time. You can also create custom variant types.
By default, Variants can hold values of any type except records, sets, static arrays, files, classes, class
references, and pointers. In other words, variants can hold anything but structured types and pointers.
They can hold interfaces, whose methods and properties can be accessed through them. (See Object
interfaces.) They can hold dynamic arrays, and they can hold a special kind of static array called a variant
array. (See Variant arrays.) Variants can mix with other variants and with integer, real, string, and
Boolean values in expressions and assignments; the compiler automatically performs type conversions.
Variants that contain strings cannot be indexed. That is, if V is a variant that holds a string value, the
construction V[1] causes a runtime error.
You can define custom Variants that extend the Variant type to hold arbitrary values. For example, you
can define a Variant string type that allows indexing or that holds a particular class reference, record
type, or static array. Custom Variant types are defined by creating descendants to the
TCustomVariantType class.
Note: This, and almost all variant functionality, is implemented in the Variants unit.
A variant occupies 16 bytes of memory and consists of a type code and a value, or pointer to a value,
of the type specified by the code. All variants are initialized on creation to the special value Unassigned.
The special value Null indicates unknown or missing data.
The standard function VarType returns a variant's type code. The varTypeMask constant is a bit mask
used to extract the code from VarType's return value, so that, for example,
445
be used to change the internal representation of a variant. The following code demonstrates the use of
variants and some of the automatic conversions performed when variants are mixed with other types.
var
V1, V2, V3, V4, V5: Variant;
I: Integer;
D: Double;
S: string;
begin
V1 := 1; { integer value }
V2 := 1234.5678; { real value }
V3 := 'Hello world!'; { string value }
V4 := '1000'; { string value }
V5 := V1 + V2 + V4; { real value 2235.5678}
I := V1; { I = 1 (integer value) }
D := V2; { D = 1234.5678 (real value) }
S := V3; { S = 'Hello world!' (string value) }
I := V4; { I = 1000 (integer value) }
S := V5; { S = '2235.5678' (string value) }
end;
The compiler performs type conversions according to the following rules.
Variant type conversion rules
446
Out-of-range assignments often result in the target variable getting the highest value in its range. Invalid
variant operations, assignments or casts raise an EVariantError exception or an exception class
decending from EVariantError.
Special conversion rules apply to the TDateTime type declared in the System unit. When a TDateTime
is converted to any other type, it treated as a normal Double. When an integer, real, or Boolean is
converted to a TDateTime, it is first converted to a Double, then read as a date-time value. When a string
is converted to a TDateTime, it is interpreted as a date-time value using the regional settings. When an
Unassigned value is converted to TDateTime, it is treated like the real or integer value 0. Converting
a Null value to TDateTime raises an exception.
On the Win32 platform, if a variant references a COM interface, any attempt to convert it reads the
object's default property and converts that value to the requested type. If the object has no default
property, an exception is raised.
Variants in Expressions
All operators except ^, is, and in take variant operands. Except for comparisons, which always return a
Boolean result, any operation on a variant value returns a variant result. If an expression combines
variants with statically-typed values, the statically-typed values are automatically converted to variants.
This is not true for comparisons, where any operation on a Null variant produces a Null variant. For
example:
V := Null + 3;
assigns a Null variant to V. By default, comparisons treat the Null variant as a unique value that is
less than any other value. For example:
Variant Arrays
You cannot assign an ordinary static array to a variant. Instead, create a variant array by calling either
of the standard functions VarArrayCreate or VarArrayOf. For example,
V: Variant;
...
V := VarArrayCreate([0,9], varInteger);
creates a variant array of integers (of length 10) and assigns it to the variant V. The array can be indexed
using V[0], V[1], and so forth, but it is not possible to pass a variant array element as a var parameter.
Variant arrays are always indexed with integers.
447
The second parameter in the call to VarArrayCreate is the type code for the array's base type. For
a list of these codes, see VarType. Never pass the code varString to VarArrayCreate; to create
a variant array of strings, use varOleStr.
Variants can hold variant arrays of different sizes, dimensions, and base types. The elements of a variant
array can be of any type allowed in variants except ShortString and AnsiString, and if the base type of
the array is Variant, its elements can even be heterogeneous. Use the VarArrayRedim function to
resize a variant array. Other standard routines that operate on variant arrays include
VarArrayDimCount, VarArrayLowBound, VarArrayHighBound, VarArrayRef,
VarArrayLock, and VarArrayUnlock.
Note: Variant arrays of custom variants are not supported, as instances of custom
variants can be added to a VarVariant variant array.
When a variant containing a variant array is assigned to another variant or passed as a value parameter,
the entire array is copied. Don't perform such operations unnecessarily, since they are memory-
inefficient.
OleVariant
The OleVariant type exists on both the Windows and Linux platforms. The main difference between
Variant and OleVariant is that Variant can contain data types that only the current application knows
what to do with. OleVariant can only contain the data types defined as compatible with OLE Automation
which means that the data types that can be passed between programs or across the network without
worrying about whether the other end will know how to handle the data.
When you assign a Variant that contains custom data (such as a Delphi string, or a one of the new
custom variant types) to an OleVariant, the runtime library tries to convert the Variant into one of the
OleVariant standard data types (such as a Delphi string converts to an OLE BSTR string). For example,
if a variant containing an AnsiString is assigned to an OleVariant, the AnsiString becomes a WideString.
The same is true when passing a Variant to an OleVariant function parameter.
448
Type Compatibility and Identity
To understand which operations can be performed on which expressions, we need to distinguish several
kinds of compatibility among types and values. These include:
• Type identity
• Type compatibility
• Assignment compatibility
Type Identity
When one type identifier is declared using another type identifier, without qualification, they denote the
same type. Thus, given the declarations
type
T1 = Integer;
T2 = T1;
T3 = Integer;
T4 = T2;
T1, T2, T3, T4, and Integer all denote the same type. To create distinct types, repeat the word type in
the declaration. For example,
Language constructions that function as type names denote a different type each time they occur. Thus
the declarations
type
TS1 = set of Char;
TS2 = set of Char;
create two distinct types, TS1 and TS2. Similarly, the variable declarations
var
S1: string[10];
S2: string[10];
create two variables of distinct types. To create variables of the same type, use
449
type MyString = string[10];
var
S1: MyString;
S2: MyString;
Type Compatibility
Every type is compatible with itself. Two distinct types are compatible if they satisfy at least one of the
following conditions.
• They are both real types.
• They are both integer types.
• One type is a subrange of the other.
• Both types are subranges of the same type.
• Both are set types with compatible base types.
• Both are packed-string types with the same number of characters.
• One is a string type and the other is a string, packed-string, or Char type.
• One type is Variant and the other is an integer, real, string, character, or Boolean type.
• Both are class, class-reference, or interface types, and one type is derived from the other.
• One type is PChar or PWideChar and the other is a zero-based character array of the form array
[0..n] of PChar or PWideChar.
• One type is Pointer (an untyped pointer) and the other is any pointer type.
• Both types are (typed) pointers to the same type and the {$T+} compiler directive is in effect.
• Both are procedural types with the same result type, the same number of parameters, and type-
identity between parameters in corresponding positions.
Assignment Compatibility
Assignment-compatibility is not a symmetric relation. An expression of type T2 can be assigned to a
variable of type T1 if the value of the expression falls in the range of T1 and at least one of the following
conditions is satisfied.
• T1 and T2 are of the same type, and it is not a file type or structured type that contains a file type
at any level.
• T1 and T2 are compatible ordinal types.
• T1 and T2 are both real types.
• T1 is a real type and T2 is an integer type.
• T1 is PChar, PWideChar or any string type and the expression is a string constant.
• T1 and T2 are both string types.
• T1 is a string type and T2 is a Char or packed-string type.
• T1 is a long string and T2 is PChar or PWideChar.
450
• T1 and T2 are compatible packed-string types.
• T1 and T2 are compatible set types.
• T1 and T2 are compatible pointer types.
• T1 and T2 are both class, class-reference, or interface types and T2 is a derived from T1.
• T1 is an interface type and T2 is a class type that implements T1.
• T1 is PChar or PWideChar and T2 is a zero-based character array of the form array[0..n] of
Char (when T1 is PChar) or of WideChar (when T1 is PWideChar).
• T1 and T2 are compatible procedural types. (A function or procedure identifier is treated, in certain
assignment statements, as an expression of a procedural type.)
• T1 is Variant and T2 is an integer, real, string, character, Boolean, interface type or OleVariant type.
• T1 is an OleVariant and T2 is an integer, real, string, character, Boolean, interface, or Variant type.
• T1 is an integer, real, string, character, or Boolean type and T2 is Variant or OleVariant.
• T1 is the IUnknown or IDispatch interface type and T2 is Variant or OleVariant. (The variant's
type code must be varEmpty, varUnknown, or varDispatch if T1 is IUnknown, and
varEmpty or varDispatch if T1 is IDispatch.)
451
Declaring Types
This topic describes the syntax of Delphi type declarations.
where newTypeName is a valid identifier. For example, given the type declaration
var S: TMyString;
A type identifier's scope doesn't include the type declaration itself (except for pointer types). So you
cannot, for example, define a record type that uses itself recursively.
When you declare a type that is identical to an existing type, the compiler treats the new type identifier
as an alias for the old one. Thus, given the declarations
For example,
For var parameters, types of formal and actual must be identical. For example,
type
TMyType = type Integer
procedure p(var t:TMyType);
begin
end;
452
procedure x;
var
m: TMyType;
i: Integer;
begin
p(m); // Works
p(i); // Error! Types of formal and actual must be identical.
end;
Note: This only applies to var parameters, not to const or by-value parameters.
453
Variables
A variable is an identifier whose value can change at runtime. Put differently, a variable is a name for a
location in memory; you can use the name to read or write to the memory location. Variables are like
containers for data, and, because they are typed, they tell the compiler how to interpret the data they
hold.
Declaring Variables
The basic syntax for a variable declaration is
where identifierList is a comma-delimited list of valid identifiers and type is any valid type. For example,
var I: Integer;
declares a variable I of type Integer, while
var X, Y: Real;
declares two variables - X and Y - of type Real.
Consecutive variable declarations do not have to repeat the reserved word var:
var
X, Y, Z: Double;
I, J, K: Integer;
Digit: 0..9;
Okay: Boolean;
Variables declared within a procedure or function are sometimes called local, while other variables are
called global. Global variables can be initialized at the same time they are declared, using the syntax
where constantExpression is any constant expression representing a value of type type. Thus the
declaration
var I: Integer = 7;
is equivalent to the declaration and statement
var I: Integer;
...
I := 7;
454
Multiple variable declarations (such as var X, Y, Z: Real;) cannot include initializations, nor can
declarations of variant and file-type variables.
If you don't explicitly initialize a global variable, the compiler initializes it to 0. Local variables, in contrast,
cannot be initialized in their declarations and their contents are undefined until a value is assigned to
them.
When you declare a variable, you are allocating memory which is freed automatically when the variable
is no longer used. In particular, local variables exist only until the program exits from the function or
procedure in which they are declared. For more information about variables and memory management,
see Memory management.
Absolute Addresses
You can create a new variable that resides at the same address as another variable. To do so, put the
directive absolute after the type name in the declaration of the new variable, followed by the name of
an existing (previously declared) variable. For example,
var
Str: string[32];
StrLen: Byte absolute Str;
specifies that the variable StrLen should start at the same address as Str. Since the first byte of a
short string contains the string's length, the value of StrLen is the length of Str.
You cannot initialize a variable in an absolute declaration or combine absolute with any other directives.
Dynamic Variables
You can create dynamic variables by calling the GetMem or New procedure. Such variables are allocated
on the heap and are not managed automatically. Once you create one, it is your responsibility ultimately
to free the variable's memory; use FreeMem to destroy variables created by GetMem and Dispose to
destroy variables created by New. Other standard routines that operate on dynamic variables include
ReallocMem, AllocMem, Initialize, Finalize, StrAlloc, and StrDispose.
Long strings, wide strings, dynamic arrays, variants, and interfaces are also heap-allocated dynamic
variables, but their memory is managed automatically.
Thread-local Variables
Thread-local (or thread) variables are used in multithreaded applications. A thread-local variable is like
a global variable, except that each thread of execution gets its own private copy of the variable, which
cannot be accessed from other threads. Thread-local variables are declared with threadvar instead of
var. For example,
455
threadvar X: Integer;
Thread-variable declarations
• cannot occur within a procedure or function.
• cannot include initializations.
• cannot specify the absolute directive.
Dynamic variables that are ordinarily managed by the compilerlong strings, wide strings, dynamic arrays,
variants, and interfacescan be declared with threadvar, but the compiler does not automatically free the
heap-allocated memory created by each thread of execution. If you use these data types in thread
variables, it is your responsibility to dispose of their memory from within the thread, before the thread
terminates. For example,
threadvar S: AnsiString;
S := 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
...
S := ''; // free the memory used by S
Note: Use of such constructs is discouraged.
You can free a variant by setting it to Unassigned and an interface or dynamic array by setting it to nil.
456
Declared Constants
Several different language constructions are referred to as 'constants'. There are numeric constants
(also called numerals) like 17, and string constants (also called character strings or string literals) like
'Hello world!'. Every enumerated type defines constants that represent the values of that type. There
are predefined constants like True, False, and nil. Finally, there are constants that, like variables, are
created individually by declaration.
Declared constants are either true constants or typed constants. These two kinds of constant are
superficially similar, but they are governed by different rules and used for different purposes.
True Constants
A true constant is a declared identifier whose value cannot change. For example,
where identifier is any valid identifier and constantExpression is an expression that the compiler can
evaluate without executing your program.
If constantExpression returns an ordinal value, you can specify the type of the declared constant using
a value typecast. For example
457
0..$7F 0..127 0..127
$80..$FF 128..255 Byte
$0100..$7FFF 256..32767 0..32767
$8000..$FFFF 32768..65535 Word
$10000..$7FFFFFFF 65536..2147483647 0..2147483647
$80000000..$FFFFFFFF 2147483648..4294967295 Cardinal
$100000000..$7FFFFFFFFFFFFFFF 4294967296..2^631 Int64
const
Min = 0;
Max = 100;
Center = (Max - Min) div 2;
Beta = Chr(225);
NumChars = Ord('Z') - Ord('A') + 1;
Message = 'Out of memory';
ErrStr = ' Error: ' + Message + '. ';
ErrPos = 80 - Length(ErrStr) div 2;
Ln10 = 2.302585092994045684;
Ln10R = 1 / Ln10;
Numeric = ['0'..'9'];
Alpha = ['A'..'Z', 'a'..'z'];
AlphaNum = Alpha + Numeric;
Constant Expressions
A constant expression is an expression that the compiler can evaluate without executing the program
in which it occurs. Constant expressions include numerals; character strings; true constants; values of
enumerated types; the special constants True, False, and nil; and expressions built exclusively from
these elements with operators, typecasts, and set constructors. Constant expressions cannot include
variables, pointers, or function calls, except calls to the following predefined functions:
This definition of a constant expression is used in several places in Delphi's syntax specification.
Constant expressions are required for initializing global variables, defining subrange types, assigning
ordinalities to values in enumerated types, specifying default parameter values, writing case statements,
and declaring both true and typed constants.
458
Examples of constant expressions:
100
'A'
256 - 1
(2.5 + 1) / (2.5 - 1)
'Borland' + ' ' + 'Developer'
Chr(32)
Ord('Z') - Ord('A') + 1
Resource Strings
Resource strings are stored as resources and linked into the executable or library so that they can be
modified without recompiling the program.
Resource strings are declared like other true constants, except that the word const is replaced by
resourcestring. The expression to the right of the = symbol must be a constant expression and must
return a string value. For example,
resourcestring
CreateError = 'Cannot create file %s';
OpenError = 'Cannot open file %s';
LineTooLong = 'Line too long';
ProductName = 'Borland Rocks';
SomeResourceString = SomeTrueConstant;
Typed Constants
Typed constants, unlike true constants, can hold values of array, record, procedural, and pointer types.
Typed constants cannot occur in constant expressions.
where identifier is any valid identifier, type is any type except files and variants, and value is an
expression of type type. For example,
459
Array Constants
To declare an array constant, enclose the values of the array's elements, separated by commas, in
parentheses at the end of the declaration. These values must be represented by constant expressions.
For example,
const Digits: array[0..9] of Char = ('0', '1', '2', '3', '4', '5', '6', '7',
'8', '9');
declares a typed constant called Digits that holds an array of characters.
Zero-based character arrays often represent null-terminated strings, and for this reason string constants
can be used to initialize character arrays. So the previous declaration can be more conveniently
represented as
Maze[0,0,0] = 0
Maze[0,0,1] = 1
Maze[0,1,0] = 2
Maze[0,1,1] = 3
Maze[1,0,0] = 4
Maze[1,0,1] = 5
Maze[1,1,0] = 6
Maze[1,1,1] = 7
Array constants cannot contain file-type values at any level.
Record Constants
To declare a record constant, specify the value of each field - as fieldName: value, with the field
assignments separated by semicolons - in parentheses at the end of the declaration. The values must
be represented by constant expressions. The fields must be listed in the order in which they appear in
the record type declaration, and the tag field, if there is one, must have a value specified; if the record
has a variant part, only the variant selected by the tag field can be assigned values.
Examples:
type
460
TPoint = record
X, Y: Single;
end;
TVector = array[0..1] of TPoint;
TMonth = (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec);
TDate = record
D: 1..31;
M: TMonth;
Y: 1900..1999;
end;
const
Origin: TPoint = (X: 0.0; Y: 0.0);
Line: TVector = ((X: -3.1; Y: 1.5), (X: 5.8; Y: 3.0));
SomeDay: TDate = (D: 2; M: Dec; Y: 1960);
Record constants cannot contain file-type values at any level.
Procedural Constants
To declare a procedural constant, specify the name of a function or procedure that is compatible with
the declared type of the constant. For example,
I := MyFunction(5, 7)
You can also assign the value nil to a procedural constant.
Pointer Constants
When you declare a pointer constant, you must initialize it to a value that can be resolvedat least as a
relative addressat compile time. There are three ways to do this: with the @ operator, with nil, and (if
the constant is of type PChar or PWideChar) with a string literal. For example, if I is a global variable
of type Integer, you can declare a constant like
461
const PF: Pointer = @MyFunction;
Because string literals are allocated as global constants, you can initialize a PChar constant with a string
literal:
462
Procedures and Functions
This section describes the syntax of function and procedure declarations.
463
Procedures and Functions
This topic covers the following items:
• Declaring procedures and functions
• Calling conventions
• Forward and interface declarations
• Declaration of external routines
• Overloading procedures and functions
• Local declarations and nested routines
Function calls, because they return a value, can be used as expressions in assignments and operations.
For example,
I := SomeFunction(X);
calls SomeFunction and assigns the result to I. Function calls cannot appear on the left side of an
assignment statement.
Procedure calls - and, when extended syntax is enabled ({$X+}), function calls - can be used as
complete statements. For example,
DoSomething;
calls the DoSomething routine; if DoSomething is a function, its return value is discarded.
Procedure Declarations
A procedure declaration has the form
464
localDeclarations;
begin
statements
end;
where procedureName is any valid identifier, statements is a sequence of statements that execute when
the procedure is called, and (parameterList), directives;, and localDeclarations; are optional.
NumString(17, MyString);
This procedure call assigns the value '17' to MyString (which must be a string variable).
Within a procedure's statement block, you can use variables and other identifiers declared in the
localDeclarations part of the procedure. You can also use the parameter names from the parameter list
(like N and S in the previous example); the parameter list defines a set of local variables, so don't try to
redeclare the parameter names in the localDeclarations section. Finally, you can use any identifiers
within whose scope the procedure declaration falls.
Function Declarations
A function declaration is like a procedure declaration except that it specifies a return type and a return
value. Function declarations have the form
465
where functionName is any valid identifier, returnType is a type identifier, statements is a sequence of
statements that execute when the function is called, and (parameterList), directives;, and
localDeclarations; are optional.
The function's statement block is governed by the same rules that apply to procedures. Within the
statement block, you can use variables and other identifiers declared in the localDeclarations part of the
function, parameter names from the parameter list, and any identifiers within whose scope the function
declaration falls. In addition, the function name itself acts as a special variable that holds the function's
return value, as does the predefined variable Result.
As long as extended syntax is enabled ({$X+}), Result is implicitly declared in every function. Do not
try to redeclare it.
For example,
466
function Power(X: Real; Y: Integer): Real;
var
I: Integer;
begin
Result := 1.0;
I := Y;
while I > 0 do
begin
if Odd(I) then Result := Result * X;
I := I div 2;
X := Sqr(X);
end;
end;
Result and the function name always represent the same value. Hence
If the function exits without assigning a value to Result or the function name, then the function's return
value is undefined.
Calling Conventions
When you declare a procedure or function, you can specify a calling convention using one of the
directives register, pascal, cdecl, stdcall, and safecall. For example,
467
• The register and pascal conventions pass parameters from left to right; that is, the left most
parameter is evaluated and passed first and the rightmost parameter is evaluated and passed last.
The cdecl, stdcall, and safecall conventions pass parameters from right to left.
• For all conventions except cdecl, the procedure or function removes parameters from the stack
upon returning. With the cdecl convention, the caller removes parameters from the stack when the
call returns.
• The register convention uses up to three CPU registers to pass parameters, while the other
conventions pass all parameters on the stack.
• The safecall convention implements exception 'firewalls.' On Win32, this implements interprocess
COM error notification.
The default register convention is the most efficient, since it usually avoids creation of a stack frame.
(Access methods for published properties must use register.) The cdecl convention is useful when you
call functions from shared libraries written in C or C++, while stdcall and safecall are recommended, in
general, for calls to external code. On Win32, the operating system APIs are stdcall and safecall. Other
operating systems generally use cdecl. (Note that stdcall is more efficient than cdecl.)
The safecall convention must be used for declaring dual-interface methods. The pascal convention is
maintained for backward compatibility.
The directives near, far, and export refer to calling conventions in 16-bit Windows programming. They
have no effect in Win32, or in .NET applications and are maintained for backward compatibility only.
468
function Calculate;
... { declarations }
begin
... { statement block }
end;
Ordinarily, a defining declaration does not have to repeat the routine's parameter list or return type, but
if it does repeat them, they must match those in the forward declaration exactly (except that default
parameters can be omitted). If the forward declaration specifies an overloaded procedure or function,
then the defining declaration must repeat the parameter list.
A forward declaration and its defining declaration must appear in the same type declaration section.
That is, you can't add a new section (such as a var section or const section) between the forward
declaration and the defining declaration. The defining declaration can be an external or assembler
declaration, but it cannot be another forward declaration.
The purpose of a forward declaration is to extend the scope of a procedure or function identifier to an
earlier point in the source code. This allows other procedures and functions to call the forward-declared
routine before it is actually defined. Besides letting you organize your code more flexibly, forward
declarations are sometimes necessary for mutual recursions.
The forward directive has no effect in the interface section of a unit. Procedure and function headers in
the interface section behave like forward declarations and must have defining declarations in the
implementation section. A routine declared in the interface section is available from anywhere else in
the unit and from any other unit or program that uses the unit where it is declared.
External Declarations
The external directive, which replaces the block in a procedure or function declaration, allows you to
call routines that are compiled separately from your program. External routines can come from object
files or dynamically loadable libraries.
When importing a C function that takes a variable number of parameters, use the varargs directive. For
example,
{$L BLOCK.OBJ}
469
links BLOCK.OBJ into the program or unit in which it occurs. Next, declare the functions and procedures
that you want to call:
On the Win32 platform, declarations like the ones above are frequently used to access external routines
written in assembly language. You can also place assembly-language routines directly in your Delphi
source code.
to the end of a normal procedure or function header, where stringConstant is the name of the library file
in single quotation marks. For example, on Win32
You can import a routine under a different name from the one it has in the library. If you do this, specify
the original name in the external directive:
external stringConstant1namestringConstant2;
where the first stringConstant gives the name of the library file and the second stringConstant is the
routine's original name.
The following declaration imports a function from user32.dll (part of the Win32 API).
Instead of a name, you can use a number to identify the routine you want to import:
externalstringConstantindexintegerConstant;
In your importing declaration, be sure to match the exact spelling and case of the routine's name. Later,
when you call the imported routine, the name is case-insensitive.
470
Overloading Procedures and Functions
You can declare more than one routine in the same scope with the same name. This is called
overloading. Overloaded routines must be declared with the overload directive and must have
distinguishing parameter lists. For example, consider the declarations
You can pass to an overloaded routine parameters that are not identical in type with those in any of the
routine's declarations, but that are assignment-compatible with the parameters in more than one
declaration. This happens most frequently when a routine is overloaded with different integer types or
different real types - for example,
Overloaded routines must be distinguished by the number of parameters they take or the types of their
parameters. Hence the following pair of declarations causes a compilation error.
471
function Func(X: Integer; Y: Real): Real; overload;
...
are legal.
When an overloaded routine is declared in a forward or interface declaration, the defining declaration
must repeat the routine's parameter list.
The compiler can distinguish between overloaded functions that contain AnsiString/PChar and
WideString/WideChar parameters in the same parameter position. String constants or literals passed
into such an overload situation are translated into the native string or character type, which is AnsiString/
PChar.
This can cause some minor side effects with float types. Float types are matched by size. If there is no
exact match for the float variable passed to the overload call but a variant parameter is available, the
variant is taken over any smaller float type.
For example:
472
foo(1.2); // variant version (float literals -> extended precision)
end;
This example calls the variant version of foo, not the double version, because the 1.2 constant is
implicitly an extended type and extended is not an exact match for double. Extended is also not an exact
match for Variant, but Variant is considered a more general type (whereas double is a smaller type than
extended).
foo(Double(1.2));
This typecast does not work. You should use typed consts instead.
When declaring a set of overloaded routines, the best way to avoid float promotion to variant is to declare
a version of your overloaded function for each float type (Single, Double, Extended) along with the variant
version.
If you use default parameters in overloaded routines, be careful not to introduce ambiguous parameter
signatures.
You can limit the potential effects of overloading by qualifying a routine's name when you call it. For
example, Unit1.MyProcedure(X, Y) can call only routines declared in Unit1; if no routine in
Unit1 matches the name and parameter list in the call, an error results.
Local Declarations
The body of a function or procedure often begins with declarations of local variables used in the routine's
statement block. These declarations can also include constants, types, and other routines. The scope
of a local identifier is limited to the routine where it is declared.
473
Nested Routines
Functions and procedures sometimes contain other functions and procedures within the local-
declarations section of their blocks. For example, the following declaration of a procedure called
DoSomething contains a nested procedure.
begin
...
NestedProc(S);
...
end;
The scope of a nested routine is limited to the procedure or function in which it is declared. In our
example, NestedProc can be called only within DoSomething.
For real examples of nested routines, look at the DateTimeToString procedure, the ScanDate function,
and other routines in the SysUtils unit.
474
Parameters
This topic covers the following items:
• Parameter semantics
• String parameters
• Array parameters
• Default parameters
About Parameters
Most procedure and function headers include a parameter list. For example, in the header
(X, Y: Real)
(var S: string; X: Integer)
(HWnd: Integer; Text, Caption: PChar; Flags: Integer)
(const P; I: Integer)
The parameter list specifies the number, order, and type of parameters that must be passed to the
routine when it is called. If a routine does not take any parameters, omit the identifier list and the
parentheses in its declaration:
procedure UpdateRecords;
begin
...
end;
Within the procedure or function body, the parameter names (X and Y in the first example) can be used
as local variables. Do not redeclare the parameter names in the local declarations section of the
procedure or function body.
Parameter Semantics
Parameters are categorized in several ways:
475
• Every parameter is classified as value, variable, constant, or out. Value parameters are the default;
the reserved words var, const, and out indicate variable, constant, and out parameters, respectively.
• Value parameters are always typed, while constant, variable, and out parameters can be either
typed or untyped.
• Special rules apply to array parameters.
Files and instances of structured types that contain files can be passed only as variable (var) parameters.
var
I, J, V, W: Integer;
begin
I := 4;
V := 4;
J := DoubleByValue(I); // J = 8, I = 4
W := DoubleByRef(V); // W = 8, V = 8
end;
After this code executes, the variable I, which was passed to DoubleByValue, has the same value
we initially assigned to it. But the variable V, which was passed to DoubleByRef, has a different value.
A value parameter acts like a local variable that gets initialized to the value passed in the procedure or
function call. If you pass a variable as a value parameter, the procedure or function creates a copy of
it; changes made to the copy have no effect on the original variable and are lost when program execution
returns to the caller.
476
A variable parameter, on the other hand, acts like a pointer rather than a copy. Changes made to the
parameter within the body of a function or procedure persist after program execution returns to the caller
and the parameter name itself has gone out of scope.
Even if the same variable is passed in two or more var parameters, no copies are made. This is illustrated
in the following example.
var I: Integer;
begin
I := 1;
AddOne(I, I);
end;
After this code executes, the value of I is 3.
If a routine's declaration specifies a var parameter, you must pass an assignable expression - that is, a
variable, typed constant (in the {$J+} state), dereferenced pointer, field, or indexed variableto the
routine when you call it. To use our previous examples, DoubleByRef(7) produces an error, although
DoubleByValue(7) is legal.
Indexes and pointer dereferences passed in var parameters - for example, DoubleByRef(MyArray
[I]) - are evaluated once, before execution of the routine.
Constant Parameters
A constant (const) parameter is like a local constant or read-only variable. Constant parameters are
similar to value parameters, except that you can't assign a value to a constant parameter within the body
of a procedure or function, nor can you pass one as a var parameter to another routine. (But when you
pass an object reference as a constant parameter, you can still modify the object's properties.)
Using const allows the compiler to optimize code for structured - and string-type parameters. It also
provides a safeguard against unintentionally passing a parameter by reference to another routine.
Here, for example, is the header for the CompareStr function in the SysUtils unit:
477
Out Parameters
An out parameter, like a variable parameter, is passed by reference. With an out parameter, however,
the initial value of the referenced variable is discarded by the routine it is passed to. The out parameter
is for output only; that is, it tells the function or procedure where to store output, but doesn't provide any
input.
Out parameters are frequently used with distributed-object models like COM and CORBA. In addition,
you should use out parameters when you pass an uninitialized variable to a function or procedure.
Untyped Parameters
You can omit type specifications when declaring var, const, and out parameters. (Value parameters
must be typed.) For example,
Within a procedure or function body, untyped parameters are incompatible with every type. To operate
on an untyped parameter, you must cast it. In general, the compiler cannot verify that operations on
untyped parameters are valid.
The following example uses untyped parameters in a function called Equal that compares a specified
number of bytes of any two variables.
478
while (N < Size) and (TBytes(Dest)[N] = TBytes(Source)[N]) do
Inc(N);
Equal := N = Size;
end;
Given the declarations
type
TVector = array[1..10] of Integer;
TPoint = record
X, Y: Integer;
end;
var
Vec1, Vec2: TVector;
N: Integer;
P: TPoint;
you could make the following calls to Equal:
String Parameters
When you declare routines that take short-string parameters, you cannot include length specifiers in the
parameter declarations. That is, the declaration
479
Short strings, OpenString, $H, and $P are supported for backward compatibility only. In new code, you
can avoid these considerations by using long strings.
Array Parameters
When you declare routines that take array parameters, you cannot include index type specifiers in the
parameter declarations. That is, the declaration
Since the Delphi language does not implement value semantics for dynamic arrays, 'value' parameters
in routines do not represent a full copy of the dynamic array. In this example
type
TDynamicArray = array of Integer;
procedure p(Value: TDynamicArray);
begin
Value[0] := 1;
end;
procedure Run;
var
a: TDynamicArray;
begin
SetLength(a, 1);
a[0] := 0;
p(a);
Writeln(a[0]); // Prints '1'
end;
Note that the assignment to Value[0] in routine p will modify the content of dynamic array of the caller,
despite Value being a by-value parameter. If a full copy of the dynamic array is required, use the Copy
standard procedure to create a value copy of the dynamic array.
480
function Find(A: array of Char): Integer;
declares a function called Find that takes a character array of any size and returns an integer.
Note: The syntax of open array parameters resembles that of dynamic array types, but
they do not mean the same thing. The previous example creates a function that takes
any array of Char elements, including (but not limited to) dynamic arrays. To declare
parameters that must be dynamic arrays, you need to specify a type identifier:
When you pass an array as an open array value parameter, the compiler creates a local copy of the
array within the routine's stack frame. Be careful not to overflow the stack by passing large arrays.
The following examples use open array parameters to define a Clear procedure that assigns zero to
each element in an array of reals and a Sum function that computes the sum of the elements in an array
of reals.
481
When you call routines that use open array parameters, you can pass open array constructors to them.
The array of const construction is equivalent to array of TVarRec. TVarRec, declared in the
System unit, represents a record with a variant part that can hold values of integer, Boolean, character,
real, string, pointer, class, class reference, interface, and variant types. TVarRec's VType field indicates
the type of each element in the array. Some types are passed as pointers rather than values; in particular,
long strings are passed as Pointer and must be typecast to string.
The following example uses a variant open array parameter in a function that creates a string
representation of each element passed to it and concatenates the results into a single string. The string-
handling routines called in this function are defined in SysUtils.
482
MakeStr(['test', 100, ' ', True, 3.14159, TForm])
returns the string 'test100 T3.14159TForm'.
Default Parameters
You can specify default parameter values in a procedure or function heading. Default values are allowed
only for typed const and value parameters. To provide a default value, end the parameter declaration
with the = symbol followed by a constant expression that is assignment-compatible with the parameter's
type.
FillArray(MyArray);
FillArray(MyArray, 0);
A multiple-parameter declaration cannot specify a default value. Thus, while
Parameters with default values must occur at the end of the parameter list. That is, all parameters
following the first declared default value must also have default values. So the following declaration is
illegal.
F := Resizer;
F(N);
483
result in the values (N, 1.0) being passed to Resizer.
Default parameters are limited to values that can be specified by a constant expression. Hence
parameters of a dynamic-array, procedural, class, class-reference, or interface type can have no value
other than nil as their default. Parameters of a record, variant, file, static-array, or object type cannot
have default values at all.
484
Calling Procedures and Functions
This topic covers the following items:
• Program control and routine parameters
• Open array constructors
When you call a routine that uses default parameter values, all actual parameters following the first
accepted default must also use the default values; calls of the form SomeFunction(,,X) are not legal.
You can omit parentheses when passing all and only the default parameters to a routine. For example,
given the procedure
DoSomething();
DoSomething;
485
An open array constructor, like a set constructor, is a sequence of expressions separated by commas
and enclosed in brackets.
var I, J: Integer;
procedure Add(A: array of Integer);
you could call the Add procedure with the statement
Add([5, 7, I, I + J]);
This is equivalent to
486
Classes and Objects
This section describes the object-oriented features of the Delphi language, such as the declaration and
usage of class types.
487
Classes and Objects
This topic covers the following material:
• Declaration syntax of classes
• Inheritance and scope
• Visibility of class members
• Forward declarations and mutually dependent classes
Class Types
A class, or class type, defines a structure consisting of fields, methods, and properties. Instances of a
class type are called objects. The fields, methods, and properties of a class are called its components
or members.
• A field is essentially a variable that is part of an object. Like the fields of a record, a class' fields
represent data items that exist in each instance of the class.
• A method is a procedure or function associated with a class. Most methods operate on objectsthat
is, instances of a class. Some methods (called class methods) operate on class types themselves.
• A property is an interface to data associated with an object (often stored in a field). Properties have
access specifiers, which determine how their data is read and modified. From other parts of a
programoutside of the object itselfa property appears in most respects like a field.
Objects are dynamically allocated blocks of memory whose structure is determined by their class type.
Each object has a unique copy of every field defined in the class, but all instances of a class share the
same methods. Objects are created and destroyed by special methods called constructors and
destructors.
A variable of a class type is actually a pointer that references an object. Hence more than one variable
can refer to the same object. Like other pointers, class-type variables can hold the value nil. But you
don't have to explicitly dereference a class-type variable to access the object it points to. For example,
SomeObject.Size := 100 assigns the value 100 to the Size property of the object referenced by
SomeObject; you would not write this as SomeObject^.Size := 100.
A class type must be declared and given a name before it can be instantiated. (You cannot define a
class type within a variable declaration.) Declare classes only in the outermost scope of a program or
unit, not in a procedure or function declaration.
type
className = class (ancestorClass)
488
memberList
end;
where className is any valid identifier, (ancestorClass) is optional, and memberList declares members
- that is, fields, methods, and properties - of the class. If you omit (ancestorClass), then the new class
inherits directly from the predefined TObject class. If you include (ancestorClass) and memberList is
empty, you can omit end. A class type declaration can also include a list of interfaces implemented by
the class; see Implementing Interfaces.
Delphi for .NET supports the additional features of sealed classes and abstract classes. A sealed class
is one that cannot be extended through inheritance. This includes all .NET languages that might use
the sealed class. Delphi for .NET also allows an entire class to be declared as abstract, even though it
does not contain any abstract virtual methods. The class declaration syntax for Delphi for .NET is:
type
className = class [abstract | sealed] (ancestorType)
memberList
end;
Methods appear in a class declaration as function or procedure headings, with no body. Defining
declarations for each method occur elsewhere in the program.
For example, here is the declaration of the TMemoryStream class from the Classes unit.
489
Given this declaration, you can create an instance of TMemoryStream as follows:
The scope of a member's identifier starts at the point where the member is declared, continues to the
end of the class declaration, and extends over all descendants of the class and the blocks of all methods
defined in the class and its descendants.
490
Compatibility of Class Types
A class type is assignment-compatible with its ancestors. Hence a variable of a class type can reference
an instance of any descendant type. For example, given the declarations
type
TFigure = class(TObject);
TRectangle = class(TFigure);
TSquare = class(TRectangle);
var
Fig: TFigure;
the variable Fig can be assigned values of type TFigure, TRectangle, and TSquare.
Object Types
The Win32 Delphi compiler allows an alternative syntax to class types, which you can declare object
types using the syntax
Since object types do not descend from TObject, they provide no built-in constructors, destructors, or
other methods. You can create instances of an object type using the New procedure and destroy them
with the Dispose procedure, or you can simply declare variables of an object type, just as you would
with records.
Object types are supported for backward compatibility only. Their use is not recommended on Win32,
and they have been completely deprecated in the Delphi for .NET compiler.
491
If a member's declaration appears without its own visibility specifier, the member has the same visibility
as the one that precedes it. Members at the beginning of a class declaration that don't have a specified
visibility are by default published, provided the class is compiled in the {$M+} state or is derived from
a class compiled in the {$M+} state; otherwise, such members are public.
For readability, it is best to organize a class declaration by visibility, placing all the private members
together, followed by all the protected members, and so forth. This way each visibility reserved word
appears at most once and marks the beginning of a new 'section' of the declaration. So a typical class
declaration should like this:
type
TMyClass = class(TControl)
private
... { private declarations here }
protected
... { protected declarations here }
public
... { public declarations here }
published
... { published declarations here }
end;
You can increase the visibility of a member in a descendant class by redeclaring it, but you cannot
decrease its visibility. For example, a protected property can be made public in a descendant, but not
private. Moreover, published members cannot become public in a descendant class. For more
information, see Property overrides and redeclarations.
A protected member is visible anywhere in the module where its class is declared and from any
descendant class, regardless of the module where the descendant class appears. A protected method
can be called, and a protected field or property read or written to, from the definition of any method
belonging to a class that descends from the one where the protected member is declared. Members
that are intended for use only in the implementation of derived classes are usually protected.
492
Additional Visibility Specifiers for .NET
In addition to private and protected visibility specifiers, the Delphi for .NET compiler supports additional
visibility settings that comply with the .NET Common Language Specification (CLS). These are, strict
private, and strict protected visibility.
Class members with strict private visibility are accessible only within the class in which they are declared.
They are not visible to procedures or functions declared within the same unit.
Class members with strict protected visibility are visible within the class in which they are declared, and
within any descendant class, regardless of where it is declared.
Delphi's traditional private visibility specifier maps to the CLR's assembly visibility. Delphi's protected
visibility specifier maps to the CLR's assembly or family visibility.
Note: The word strict is treated as a directive within the context of a class declaration.
Within a class declaration you cannot declare a member named 'strict', but it is
acceptable for use outside of a class declaration.
Published Members
Published members have the same visibility as public members. The difference is that runtime type
information (RTTI) is generated for published members. RTTI allows an application to query the fields
and properties of an object dynamically and to locate its methods. RTTI is used to access the values of
properties when saving and loading form files, to display properties in the Object Inspector, and to
associate specific methods (called event handlers) with specific properties (called events).
Published properties are restricted to certain data types. Ordinal, string, class, interface, variant, and
method-pointer types can be published. So can set types, provided the upper and lower bounds of the
base type have ordinal values between 0 and 31. (In other words, the set must fit in a byte, word, or
double word.) Any real type except Real48 can be published. Properties of an array type (as distinct
from array properties, discussed below) cannot be published.
Some properties, although publishable, are not fully supported by the streaming system. These include
properties of record types, array properties of all publishable types, and properties of enumerated types
that include anonymous values. If you publish a property of this kind, the Object Inspector won't display
it correctly, nor will the property's value be preserved when objects are streamed to disk.
All methods are publishable, but a class cannot publish two or more overloaded methods with the same
name. Fields can be published only if they are of a class or interface type.
A class cannot have published members unless it is compiled in the {$M+} state or descends from a
class compiled in the {$M+} state. Most classes with published members derive from TPersistent, which
is compiled in the {$M+} state, so it is seldom necessary to use the $M directive.
493
members typically appear only in Win32 classes, and the automated reserved word has been
deprecated in the .NET compiler. The automated reserved word is maintained for backward
compatibility. The TAutoObject class in the ComObj unit does not use automated.
The declaration of an automated method or property can include a dispid directive. Specifying an already
used ID in a dispid directive causes an error.
On the Win32 platform, this directive must be followed by an integer constant that specifies an
Automation dispatch ID for the member. Otherwise, the compiler automatically assigns the member a
dispatch ID that is one larger than the largest dispatch ID used by any method or property in the class
and its ancestors. For more information about Automation (on Win32 only), see Automation objects.
with no ancestor or class members listed after the word class, then it is a forward declaration. A forward
declaration must be resolved by a defining declaration of the same class within the same type declaration
section. In other words, between a forward declaration and its defining declaration, nothing can occur
except other type declarations.
type
TFigure = class; // forward declaration
TDrawing = class
Figure: TFigure;
...
end;
494
Drawing: TDrawing;
...
end;
Do not confuse forward declarations with complete declarations of types that derive from TObject without
declaring any class members.
type
TFirstClass = class; // this is a forward declaration
TSecondClass = class // this is a complete class declaration
end; //
TThirdClass = class(TObject); // this is a complete class declaration
495
Fields
This topic describes the syntax of class data fields declarations.
About Fields
A field is like a variable that belongs to an object. Fields can be of any type, including class types. (That
is, fields can hold object references.) Fields are usually private.
To define a field member of a class, simply declare the field as you would a variable. All field declarations
must occur before any property or method declarations. For example, the following declaration creates
a class called TNumber whose only member, other than the methods is inherits from TObject, is an
integer field called Int.
type
TAncestor = class
Value: Integer;
end;
TDescendant = class(TAncestor)
Value: string; // hides the inherited Value field
end;
var
MyObject: TAncestor;
begin
MyObject := TDescendant.Create;
MyObject.Value := 'Hello!' // error
496
Class Fields (.NET)
Class fields are data fields in a class that can be accessed without an object reference.
You can introduce a block of class fields within a class declaration by using the class var block
declaration. All fields declared after class var have static storage attributes. A class var block is
terminated by the following:
1. Another class var declaration
2. A procedure or function (i.e. method) declaration (including class procedures and class functions)
3. A property declaration (including class properties)
4. A constructor or destructor declaration
5. A visibility scope specifier (public, private, protected, published, strict private, and strict protected)
For example:
type
TMyClass = class
public
class var // Introduce a block of class static fields.
Red: Integer;
Green: Integer;
Blue: Integer;
procedure Proc1; // Ends the class var block.
end;
The above class fields can be accessed with the code:
TMyClass.Red := 0;
TMyClass.Green := 0;
TMyClass.Blue := 0;
497
Methods
A method is a procedure or function associated with a class. A call to a method specifies the object (or,
if it is a class method, the class) that the method should operate on. For example,
SomeObject.Free calls the Free method in SomeObject.
About Methods
Within a class declaration, methods appear as procedure and function headings, which work like forward
declarations. Somewhere after the class declaration, but within the same module, each method must
be implemented by a defining declaration. For example, suppose the declaration of TMyClass includes
a method called DoSomething:
type
TMyClass = class(TObject)
...
procedure DoSomething;
...
end;
A defining declaration for DoSomething must occur later in the module:
procedure TMyClass.DoSomething;
begin
...
end;
While a class can be declared in either the interface or the implementation section of a unit, defining
declarations for a class' methods must be in the implementation section.
In the heading of a defining declaration, the method name is always qualified with the name of the class
to which it belongs. The heading can repeat the parameter list from the class declaration; if it does, the
order, type and names of the parameters must match exactly, and if the method is a function, the return
value must match as well.
498
Method declarations can include special directives that are not used with other functions or procedures.
Directives should appear in the class declaration only, not in the defining declaration, and should always
be listed in the following order:
where binding is virtual, dynamic, or override; calling convention is register, pascal, cdecl, stdcall, or
safecall; and warning is platform, deprecated, or library.
Inherited
The reserved word inherited plays a special role in implementing polymorphic behavior. It can occur in
method definitions, with or without an identifier after it.
If inherited is followed by the name of a member, it represents a normal method call or reference to a
property or field - except that the search for the referenced member begins with the immediate ancestor
of the enclosing method's class. For example, when
inherited Create(...);
occurs in the definition of a method, it calls the inherited Create.
When inherited has no identifier after it, it refers to the inherited method with the same name as the
enclosing method or, if the enclosing method is a message handler, to the inherited message handler
for the same message. In this case, inherited takes no explicit parameters, but passes to the inherited
method the same parameters with which the enclosing method was called. For example,
inherited;
occurs frequently in the implementation of constructors. It calls the inherited constructor with the same
parameters that were passed to the descendant.
Self
Within the implementation of a method, the identifier Self references the object in which the method is
called. For example, here is the implementation of TCollection's Add method in the Classes unit.
499
...
MyCollection.Add // MyCollection is passed to the TCollectionItem.
Create method
Self is useful for a variety of reasons. For example, a member identifier declared in a class type might
be redeclared in the block of one of the class' methods. In this case, you can access the original member
identifier as Self.Identifier.
Method Binding
Method bindings can be static (the default), virtual, or dynamic. Virtual and dynamic methods can be
overridden, and they can be abstract. These designations come into play when a variable of one class
type holds a value of a descendant class type. They determine which implementation is activated when
a method is called.
Static Methods
Methods are by default static. When a static method is called, the declared (compile-time) type of the
class or object variable used in the method call determines which implementation to activate. In the
following example, the Draw methods are static.
type
TFigure = class
procedure Draw;
end;
TRectangle = class(TFigure)
procedure Draw;
end;
Given these declarations, the following code illustrates the effect of calling a static method. In the second
call to Figure.Draw, the Figure variable references an object of class TRectangle, but the call
invokes the implementation of Draw in TFigure, because the declared type of the Figure variable is
TFigure.
var
Figure: TFigure;
Rectangle: TRectangle;
begin
Figure := TFigure.Create;
Figure.Draw; // calls TFigure.Draw
Figure.Destroy;
Figure := TRectangle.Create;
500
Figure.Draw; // calls TFigure.Draw
Figure.Destroy;
Rectangle := TRectangle.Create;
Rectangle.Draw; // calls TRectangle.Draw
Rectangle.Destroy;
end;
To override a method, redeclare it with the override directive. An override declaration must match the
ancestor declaration in the order and type of its parameters and in its result type (if any).
In the following example, the Draw method declared in TFigure is overridden in two descendant
classes.
type
TFigure = class
procedure Draw; virtual;
end;
TRectangle = class(TFigure)
procedure Draw; override;
end;
TEllipse = class(TFigure)
procedure Draw; override;
end;
Given these declarations, the following code illustrates the effect of calling a virtual method through a
variable whose actual type varies at runtime.
var
Figure: TFigure;
begin
Figure := TRectangle.Create;
Figure.Draw; // calls TRectangle.Draw
Figure.Destroy;
Figure := TEllipse.Create;
501
Figure.Draw; // calls TEllipse.Draw
Figure.Destroy;
end;
Only virtual and dynamic methods can be overridden. All methods, however, can be overloaded; see
Overloading methods.
The Delphi for .NET compiler supports the concept of a final virtual method. When the keyword final is
applied to a virtual method, no ancestor class can override that method. Use of the final keyword is an
important design decision that can help document how the class is intended to be used. It can also give
the .NET JIT compiler hints that allow it to optimize the code it produces.
In general, virtual methods are the most efficient way to implement polymorphic behavior. Dynamic
methods are useful when a base class declares many overridable methods which are inherited by many
descendant classes in an application, but only occasionally overridden.
Note: Only use dynamic methods if there is a clear, observable benefit. Generally, use
virtual methods.
Overriding Versus Hiding
If a method declaration specifies the same method identifier and parameter signature as an inherited
method, but doesn't include override, the new declaration merely hides the inherited one without
overriding it. Both methods exist in the descendant class, where the method name is statically bound.
For example,
type
T1 = class(TObject)
procedure Act; virtual;
end;
T2 = class(T1)
procedure Act; // Act is redeclared, but not overridden
end;
var
SomeObject: T1;
begin
SomeObject := T2.Create;
SomeObject.Act; // calls T1.Act
end;
502
Reintroduce
The reintroduce directive suppresses compiler warnings about hiding previously declared virtual
methods. For example,
Abstract Methods
An abstract method is a virtual or dynamic method that has no implementation in the class where it is
declared. Its implementation is deferred to a descendant class. Abstract methods must be declared with
the directive abstract after virtual or dynamic. For example,
Methods are made class static by appending the word static to their declaration, for example
type
TMyClass = class
strict private
class var
FX: Integer;
strict protected
public
class property X: Integer read GetX write SetX;
procedure StatProc(s: String); static;
503
end;
You can call a class static method through the class type (i.e. without having an object reference), for
example
TMyClass.X := 17;
TMyClass.StatProc('Hello');
Overloading Methods
A method can be redeclared using the overload directive. In this case, if the redeclared method has a
different parameter signature from its ancestor, it overloads the inherited method without hiding it. Calling
the method in a descendant class activates whichever implementation matches the parameters in the
call.
If you overload a virtual method, use the reintroduce directive when you redeclare it in descendant
classes. For example,
type
T1 = class(TObject)
procedure Test(I: Integer); overload; virtual;
end;
T2 = class(T1)
procedure Test(S: string); reintroduce; overload;
end;
...
SomeObject := T2.Create;
SomeObject.Test('Hello!'); // calls T2.Test
SomeObject.Test(7); // calls T1.Test
Within a class, you cannot publish multiple overloaded methods with the same name. Maintenance of
runtime type information requires a unique name for each published member.
type
TSomeClass = class
published
function Func(P: Integer): Integer;
function Func(P: Boolean): Integer; // error
...
Methods that serve as property read or write specifiers cannot be overloaded.
504
The implementation of an overloaded method must repeat the parameter list from the class declaration.
For more information about overloading, see Overloading procedures and functions.
Constructors
A constructor is a special method that creates and initializes instance objects. The declaration of a
constructor looks like a procedure declaration, but it begins with the word constructor. Examples:
constructor Create;
constructor Create(AOwner: TComponent);
Constructors must use the default register calling convention. Although the declaration specifies no
return value, a constructor returns a reference to the object it creates or is called in.
A class can have more than one constructor, but most have only one. It is conventional to call the
constructor Create.
To create an object, call the constructor method on a class type. For example,
MyObject := TMyClass.Create;
This allocates storage for the new object, sets the values of all ordinal fields to zero, assigns nil to all
pointer and class-type fields, and makes all string fields empty. Other actions specified in the constructor
implementation are performed next; typically, objects are initialized based on values passed as
parameters to the constructor. Finally, the constructor returns a reference to the newly allocated and
initialized object. The type of the returned value is the same as the class type specified in the constructor
call.
If an exception is raised during execution of a constructor that was invoked on a class reference, the
Destroy destructor is automatically called to destroy the unfinished object.
When a constructor is called using an object reference (rather than a class reference), it does not create
an object. Instead, the constructor operates on the specified object, executing only the statements in
the constructor's implementation, and then returns a reference to the object. A constructor is typically
invoked on an object reference in conjunction with the reserved word inherited to execute an inherited
constructor.
type
TShape = class(TGraphicControl)
private
FPen: TPen;
FBrush: TBrush;
procedure PenChanged(Sender: TObject);
procedure BrushChanged(Sender: TObject);
505
public
constructor Create(Owner: TComponent); override;
destructor Destroy; override;
...
end;
When invoked through a class-type identifier, a constructor declared as virtual is equivalent to a static
constructor. When combined with class-reference types, however, virtual constructors allow
polymorphic construction of objectsthat is, construction of objects whose types aren't known at compile
time. (See Class references.)
Note: For more information on constructors, destructors, and memory management
issues on the .NET platform, please see the topic Memory Management Issues on the .
NET Platform.
There can be no guarantees on when a class constructor will execute, except to say that it will execute
at some time before the class is used. On the .NET platform in order for a class to be "used", it must
reside in code that is actually executed. For example, if a class is first referenced in an if statement, and
the test of the if statement is never true during the course of execution, then the class will never be
loaded and JIT compiled. Hence, in this case the class constructor would not be called.
The following class declaration demonstrates the syntax of class properties and fields, as well as class
static methods and class constructors:
506
type
TMyClass = class
strict protected
Destructors
A destructor is a special method that destroys the object where it is called and deallocates its memory.
The declaration of a destructor looks like a procedure declaration, but it begins with the word destructor.
Example:
MyObject.Destroy;
When a destructor is called, actions specified in the destructor implementation are performed first.
Typically, these consist of destroying any embedded objects and freeing resources that were allocated
by the object. Then the storage that was allocated for the object is disposed of.
destructor TShape.Destroy;
begin
FBrush.Free;
FPen.Free;
inherited Destroy;
end;
507
The last action in a destructor's implementation is typically to call the inherited destructor to destroy the
object's inherited fields.
When an exception is raised during creation of an object, Destroy is automatically called to dispose of
the unfinished object. This means that Destroy must be prepared to dispose of partially constructed
objects. Because a constructor sets the fields of a new object to zero or empty values before performing
other actions, class-type and pointer-type fields in a partially constructed object are always nil. A
destructor should therefore check for nil values before operating on class-type or pointer-type fields.
Calling the Free method (defined in TObject), rather than Destroy, offers a convenient way of checking
for nil values before destroying an object.
Note: For more information on constructors, destructors, and memory management
issues on the .NET platform, please see the topic Memory Management Issues on the .
NET Platform.
Message Methods
Message methods implement responses to dynamically dispatched messages. The message method
syntax is supported on all platforms. VCL uses message methods to respond to Windows messages.
A message method is created by including the message directive in a method declaration, followed by
an integer constant between 1 and 49151 which specifies the message ID. For message methods in
VCL controls, the integer constant can be one of the Win32 message IDs defined, along with
corresponding record types, in the Messages unit. A message method must be a procedure that takes
a single var parameter.
For example:
type
TTextBox = class(TCustomControl)
private
procedure WMChar(var Message: TWMChar); message WM_CHAR;
...
end;
A message method does not have to include the override directive to override an inherited message
method. In fact, it doesn't have to specify the same method name or parameter type as the method it
overrides. The message ID alone determines which message the method responds to and whether it is
an override.
508
if Message.CharCode = Ord(#13) then
ProcessEnter
else
inherited;
end;
The inherited statement searches backward through the class hierarchy and invokes the first message
method with the same ID as the current method, automatically passing the message record to it. If no
ancestor class implements a message method for the given ID, inherited calls the DefaultHandler
method originally defined in TObject.
The implementation of DefaultHandler in TObject simply returns without performing any actions. By
overriding DefaultHandler, a class can implement its own default handling of messages. On Win32,
the DefaultHandler method for controls calls the Win32 API DefWindowProc.
Message Dispatching
Message handlers are seldom called directly. Instead, messages are dispatched to an object using the
Dispatch method inherited from TObject:
Dispatch searches backward through the class hierarchy (starting from the class of the object where
it is called) and invokes the first message method for the ID passed to it. If no message method is found
for the given ID, Dispatch calls DefaultHandler.
509
Properties
This topic describes the following material:
• Property access
• Array properties
• Index specifiers
• Storage specifiers
• Property overrides and redeclarations
• Class properties (.NET)
About Properties
A property, like a field, defines an attribute of an object. But while a field is merely a storage location
whose contents can be examined and changed, a property associates specific actions with reading or
modifying its data. Properties provide control over access to an object's attributes, and they allow
attributes to be computed.
The declaration of a property specifies a name and a type, and includes at least one access specifier.
The syntax of a property declaration is
where
• propertyName is any valid identifier.
• [indexes] is optional and is a sequence of parameter declarations separated by semicolons. Each
parameter declaration has the form identifier1, ..., identifiern: type. For more information, see Array
Properties, below.
• type must be a predefined or previously declared type identifier. That is, property declarations like
property Num: 0..9 ... are invalid.
• the index integerConstant clause is optional. For more information, see Index Specifiers, below.
• specifiers is a sequence of read, write, stored, default (or nodefault), and implements specifiers.
Every property declaration must have at least one read or write specifier.
Properties are defined by their access specifiers. Unlike fields, properties cannot be passed as var
parameters, nor can the @ operator be applied to a property. The reason is that a property doesn't
necessarily exist in memory. It could, for instance, have a read method that retrieves a value from a
database or generates a random value.
Property Access
Every property has a read specifier, a write specifier, or both. These are called access specifiers and
they have the form
510
read fieldOrMethod
write fieldOrMethod
where fieldOrMethod is the name of a field or method declared in the same class as the property or in
an ancestor class.
• If fieldOrMethod is declared in the same class, it must occur before the property declaration. If it is
declared in an ancestor class, it must be visible from the descendant; that is, it cannot be a private
field or method of an ancestor class declared in a different unit.
• If fieldOrMethod is a field, it must be of the same type as the property.
• If fieldOrMethod is a method, it cannot be dynamic and, if virtual, cannot be overloaded. Moreover,
access methods for a published property must use the default register calling convention.
• In a read specifier, if fieldOrMethod is a method, it must be a parameterless function whose result
type is the same as the property's type. (An exception is the access method for an indexed property
or an array property.)
• In a write specifier, if fieldOrMethod is a method, it must be a procedure that takes a single value
or const parameter of the same type as the property (or more, if it is an array property or indexed
property).
When a property is referenced in an expression, its value is read using the field or method listed in the
read specifier. When a property is referenced in an assignment statement, its value is written using the
field or method listed in the write specifier.
The example below declares a class called TCompass with a published property called Heading. The
value of Heading is read through the FHeading field and written through the SetHeading procedure.
type
THeading = 0..359;
TCompass = class(TControl)
private
FHeading: THeading;
511
procedure SetHeading(Value: THeading);
published
property Heading: THeading read FHeading write SetHeading;
...
end;
Given this declaration, the statements
Array Properties
Array properties are indexed properties. They can represent things like items in a list, child controls of
a control, and pixels of a bitmap.
The declaration of an array property includes a parameter list that specifies the names and types of the
indexes. For example,
512
property Pixels[X, Y: Integer]: TColor read GetPixel write SetPixel;
property Values[const Name: string]: string read GetValue write SetValue;
The format of an index parameter list is the same as that of a procedure's or function's parameter list,
except that the parameter declarations are enclosed in brackets instead of parentheses. Unlike arrays,
which can use only ordinal-type indexes, array properties allow indexes of any type.
For array properties, access specifiers must list methods rather than fields. The method in a read
specifier must be a function that takes the number and type of parameters listed in the property's index
parameter list, in the same order, and whose result type is identical to the property's type. The method
in a write specifier must be a procedure that takes the number and type of parameters listed in the
property's index parameter list, in the same order, plus an additional value or const parameter of the
same type as the property.
For example, the access methods for the array properties above might be declared as
type
TStringArray = class
public
property Strings[Index: Integer]: string ...; default;
...
end;
513
If a class has a default property, you can access that property with the abbreviation object[index],
which is equivalent to object.property[index]. For example, given the declaration above,
StringArray.Strings[7] can be abbreviated to StringArray[7]. A class can have only one
default property. Changing or hiding the default property in descendant classes may lead to unexpected
behavior, since the compiler always binds to properties statically.
Index Specifiers
Index specifiers allow several properties to share the same access method while representing different
values. An index specifier consists of the directive index followed by an integer constant between
-2147483647 and 2147483647. If a property has an index specifier, its read and write specifiers must
list methods rather than fields. For example,
type
TRectangle = class
private
FCoordinates: array[0..3] of Longint;
function GetCoordinate(Index: Integer): Longint;
procedure SetCoordinate(Index: Integer; Value: Longint);
public
property Left: Longint index 0 read GetCoordinate write
SetCoordinate;
property Top: Longint index 1 read GetCoordinate write SetCoordinate;
property Right: Longint index 2 read GetCoordinate write
SetCoordinate;
property Bottom: Longint index 3 read GetCoordinate write
SetCoordinate;
property Coordinates[Index: Integer]: Longint read GetCoordinate
write SetCoordinate;
...
end;
An access method for a property with an index specifier must take an extra value parameter of type
Integer. For a read function, it must be the last parameter; for a write procedure, it must be the second-
to-last parameter (preceding the parameter that specifies the property value). When a program accesses
the property, the property's integer constant is automatically passed to the access method.
514
Storage Specifiers
The optional stored, default, and nodefault directives are called storage specifiers. They have no effect
on program behavior, but control whether or not to save the values of published properties in form files.
The stored directive must be followed by True, False, the name of a Boolean field, or the name of a
parameterless method that returns a Boolean value. For example,
The default directive must be followed by a constant of the same type as the property. For example,
515
The following declarations illustrate the use of property overrides.
type
TAncestor = class
...
protected
property Size: Integer read FSize;
property Text: string read GetText write SetText;
property Color: TColor read FColor write SetColor stored False;
...
end;
type
TDerived = class(TAncestor)
...
protected
property Size write SetSize;
published
property Text;
property Color stored True default clBlue;
...
end;
The override of Size adds a write specifier to allow the property to be modified. The overrides of
Text and Color change the visibility of the properties from protected to published. The property
override of Color also specifies that the property should be filed if its value isn't clBlue.
A redeclaration of a property that includes a type identifier hides the inherited property rather than
overriding it. This means that a new property is created with the same name as the inherited one. Any
property declaration that specifies a type must be a complete declaration, and must therefore include
at least one access specifier.
Whether a property is hidden or overridden in a derived class, property look-up is always static. That is,
the declared (compile-time) type of the variable used to identify an object determines the interpretation
of its property identifiers. Hence, after the following code executes, reading or assigning a value to
MyObject.Value invokes Method1 or Method2, even though MyObject holds an instance of
TDescendant. But you can cast MyObject to TDescendant to access the descendant class's
properties and their access specifiers.
type
TAncestor = class
...
property Value: Integer read Method1 write Method2;
end;
516
TDescendant = class(TAncestor)
...
property Value: Integer read Method3 write Method4;
end;
You can introduce a block of class static properties within a class declaration by using the class var
block declaration. All properties declared after class var have static storage attributes. A class var block
is terminated by the following:
1. Another class var declaration
2. A procedure or function (i.e. method) declaration (including class procedures and class functions)
3. A property declaration (including class properties)
4. A constructor or destructor declaration
5. A visibility scope specifier (public, private, protected, published, strict private, and strict protected)
For example:
type
TMyClass = class
strict private
class var // Note fields must be declared as class fields
FRed: Integer;
FGreen: Integer;
FBlue: Integer;
public
class var // Introduce a block of class properties
property Red: Integer read FRed write FRed;
Green: Integer read FGreen write FGreen;
Blue: Integer read FBlue write FBlue;
procedure Proc1; // Ends the class var block.
end;
You can access the above class properites with the code:
TMyClass.Red := 0;
517
TMyClass.Blue := 0;
TMyClass.Green := 0;
518
Class References
Sometimes operations are performed on a class itself, rather than on instances of a class (that is,
objects). This happens, for example, when you call a constructor method using a class reference. You
can always refer to a specific class using its name, but at times it is necessary to declare variables or
parameters that take classes as values, and in these situations you need class-reference types.
Class-Reference Types
A class-reference type, sometimes called a metaclass, is denoted by a construction of the form
class of type
where type is any class type. The identifier type itself denotes a value whose type is class of type. If
type1 is an ancestor of type2, then class of type2 is assignment-compatible with class of
type1. Thus
To see how class-reference types are used, look at the declaration of the constructor for TCollection (in
the Classes unit):
Class-reference types are useful when you want to invoke a class method or virtual constructor on a
class or object whose actual type is unknown at compile time.
519
Constructors and Class References
A constructor can be called using a variable of a class-reference type. This allows construction of objects
whose type isn't known at compile time. For example,
Class Operators
Every class inherits from TObject methods called ClassType and ClassParent that return, respectively,
a reference to the class of an object and of an object's immediate ancestor. Both methods return a value
of type TClass (where TClass = class of TObject), which can be cast to a more specific type.
Every class also inherits a method called InheritsFrom that tests whether the object where it is called
descends from a specified class. These methods are used by the is and as operators, and it is seldom
necessary to call them directly.
The is Operator
The is operator, which performs dynamic type checking, is used to verify the actual runtime class of an
object. The expression
520
objectisclass
returns True if object is an instance of the class denoted by class or one of its descendants, and False
otherwise. (If object is nil, the result is False.) If the declared type of object is unrelated to class - that
is, if the types are distinct and one is not an ancestor of the othera compilation error results. For example,
The as Operator
The as operator performs checked typecasts. The expression
objectasclass
returns a reference to the same object as object, but with the type given by class. At runtime, object
must be an instance of the class denoted by class or one of its descendants, or be nil; otherwise an
exception is raised. If the declared type of object is unrelated to class - that is, if the types are distinct
and one is not an ancestor of the other - a compilation error results. For example,
Class Methods
A class method is a method (other than a constructor) that operates on classes instead of objects. The
definition of a class method must begin with the reserved word class. For example,
type
TFigure = class
public
class function Supports(Operation: string): Boolean; virtual;
class procedure GetInfo(var Info: TFigureInfo); virtual;
...
end;
The defining declaration of a class method must also begin with class. For example,
521
class procedure TFigure.GetInfo(var Info: TFigureInfo);
begin
...
end;
In the defining declaration of a class method, the identifier Self represents the class where the method
is called (which could be a descendant of the class in which it is defined). If the method is called in the
class C, then Self is of the type class of C. Thus you cannot use Self to access fields, properties, and
normal (object) methods, but you can use it to call constructors and other class methods.
A class method can be called through a class reference or an object reference. When it is called through
an object reference, the class of the object becomes the value of Self.
522
Exceptions
This topic covers the following material:
• A conceptual overview of exceptions and exception handling
• Declaring exception types
• Raising and handling exceptions
About Exceptions
An exception is raised when an error or other event interrupts normal execution of a program. The
exception transfers control to an exception handler, which allows you to separate normal program logic
from error-handling. Because exceptions are objects, they can be grouped into hierarchies using
inheritance, and new exceptions can be introduced without affecting existing code. An exception can
carry information, such as an error message, from the point where it is raised to the point where it is
handled.
When an application uses the SysUtils unit, most runtime errors are automatically converted into
exceptions. Many errors that would otherwise terminate an application - such as insufficient memory,
division by zero, and general protection faults - can be caught and handled.
Exception handling is appropriate for errors whose chances of occurring are low or difficult to assess,
but whose consequences are likely to be catastrophic (such as crashing the application); for error
conditions that are complicated or difficult to test for in if...then statements; and when you need to
respond to exceptions raised by the operating system or by routines whose source code you don't
control. Exceptions are commonly used for hardware, memory, I/O, and operating-system errors.
Conditional statements are often the best way to test for errors. For example, suppose you want to make
sure that a file exists before trying to open it. You could do it this way:
try
AssignFile(F, FileName);
Reset(F); // raises an EInOutError exception if file is not found
except
523
on Exception do ...
end;
But you could also avoid the overhead of exception handling by using
begin
AssignFile(F, FileName);
Reset(F);
end;
Assertions provide another way of testing a Boolean condition anywhere in your source code. When an
Assert statement fails, the program either halts with a runtime error or (if it uses the SysUtils unit)
raises an EAssertionFailed exception. Assertions should be used only to test for conditions that you do
not expect to occur.
You can group exceptions into families using inheritance. For example, the following declarations in
SysUtils define a family of exception types for math errors.
type
EMathError = class(Exception);
EInvalidOp = class(EMathError);
EZeroDivide = class(EMathError);
EOverflow = class(EMathError);
EUnderflow = class(EMathError);
Given these declarations, you can define a single EMathError exception handler that also handles
EInvalidOp, EZeroDivide, EOverflow, and EUnderflow.
Exception classes sometimes define fields, methods, or properties that convey additional information
about the error. For example,
524
Raising and Handling Exceptions
To raise an exception object, use an instance of the exception class with a raise statement. For example,
raise EMathError.Create;
In general, the form of a raise statement is
raiseobjectataddress
where object and at address are both optional. When an address is specified, it can be any expression
that evaluates to a pointer type, but is usually a pointer to a procedure or function. For example:
For example, the function below converts a string to an integer, raising an ERangeError exception if the
resulting value is outside a specified range.
A raised exception is destroyed automatically after it is handled. Never attempt to destroy a raised
exception manually.
Note: Raising an exception in the initialization section of a unit may not produce the
intended result. Normal exception support comes from the SysUtils unit, which must
be initialized before such support is available. If an exception occurs during initialization,
all initialized units - including SysUtils - are finalized and the exception is re-raised.
Then the exception is caught and handled, usually by interrupting the program.
Similarly, raising an exception in the finalization section of a unit may not lead to the
intended result if SysUtils has already been finalized when the exception has been
raised.
525
Try...except Statements
Exceptions are handled within try...except statements. For example,
try
X := Y/Z;
except
on EZeroDivide do HandleZeroDivide;
end;
This statement attempts to divide Y by Z, but calls a routine named HandleZeroDivide if an
EZeroDivide exception is raised.
try statementsexceptexceptionBlockend
elsestatements
onidentifier: typedostatement
where identifier: is optional (if included, identifier can be any valid identifier), type is a type used to
represent exceptions, and statement is any statement.
A try...except statement executes the statements in the initial statements list. If no exceptions are
raised, the exception block (exceptionBlock) is ignored and control passes to the next part of the
program.
If an exception is raised during execution of the initial statements list, either by a raise statement in the
statements list or by a procedure or function called from the statements list, an attempt is made to 'handle'
the exception:
• If any of the handlers in the exception block matches the exception, control passes to the first such
handler. An exception handler 'matches' an exception just in case the type in the handler is the
class of the exception or an ancestor of that class.
• If no such handler is found, control passes to the statement in the else clause, if there is one.
• If the exception block is just a sequence of statements without any exception handlers, control
passes to the first statement in the list.
If none of the conditions above is satisfied, the search continues in the exception block of the next-most-
recently entered try...except statement that has not yet exited. If no appropriate handler, else
clause, or statement list is found there, the search propagates to the next-most-recently entered try...
526
except statement, and so forth. If the outermost try...except statement is reached and the
exception is still not handled, the program terminates.
When an exception is handled, the stack is traced back to the procedure or function containing the
try...except statement where the handling occurs, and control is transferred to the executed
exception handler, else clause, or statement list. This process discards all procedure and function calls
that occurred after entering the try...except statement where the exception is handled. The
exception object is then automatically destroyed through a call to its Destroy destructor and control is
passed to the statement following the try...except statement. (If a call to the Exit, Break, or
Continue standard procedure causes control to leave the exception handler, the exception object is
still automatically destroyed.)
In the example below, the first exception handler handles division-by-zero exceptions, the second one
handles overflow exceptions, and the final one handles all other math exceptions. EMathError appears
last in the exception block because it is the ancestor of the other two exception classes; if it appeared
first, the other two handlers would never be invoked.
try
...
except
on EZeroDivide do HandleZeroDivide;
on EOverflow do HandleOverflow;
on EMathError do HandleMathError;
end;
An exception handler can specify an identifier before the name of the exception class. This declares the
identifier to represent the exception object during execution of the statement that follows on...do. The
scope of the identifier is limited to that statement. For example,
try
...
except
on E: Exception do ErrorDialog(E.Message, E.HelpContext);
end;
If the exception block specifies an else clause, the else clause handles any exceptions that aren't
handled by the block's exception handlers. For example,
try
...
except
on EZeroDivide do HandleZeroDivide;
on EOverflow do HandleOverflow;
on EMathError do HandleMathError;
else
527
HandleAllOthers;
end;
Here, the else clause handles any exception that isn't an EMathError.
An exception block that contains no exception handlers, but instead consists only of a list of statements,
handles all exceptions. For example,
try
...
except
HandleException;
end;
Here, the HandleException routine handles any exception that occurs as a result of executing the
statements between try and except.
Re-raising Exceptions
When the reserved word raise occurs in an exception block without an object reference following it, it
raises whatever exception is handled by the block. This allows an exception handler to respond to an
error in a limited way and then re-raise the exception. Re-raising is useful when a procedure or function
has to clean up after an exception occurs but cannot fully handle the exception.
For example, the GetFileList function allocates a TStringList object and fills it with file names
matching a specified search path:
528
invalid, or because there is not enough memory to fill in the string list - GetFileList needs to dispose
of the new string list, since the caller does not yet know of its existence. For this reason, initialization of
the string list is performed in a try...except statement. If an exception occurs, the statement's
exception block disposes of the string list, then re-raises the exception.
Nested Exceptions
Code executed in an exception handler can itself raise and handle exceptions. As long as these
exceptions are also handled within the exception handler, they do not affect the original exception.
However, once an exception raised in an exception handler propagates beyond that handler, the original
exception is lost. This is illustrated by the Tan function below.
type
ETrigError = class(EMathError);
function Tan(X: Extended): Extended;
begin
try
Result := Sin(X) / Cos(X);
except
on EMathError do
raise ETrigError.Create('Invalid argument to Tan');
end;
end;
If an EMathError exception occurs during execution of Tan, the exception handler raises an
ETrigError. Since Tan does not provide a handler for ETrigError, the exception propagates beyond
the original exception handler, causing the EMathError exception to be destroyed. To the caller, it
appears as if the Tan function has raised an ETrigError exception.
Try...finally Statements
Sometimes you want to ensure that specific parts of an operation are completed, whether or not the
operation is interrupted by an exception. For example, when a routine acquires control of a resource, it
is often important that the resource be released, regardless of whether the routine terminates normally.
In these situations, you can use a try...finally statement.
The following example shows how code that opens and processes a file can ensure that the file is
ultimately closed, even if an error occurs during execution.
Reset(F);
try
... // process file F
finally
529
CloseFile(F);
end;
The syntax of a try...finally statement is
trystatementList1finallystatementList2end
where each statementList is a sequence of statements delimited by semicolons. The
try...finally statement executes the statements in statementList1 (the try clause). If
statementList1 finishes without raising exceptions, statementList2 (the finally clause) is executed. If an
exception is raised during execution of statementList1, control is transferred to statementList2; once
statementList2 finishes executing, the exception is re-raised. If a call to the Exit, Break, or
Continue procedure causes control to leave statementList1, statementList2 is automatically executed.
Thus the finally clause is always executed, regardless of how the try clause terminates.
If an exception is raised but not handled in the finally clause, that exception is propagated out of the
try...finally statement, and any exception already raised in the try clause is lost. The finally clause
should therefore handle all locally raised exceptions, so as not to disturb propagation of other exceptions.
The Exception class has properties called Message and HelpContext that can be used to pass an
error description and a context ID for context-sensitive online documentation. It also defines various
constructor methods that allow you to specify the description and context ID in different ways.
530
Standard Routines and I/O
This section describes the standard routines included in the Delphi runtime library.
531
Standard Routines and I/O
These topics discuss text and file I/O and summarize standard library routines. Many of the procedures
and functions listed here are defined in the System and SysInit units, which are implicitly used with
every application. Others are built into the compiler but are treated as if they were in the System unit.
Some standard routines are in units such as SysUtils, which must be listed in a uses clause to make
them available in programs. You cannot, however, list System in a uses clause, nor should you modify
the System unit or try to rebuild it explicitly.
532
RmDir Removes an empty subdirectory.
Seek Moves the current position of a typed or untyped file to a specified component. Not used
with text files.
SeekEof Returns the end-of-file status of a text file.
SeekEoln Returns the end-of-line status of a text file.
SetTextBuf Assigns an I/O buffer to a text file.
Truncate Truncates a typed or untyped file at the current file position.
Write Writes one or more values to a file.
Writeln Does the same as Write, and then writes an end-of-line marker to the text file.
A file variable is any variable whose type is a file type. There are three classes of file: typed, text, and
untyped. The syntax for declaring file types is given in File types.
Before a file variable can be used, it must be associated with an external file through a call to the
AssignFile procedure. An external file is typically a named disk file, but it can also be a device, such as
the keyboard or the display. The external file stores the information written to the file or supplies the
information read from the file.
Once the association with an external file is established, the file variable must be opened to prepare it
for input or output. An existing file can be opened via the Reset procedure, and a new file can be created
and opened via the Rewrite procedure. Text files opened with Reset are read-only and text files opened
with Rewrite and Append are write-only. Typed files and untyped files always allow both reading and
writing regardless of whether they were opened with Reset or Rewrite.
Every file is a linear sequence of components, each of which has the component type (or record type)
of the file. The components are numbered starting with zero.
Files are normally accessed sequentially. That is, when a component is read using the standard
procedure Read or written using the standard procedure Write, the current file position moves to the
next numerically ordered file component. Typed files and untyped files can also be accessed randomly
through the standard procedure Seek, which moves the current file position to a specified component.
The standard functions FilePos and FileSize can be used to determine the current file position and the
current file size.
When a program completes processing a file, the file must be closed using the standard procedure
CloseFile. After a file is closed, its associated external file is updated. The file variable can then be
associated with another external file.
By default, all calls to standard I/O procedures and functions are automatically checked for errors, and
if an error occurs an exception is raised (or the program is terminated if exception handling is not
enabled). This automatic checking can be turned on and off using the {$I+} and {$I} compiler directives.
When I/O checking is off, that is, when a procedure or function call is compiled in the {$I} state an I/O
error doesn't cause an exception to be raised; to check the result of an I/O operation, you must call the
standard function IOResult instead.
533
You must call the IOResult function to clear an error, even if you aren't interested in the error. If you
don't clear an error and {$I+} is the current state, the next I/O function call will fail with the lingering
IOResult error.
Text Files
This section summarizes I/O using file variables of the standard type Text.
When a text file is opened, the external file is interpreted in a special way: It is considered to represent
a sequence of characters formatted into lines, where each line is terminated by an end-of-line marker
(a carriage-return character, possibly followed by a line feed character). The type Text is distinct from
the type file of Char.
For text files, there are special forms of Read and Write that let you read and write values that are not
of type Char. Such values are automatically translated to and from their character representation. For
example, Read(F, I), where I is a type Integer variable, reads a sequence of digits, interprets that
sequence as a decimal integer, and stores it in I.
There are two standard text file variables, Input and Output. The standard file variable Input is a read-
only file associated with the operating system's standard input (typically, the keyboard). The standard
file variable Output is a write-only file associated with the operating system's standard output (typically,
the display). Before an application begins executing, Input and Output are automatically opened, as if
the following statements were executed:
AssignFile(Input, '');
Reset(Input);
AssignFile(Output, '');
Rewrite(Output);
Note: For Win32 applications, text-oriented I/O is available only in console applications,
that is, applications compiled with the Generate console application option checked on
the Linker page of the Project Options dialog box or with the -cc command-line compiler
option. In a GUI (non-console) application, any attempt to read or write using Input or
Output will produce an I/O error.
Some of the standard I/O routines that work on text files don't need to have a file variable explicitly given
as a parameter. If the file parameter is omitted, Input or Output is assumed by default, depending on
whether the procedure or function is input- or output-oriented. For example, Read(X) corresponds to
Read(Input, X) and Write(X) corresponds to Write(Output, X).
If you do specify a file when calling one of the input or output routines that work on text files, the file
must be associated with an external file using AssignFile, and opened using Reset, Rewrite, or Append.
An error occurs if you pass a file that was opened with Reset to an output-oriented procedure or function.
An error also occurs if you pass a file that was opened with Rewrite or Append to an input-oriented
procedure or function.
534
Untyped Files
Untyped files are low-level I/O channels used primarily for direct access to disk files regardless of type
and structuring. An untyped file is declared with the word file and nothing more. For example,
Except for Read and Write, all typed-file standard procedures and functions are also allowed on untyped
files. Instead of Read and Write, two procedures called BlockRead and BlockWrite are used for high-
speed data transfers.
The four functions that define each device driver are Open, InOut, Flush, and Close. The function header
of each function is
To associate the device-interface functions with a specific file, you must write a customized Assign
procedure. The Assign procedure must assign the addresses of the four device-interface functions to
the four function pointers in the text file variable. In addition, it should store the fmClosedmagic constant
in the Mode field, store the size of the text file buffer in BufSize, store a pointer to the text file buffer in
BufPtr, and clear the Name string.
Assuming, for example, that the four device-interface functions are called DevOpen, DevInOut,
DevFlush, and DevClose, the Assign procedure might look like this:
535
FlushFunc := @DevFlush;
CloseFunc := @DevClose;
Name[0] := #0;
end;
end;
The device-interface functions can use the UserData field in the file record to store private information.
This field isn't modified by the product file system at any time.
The Open function prepares the file for input or output, according to the Mode value. If Mode specified
fmInOut (indicating that Open was called from Append), it must be changed to fmOutput before Open
returns.
Open is always called before any of the other device-interface functions. For that reason, AssignDev
only initializes the OpenFunc field, leaving initialization of the remaining vectors up to Open. Based on
Mode, Open can then install pointers to either input- or output-oriented functions. This saves the InOut,
Flush functions and the CloseFile procedure from determining the current mode.
When Mode is fmInput, the InOut function reads up to BufSize characters into BufPtr^, and returns the
number of characters read in BufEnd. In addition, it stores zero in BufPos. If the InOut function returns
zero in BufEnd as a result of an input request, Eof becomes True for the file.
When Mode is fmOutput, the InOut function writes BufPos characters from BufPtr^, and returns zero in
BufPos.
If Mode is fmOutput, the Flush function can write the contents of the buffer exactly like the InOut function,
which ensures that text written to the device appears on the device immediately. If Flush does nothing,
the text doesn't appear on the device until the buffer becomes full or the file is closed.
536
The Close function
The Close function is called by the CloseFile standard procedure to close a text file associated with a
device. (The Reset, Rewrite, and Append procedures also call Close if the file they are opening is already
open.) If Mode is fmOutput, then before calling Close, the file system calls the InOut function to ensure
that all characters have been written to the device.
Function Description
537
StrNew Allocates a string on the heap.
StrPCopy Copies a Pascal string to a null-terminated string.
StrPLCopy Copies a Pascal string to a null-terminated string with a given maximum length.
StrPos Returns a pointer to the first occurrence of a given substring within a string.
StrRScan Returns a pointer to the last occurrence of a given character within a string.
StrScan Returns a pointer to the first occurrence of a given character within a string.
StrUpper Converts a string to uppercase.
Standard string-handling functions have multibyte-enabled counterparts that also implement locale-
specific ordering for characters. Names of multibyte functions start with Ansi-. For example, the multibyte
version of StrPos is AnsiStrPos. Multibyte character support is operating-system dependent and based
on the current locale.
Wide-Character Strings
The System unit provides three functions, WideCharToString, WideCharLenToString, and
StringToWideChar, that can be used to convert null-terminated wide character strings to single- or
double-byte long strings.
Assignment will also convert between strings. For instance, the following are both valid:
MyAnsiString := MyWideString;
MyWideString := MyAnsiString;
538
Chr Returns the character for a specified integer value.
Close Closes a file.
CompareMem Performs a binary comparison of two memory images.
CompareStr Compares strings case sensitively.
CompareText Compares strings by ordinal value and is not case sensitive.
Continue Returns control to the next iteration of for, while, or repeat statements.
Copy Returns a substring of a string or a segment of a dynamic array.
Cos Calculates the cosine of an angle.
CurrToStr Converts a currency variable to a string.
Date Returns the current date.
DateTimeToStr Converts a variable of type TDateTime to a string.
DateToStr Converts a variable of type TDateTime to a string.
Dec Decrements an ordinal variable or a typed pointer variable.
Dispose Releases dynamically allocated variable memory.
ExceptAddr Returns the address at which the current exception was raised.
Exit Exits from the current procedure.
Exp Calculates the exponential of X.
FillChar Fills contiguous bytes with a specified value.
Finalize Uninitializes a dynamically allocated variable.
FloatToStr Converts a floating point value to a string.
FloatToStrF Converts a floating point value to a string, using specified format.
FmtLoadStr Returns formatted output using a resourced format string.
FmtStr Assembles a formatted string from a series of arrays.
Format Assembles a string from a format string and a series of arrays.
FormatDateTime Formats a date-and-time value.
FormatFloat Formats a floating point value.
FreeMem Releases allocated memory.
GetMem Allocates dynamic memory and a pointer to the address of the block.
Halt Initiates abnormal termination of a program.
Hi Returns the high-order byte of an expression as an unsigned value.
High Returns the highest value in the range of a type, array, or string.
Inc Increments an ordinal variable or a typed pointer variable.
Initialize Initializes a dynamically allocated variable.
Insert Inserts a substring at a specified point in a string.
539
Int Returns the integer part of a real number.
IntToStr Converts an integer to a string.
Length Returns the length of a string or array.
Lo Returns the low-order byte of an expression as an unsigned value.
Low Returns the lowest value in the range of a type, array, or string.
LowerCase Converts an ASCII string to lowercase.
MaxIntValue Returns the largest signed value in an integer array.
MaxValue Returns the largest signed value in an array.
MinIntValue Returns the smallest signed value in an integer array.
MinValue Returns smallest signed value in an array.
New Creates a dynamic allocated variable memory and references it with a specified pointer.
Now Returns the current date and time.
Ord Returns the ordinal integer value of an ordinal-type expression.
Pos Returns the index of the first single-byte character of a specified substring in a string.
Pred Returns the predecessor of an ordinal value.
Ptr Converts a value to a pointer.
Random Generates random numbers within a specified range.
ReallocMem Reallocates a dynamically allocatable memory.
Round Returns the value of a real rounded to the nearest whole number.
SetLength Sets the dynamic length of a string variable or array.
SetString Sets the contents and length of the given string.
ShowException Displays an exception message with its address.
ShowMessage Displays a message box with an unformatted string and an OK button.
ShowMessageFmt Displays a message box with a formatted string and an OK button.
Sin Returns the sine of an angle in radians.
SizeOf Returns the number of bytes occupied by a variable or type.
Sqr Returns the square of a number.
Sqrt Returns the square root of a number.
Str Converts an integer or real number into a string.
StrToCurr Converts a string to a currency value.
StrToDate Converts a string to a date format (TDateTime).
StrToDateTime Converts a string to a TDateTime.
StrToFloat Converts a string to a floating-point value.
StrToInt Converts a string to an integer.
540
StrToTime Converts a string to a time format (TDateTime).
StrUpper Returns an ASCII string in upper case.
Succ Returns the successor of an ordinal value.
Sum Returns the sum of the elements from an array.
Time Returns the current time.
TimeToStr Converts a variable of type TDateTime to a string.
Trunc Truncates a real number to an integer.
UniqueString Ensures that a string has only one reference. (The string may be copied to produce a
single reference.)
UpCase Converts a character to uppercase.
UpperCase Returns a string in uppercase.
VarArrayCreate Creates a variant array.
VarArrayDimCount Returns number of dimensions of a variant array.
VarArrayHighBound Returns high bound for a dimension in a variant array.
VarArrayLock Locks a variant array and returns a pointer to the data.
VarArrayLowBound Returns the low bound of a dimension in a variant array.
VarArrayOf Creates and fills a one-dimensional variant array.
VarArrayRedim Resizes a variant array.
VarArrayRef Returns a reference to the passed variant array.
VarArrayUnlock Unlocks a variant array.
VarAsType Converts a variant to specified type.
VarCast Converts a variant to a specified type, storing the result in a variable.
VarClear Clears a variant.
VarCopy Copies a variant.
VarToStr Converts variant to string.
VarType Returns type code of specified variant.
541
Libraries and Packages
This section describes how to create static and dynamically loadable libraries in Delphi.
542
Libraries and Packages
A dynamically loadable library is a dynamic-link library (DLL) on Win32, and an assembly (also a DLL)
on the .NET platform. It is a collection of routines that can be called by applications and by other DLLs
or shared objects. Like units, dynamically loadable libraries contain sharable code or resources. But this
type of library is a separately compiled executable that is linked at runtime to the programs that use it.
Delphi programs can call DLLs and assemblies written in other languages, and applications written in
other languages can call DLLs or assemblies written in Delphi.
Before you can call routines defined in DLL or assembly, you must import them. This can be done in
two ways: by declaring an external procedure or function, or by direct calls to the operating system.
Whichever method you use, the routines are not linked to your application until runtime.
The Delphi language does not support importing of variables from DLLs or assemblies.
Static Loading
The simplest way to import a procedure or function is to declare it using the external directive. For
example,
Declarations of imported routines can be placed directly in the program or unit where they are called.
To simplify maintenance, however, you can collect external declarations into a separate "import unit"
that also contains any constants and types required for interfacing with the library. Other modules that
use the import unit can call any routines declared in it.
Dynamic Loading
You can access routines in a library through direct calls to Win32 APIs, including LoadLibrary,
FreeLibrary, and GetProcAddress. These functions are declared in Windows.pas. on Linux, they
are implemented for compatibility in SysUtils.pas; the actual Linux OS routines are dlopen, dlclose, and
dlsym (all declared in libc; see the man pages for more information). In this case, use procedural-type
variables to reference the imported routines.
543
For example,
type
TTimeRec = record
Second: Integer;
Minute: Integer;
Hour: Integer;
end;
var
Time: TTimeRec;
Handle: THandle;
GetTime: TGetTime;
.
.
.
begin
Handle := LoadLibrary('libraryname');
if Handle <> 0 then
begin
@GetTime := GetProcAddress(Handle, 'GetTime');
if @GetTime <> nil then
begin
GetTime(Time);
with Time do
WriteLn('The time is ', Hour, ':', Minute, ':', Second);
end;
FreeLibrary(Handle);
end;
end;
When you import routines this way, the library is not loaded until the code containing the call to
LoadLibrary executes. The library is later unloaded by the call to FreeLibrary. This allows you to
conserve memory and to run your program even when some of the libraries it uses are not present.
544
Writing Dynamically Loaded Libraries
The following topics describe elements of writing dynamically loadable libraries, including
• The exports clause.
• Library initialization code.
• Global variables.
• Libraries and system variables.
Only routines that a library explicitly exports are available for importing by other libraries or programs.
The following example shows a library with two exported functions, Min and Max.
library MinMax;
function Min(X, Y: Integer): Integer; stdcall;
begin
if X < Y then Min := X else Min := Y;
end;
function Max(X, Y: Integer): Integer; stdcall;
begin
if X > Y then Max := X else Max := Y;
end;
exports
Min,
Max;
begin
end.
If you want your library to be available to applications written in other languages, it's safest to specify
stdcall in the declarations of exported functions. Other languages may not support Delphi's default
register calling convention.
Libraries can be built from multiple units. In this case, the library source file is frequently reduced to a
uses clause, an exports clause, and the initialization code. For example,
library Editors;
uses EdInit, EdInOut, EdFormat, EdPrint;
exports
InitEditors,
DoneEditors name Done,
InsertText name Insert,
DeleteSelection name Delete,
545
FormatSelection,
PrintSelection name Print,
.
.
.
SetErrorHandler;
begin
InitLibrary;
end.
You can put exports clauses in the interface or implementation section of a unit. Any library that includes
such a unit in its uses clause automatically exports the routines listed the unit's exports clauses without
the need for an exports clause of its own.
The directive local, which marks routines as unavailable for export, is platform-specific and has no effect
in Windows programming.
On Linux, the local directive provides a slight performance optimization for routines that are compiled
into a library but are not exported. This directive can be specified for stand-alone procedures and
functions, but not for methods. A routine declared with localfor example,
A routine is exported when it is listed in an exports clause, which has the form
(Entries can also include the directive resident, which is maintained for backward compatibility and is
ignored by the compiler.)
On the Win32 platform, an index specifier consists of the directive index followed by a numeric constant
between 1 and 2,147,483,647. (For more efficient programs, use low index values.) If an entry has no
index specifier, the routine is automatically assigned a number in the export table.
Note: Use of index specifiers, which are supported for backward compatibility only, is
discouraged and may cause problems for other development tools.
A name specifier consists of the directive name followed by a string constant. If an entry has no name
specifier, the routine is exported under its original declared name, with the same spelling and case. Use
a name clause when you want to export a routine under a different name. For example,
546
exports
DoSomethingABC name 'DoSomething';
When you export an overloaded function or procedure from a dynamically loadable library, you must
specify its parameter list in the exports clause. For example,
exports
Divide(X, Y: Integer) name 'Divide_Ints',
Divide(X, Y: Real) name 'Divide_Reals';
On Win32, do not include index specifiers in entries for overloaded routines.
An exports clause can appear anywhere and any number of times in the declaration part of a program
or library, or in the interface or implementation section of a unit. Programs seldom contain an exports
clause.
Library initialization code can signal an error by setting the ExitCode variable to a nonzero value.
ExitCode is declared in the System unit and defaults to zero, indicating successful initialization. If a
library's initialization code sets ExitCode to another value, the library is unloaded and the calling
application is notified of the failure. Similarly, if an unhandled exception occurs during execution of the
initialization code, the calling application is notified of a failure to load the library.
Here is an example of a library with initialization code and an entry point procedure.
library Test;
var
SaveDllProc: Pointer;
procedure LibExit(Reason: Integer);
begin
if Reason = DLL_PROCESS_DETACH then
begin
.
. // library exit code
.
end;
SaveDllProc(Reason); // call saved entry point procedure
end;
begin
.
547
. // library initialization code
.
SaveDllProc := DllProc; // save exit procedure chain
DllProc := @LibExit; // install LibExit exit procedure
end.
DllProc is called when the library is first loaded into memory, when a thread starts or stops, or when
the library is unloaded. The initialization parts of all units used by a library are executed before the
library's initialization code, and the finalization parts of those units are executed after the library's entry
point procedure.
A library can be used by several applications at once, but each application has a copy of the library in
its own process space with its own set of global variables. For multiple libraries - or multiple instances
of a library - to share memory, they must use memory-mapped files. Refer to the your system
documentation for further information.
The DLLProc variable allows a library to monitor calls that the operating system makes to the library
entry point. This feature is normally used only by libraries that support multithreading. DLLProc is
available on both Windows and Linux but its use differs on each. On Win32, DLLProc is used in
multithreading applications.; on Linux, it is used to determine when your library is being unloaded. You
should use finalization sections, rather than exit procedures, for all exit behavior.
To monitor operating-system calls, create a callback procedure that takes a single integer parameterfor
example,
DLL_PROCESS_DETACHIndicates that the library is detaching from the address space of the calling process as
a result of a clean exit or a call to FreeLibrary.
DLL_PROCESS_ATTACH Indicates that the library is attaching to the address space of the calling process as the
result of a call to LoadLibrary.
548
DLL_THREAD_ATTACH Indicates that the current process is creating a new thread.
DLL_THREAD_DETACH Indicates that a thread is exiting cleanly.
In the body of the procedure, you can specify actions to take depending on which parameter is passed
to the procedure.
On Win32, if the calling application or library is written in another language, the exception can be handled
as an operating-system exception with the exception code $0EEDFADE. The first entry in the
ExceptionInformation array of the operating-system exception record contains the exception
address, and the second entry contains a reference to the Delphi exception object.
Generally, you should not let exceptions escape from your library. Delphi exceptions map to the OS
exception model (including the .NET exception model)..
If a library does not use the SysUtils unit, exception support is disabled. In this case, when a runtime
error occurs in the library, the calling application terminates. Because the library has no way of knowing
whether it was called from a Delphi program, it cannot invoke the application's exit procedures; the
application is simply aborted and removed from memory.
ShareMem is the interface unit for the BORLANDMM.DLL memory manager, which allows modules to
share dynamically allocated memory. BORLANDMM.DLL must be deployed with applications and DLLs
that use ShareMem. When an application or DLL uses ShareMem, its memory manager is replaced by
the memory manager in BORLANDMM.DLL.
549
Packages
The following topics describe packages and various issues involved in creating and compiling them.
• Package declarations and source files
• Naming packages
• The requires clause
• Avoiding circular package references
• Duplicate package references
• The contains clause
• Avoiding redundant source code uses
• Compiling packages
• Generated files
• Package-specific compiler directives
• Package-specific command-line compiler switches
Understanding Packages
A package is a specially compiled library used by applications, the IDE, or both. Packages allow you to
rearrange where code resides without affecting the source code. This is sometimes referred to as
application partitioning.
Runtime packages provide functionality when a user runs an application. Design-time packages are
used to install components in the IDE and to create special property editors for custom components. A
single package can function at both design time and runtime, and design-time packages frequently work
by referencing runtime packages in their requires clauses.
On Win32, package files end with the .bpl (Borland package library) extension. On the .NET platform,
packages are .NET assemblies, and end with an extension of .dll
Ordinarily, packages are loaded statically when an applications starts. But you can use the
LoadPackage and UnloadPackage routines (in the SysUtils unit) to load packages dynamically.
Note: When an application utilizes packages, the name of each packaged unit still must
appear in the uses clause of any source file that references it.
550
• a list of unit files contained by, or bound into, the package when it is compiled. The package is
essentially a wrapper for these source-code units, which provide the functionality of the compiled
package.
packagepackageName;
requiresClause;
containsClause;
end.
where packageName is any valid identifier. The requiresClause and containsClause are both optional.
For example, the following code declares the DATAX package.
package DATAX;
requires
rtl,
contains Db, DBLocal, DBXpress, ... ;
end.
The requires clause lists other, external packages used by the package being declared. It consists of
the directive requires, followed by a comma-delimited list of package names, followed by a semicolon.
If a package does not reference other packages, it does not need a requires clause.
The contains clause identifies the unit files to be compiled and bound into the package. It consists of
the directive contains, followed by a comma-delimited list of unit names, followed by a semicolon. Any
unit name may be followed by the reserved word in and the name of a source file, with or without a
directory path, in single quotation marks; directory paths can be absolute or relative. For example,
Naming packages
A compiled package involves several generated files. For example, the source file for the package called
DATAX is DATAX.DPK, from which the compiler generates an executable and a binary image called
DATAX is used to refer to the package in the requires clauses of other packages, or when using the
package in an application. Package names must be unique within a project.
551
compile time into any application that uses both the current package and one of the units contained in
the external package.
If the unit files contained in a package make references to other packaged units, the other packages
should be included in the first package's requires clause. If the other packages are omitted from the
requires clause, the compiler loads the referenced units from their .dcu or .dcuil files.
All units included directly in a package's contains clause, or indirectly in the uses clauses of those units,
are bound into the package at compile time. The units contained (directly or indirectly) in a package
cannot be contained in any other packages referenced in requires clause of that package.
A unit cannot be contained (directly or indirectly) in more than one package used by the same application.
Compiling Packages
Packages are ordinarily compiled from the IDE using .dpk files generated by the Project Manager. You
can also compile .dpk files directly from the command line. When you build a project that contains a
package, the package is implicitly recompiled, if necessary.
Generated Files
The following table lists the files produced by the successful compilation of a package.
552
Compiled package files
DCP (Win32) or DCPIL (.NET)A binary image containing a package header and the concatenation of all .dcu
(Win32) or .dcuil (.NET) files in the package. A single .dcp or .dcpil file is created for
each package. The base name for the file is the base name of the .dpk source file.
BPL (Win32) or DLL (.NET) The runtime package. This file is a DLL on Win32 with special Borland-specific
features. The base name for the package is the base name of the dpk source file.
Directive Purpose
{$IMPLICITBUILD OFF} Prevents a package from being implicitly recompiled later. Use in .dpk files
when compiling packages that provide low-level functionality, that change
infrequently between builds, or whose source code will not be distributed.
{$G-} or {$IMPORTEDDATA OFF}Disables creation of imported data references. This directive increases
memory-access efficiency, but prevents the unit where it occurs from
referencing variables in other packages.
{$WEAKPACKAGEUNIT ON} Packages unit weakly.
{$DENYPACKAGEUNIT ON} Prevents unit from being placed in a package.
{$DESIGNONLY ON} Compiles the package for installation in the IDE. (Put in .dpk file.)
{$RUNONLY ON} Compiles the package as runtime only. (Put in .dpk file.)
Including {$DENYPACKAGEUNIT ON} in source code prevents the unit file from being packaged.
Including {$G-} or {$IMPORTEDDATA OFF} may prevent a package from being used in the same
application with other packages.
Switch Purpose
-$G- Disables creation of imported data references. Using this switch increases
memory-access efficiency, but prevents packages compiled with it from
referencing variables in other packages.
553
LE path Specifies the directory where the compiled package file will be placed.
LN path Specifies the directory where the package dcp or dcpil file will be placed.
LUpackageName [;packageName2;...]Specifies additional runtime packages to use in an application. Used when
compiling a project.
Z Prevents a package from being implicitly recompiled later. Use when
compiling packages that provide low-level functionality, that change
infrequently between builds, or whose source code will not be distributed.
Using the -$G- switch may prevent a package from being used in the same application with other
packages.
554
Object Interfaces
This section describes the use of interfaces in Delphi.
555
Object Interfaces
An object interface, or simply interface, defines methods that can be implemented by a class. Interfaces
are declared like classes, but cannot be directly instantiated and do not have their own method
definitions. Rather, it is the responsibility of any class that supports an interface to provide
implementations for the interface's methods. A variable of an interface type can reference an object
whose class implements that interface; however, only methods declared in the interface can be called
using such a variable.
Interfaces offer some of the advantages of multiple inheritance without the semantic difficulties. They
are also essential for using distributed object models (such as CORBA and SOAP). Using a distributed
object model, custom objects that support interfaces can interact with objects written in C++, Java, and
other languages.
Interface Types
Interfaces, like classes, can be declared only in the outermost scope of a program or unit, not in a
procedure or function declaration. An interface type declaration has the form
type
IMalloc = interface(IInterface)
['{00000002-0000-0000-C000-000000000046}']
function Alloc(Size: Integer): Pointer; stdcall;
function Realloc(P: Pointer; Size: Integer): Pointer; stdcall;
procedure Free(P: Pointer); stdcall;
function GetSize(P: Pointer): Integer; stdcall;
556
function DidAlloc(P: Pointer): Integer; stdcall;
procedure HeapMinimize; stdcall;
end;
In some interface declarations, the interface reserved word is replaced by dispinterface. This
construction (along with the dispid, read only, and write only directives) is platform-specific and is not
used in Linux programming.
The declaration of an interface can specify an ancestor interface. If no ancestor is specified, the interface
is a direct descendant of IInterface, which is defined in the System unit and is the ultimate ancestor
of all other interfaces. On Win32, IInterface declares three methods: QueryInterface,
_AddRef, and _Release. These methods are not present on the .NET platform, and you do not need
to implement them.
Note: IInterface is equivalent to IUnknown. You should generally use
IInterface for platform independent applications and reserve the use of
IUnknown for specific programs that include Win32 dependencies.
QueryInterface provides the means to obtain a reference to the different interfaces that an object
supports. _AddRef and _Release provide lifetime memory management for interface references. The
easiest way to implement these methods is to derive the implementing class from the System unit's
TInterfacedObject. It is also possible to dispense with any of these methods by implementing it as an
empty function; COM objects, however, must be managed through _AddRef and _Release.
Interface Identification
An interface declaration can specify a globally unique identifier (GUID), represented by a string literal
enclosed in brackets immediately preceding the member list. The GUID part of the declaration must
have the form
['{xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}']
where each x is a hexadecimal digit (0 through 9 or A through F). The Type Library editor automatically
generates GUIDs for new interfaces. You can also generate GUIDs by pressing Ctrl+Shift+G in the code
editor.
A GUID is a 16-byte binary value that uniquely identifies an interface. If an interface has a GUID, you
can use interface querying to get references to its implementations.
The TGUID and PGUID types, declared in the System unit, are used to manipulate GUIDs.
557
type
PGUID = ^TGUID;
TGUID = packed record
D1: Longword;
D2: Word;
D3: Word;
D4: array[0..7] of Byte;
end;
On the .NET platform, you can tag an interface as described above (i.e. following the interface
declaration). However, if you use the traditional Delphi syntax, the first square bracket construct following
the interface declaration is taken as a GUID specifier - not as a .NET attribute. (Note that .NET attributes
always apply to the next symbol, not the previous one.) You can also associate a GUID with an interface
using the .NET Guid custom attribute. In this case you would use the .NET style syntax, placing the
attribute immediately before the interface declaration.
GUIDs are not required for interfaces in the .NET framework. They are only used for COM
interoperability.
When you declare a typed constant of type TGUID, you can use a string literal to specify its value. For
example,
558
Interface Properties
Properties declared in an interface are accessible only through expressions of the interface type; they
cannot be accessed through class-type variables. Moreover, interface properties are visible only within
programs where the interface is compiled.
In an interface, property read and write specifiers must be methods, since fields are not available.
Forward Declarations
An interface declaration that ends with the reserved word interface and a semicolon, without specifying
an ancestor, GUID, or member list, is a forward declaration. A forward declaration must be resolved by
a defining declaration of the same interface within the same type declaration section. In other words,
between a forward declaration and its defining declaration, nothing can occur except other type
declarations.
type
IControl = interface;
IWindow = interface
['{00000115-0000-0000-C000-000000000044}']
function GetControl(Index: Integer): IControl;
.
.
.
end;
IControl = interface
['{00000115-0000-0000-C000-000000000049}']
function GetWindow: IWindow;
.
.
.
end;
Mutually derived interfaces are not allowed. For example, it is not legal to derive IWindow from
IControl and also derive IControl from IWindow.
559
Implementing Interfaces
Once an interface has been declared, it must be implemented in a class before it can be used. The
interfaces implemented by a class are specified in the class's declaration, after the name of the class's
ancestor.
Class Declarations
Such declarations have the form
type
TMemoryManager = class(TInterfacedObject, IMalloc, IErrorInfo)
.
.
.
end;
declares a class called TMemoryManager that implements the IMalloc and IErrorInfo interfaces.
When a class implements an interface, it must implement (or inherit an implementation of) each method
declared in the interface.
Here is the (Win32) declaration of TInterfacedObject in the System unit. On the .NET platform,
TInterfacedObject is an alias for TObject.
type
TInterfacedObject = class(TObject, IInterface)
protected
FRefCount: Integer;
function QueryInterface(const IID: TGUID; out Obj): HResult;
stdcall;
function _AddRef: Integer; stdcall;
function _Release: Integer; stdcall;
public
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
class function NewInstance: TObject; override;
560
property RefCount: Integer read FRefCount;
end;
TInterfacedObject implements the IInterface interface. Hence TInterfacedObject declares and
implements each of IInterface''s three methods.
Classes that implement interfaces can also be used as base classes. (The first example above declares
TMemoryManager as a direct descendent of TInterfacedObject.) On the Win32 platform, every interface
inherits from IInterface, and a class that implements interfaces must implement the QueryInterface,
_AddRef, and _Release methods. The System unit's TInterfacedObject implements these methods
and is thus a convenient base from which to derive other classes that implement interfaces. On
the .NET platform, IInterface does not declare these methods, and you do not need to implement them.
When an interface is implemented, each of its methods is mapped onto a method in the implementing
class that has the same result type, the same calling convention, the same number of parameters, and
identically typed parameters in each position. By default, each interface method is mapped to a method
of the same name in the implementing class.
type
TMemoryManager = class(TInterfacedObject, IMalloc, IErrorInfo)
function IMalloc.Alloc = Allocate;
procedure IMalloc.Free = Deallocate;
.
.
.
end;
561
maps IMalloc's Alloc and Free methods onto TMemoryManager's Allocate and Deallocate
methods.
A class can also reimplement an entire interface that it inherits from an ancestor class. This involves
relisting the interface in the descendant class' declaration. For example,
type
IWindow = interface
['{00000115-0000-0000-C000-000000000146}']
procedure Draw;
.
.
.
end;
TWindow = class(TInterfacedObject, IWindow)// TWindow implements IWindow
procedure Draw;
.
.
.
end;
TFrameWindow = class(TWindow, IWindow)// TFrameWindow reimplements IWindow
procedure Draw;
.
.
.
end;
Reimplementing an interface hides the inherited implementation of the same interface. Hence method
resolution clauses in an ancestor class have no effect on the reimplemented interface.
562
The implements directive must be the last specifier in the property declaration and can list more than
one interface, separated by commas. The delegate property
• must be of a class or interface type.
• cannot be an array property or have an index specifier.
• must have a read specifier. If the property uses a read method, that method must use the default
register calling convention and cannot be dynamic (though it can be virtual) or specify the message
directive.
The class you use to implement the delegated interface should derive from TAggregationObject.
type
IMyInterface = interface
procedure P1;
procedure P2;
end;
TMyClass = class(TObject, IMyInterface)
FMyInterface: IMyInterface;
property MyInterface: IMyInterface read FMyInterface implements
IMyInterface;
end;
var
MyClass: TMyClass;
MyInterface: IMyInterface;
begin
MyClass := TMyClass.Create;
MyClass.FMyInterface := ...// some object whose class implements
IMyInterface
MyInterface := MyClass;
MyInterface.P1;
end;
563
ambiguities or specify a particular method. An interface cannot be implemented by more than one class-
type property. For example,
type
IMyInterface = interface
procedure P1;
procedure P2;
end;
TMyImplClass = class
procedure P1;
procedure P2;
end;
TMyClass = class(TInterfacedObject, IMyInterface)
FMyImplClass: TMyImplClass;
property MyImplClass: TMyImplClass read FMyImplClass implements
IMyInterface;
procedure IMyInterface.P1 = MyP1;
procedure MyP1;
end;
procedure TMyImplClass.P1;
.
.
.
procedure TMyImplClass.P2;
.
.
.
procedure TMyClass.MyP1;
.
.
.
var
MyClass: TMyClass;
MyInterface: IMyInterface;
begin
MyClass := TMyClass.Create;
MyClass.FMyImplClass := TMyImplClass.Create;
MyInterface := MyClass;
MyInterface.P1; // calls TMyClass.MyP1;
MyInterface.P2; // calls TImplClass.P2;
end;
564
Interface References
If you declare a variable of an interface type, the variable can reference instances of any class that
implements the interface. These topics describe Interface references and related topics.
For example,
type
IAncestor = interface
end;
IDescendant = interface(IAncestor)
procedure P1;
end;
TSomething = class(TInterfacedObject, IDescendant)
procedure P1;
procedure P2;
end;
.
.
.
var
D: IDescendant;
A: IAncestor;
begin
D := TSomething.Create; // works!
A := TSomething.Create; // error
D.P1; // works!
D.P2; // error
end;
In this example, A is declared as a variable of type IAncestor. Because TSomething does not list
IAncestor among the interfaces it implements, a TSomething instance cannot be assigned to A. But if
we changed TSomething's declaration to
565
.
.
the first error would become a valid assignment. D is declared as a variable of type IDescendant.
While D references an instance of TSomething, we cannot use it to access TSomething's P2 method,
since P2 is not a method of IDescendant. But if we changed D's declaration to
D: TSomething;
the second error would become a valid method call.
On the Win32 platform, interface references are typically managed through reference-counting, which
depends on the _AddRef and _Release methods inherited from IInterface. These methods, and
reference counting in general, are not applicable on the .NET platform, which is a garbage collected
environment. Using the default implementation of reference counting, when an object is referenced only
through interfaces, there is no need to destroy it manually; the object is automatically destroyed when
the last reference to it goes out of scope. Some classes implement interfaces to bypass this default
lifetime management, and some hybrid objects use reference counting only when the object does not
have an owner.
To determine whether an interface-type expression references an object, pass it to the standard function
Assigned.
Interface Typecasts
An interface-type expression can be cast to Variant. If the interface is of type IDispatch or a
descendant, the resulting variant has the type code varDispatch. Otherwise, the resulting variant has
the type code varUnknown.
566
A variant whose type code is varEmpty, varUnknown, or varDispatch can be cast to
IInterface. A variant whose type code is varEmpty or varDispatch can be cast to IDispatch.
Interface Querying
You can use the as operator to perform checked interface typecasts. This is known as interface querying,
and it yields an interface-type expression from an object reference or from another interface reference,
based on the actual (runtime) type of the object. An interface query has the form
object as interface
where object is an expression of an interface or variant type or denotes an instance of a class that
implements an interface, and interface is any interface declared with a GUID.
An interface query returns nil if object is nil. Otherwise, it passes the GUID of interface to the
QueryInterface method in object, raising an exception unless QueryInterface returns zero. If
QueryInterface returns zero (indicating that object's class implements interface), the interface query
returns an interface reference to object.
567
Automation Objects (Win32 Only)
An object whose class implements the IDispatch interface (declared in the System unit) is an
Automation object.
Use variants to access Automation objects. When a variant references an Automation object, you can
call the object's methods and read or write to its properties through the variant. To do this, you must
include ComObj in the uses clause of one of your units or your program or library.
type
IStringsDisp = dispinterface
['{EE05DFE2-5549-11D0-9EA9-0020AF3D82DA}']
property ControlDefault[Index: Integer]: OleVariant dispid 0;
default;
function Count: Integer; dispid 1;
property Item[Index: Integer]: OleVariant dispid 2;
procedure Remove(Index: Integer); dispid 3;
procedure Clear; dispid 4;
function Add(Item: OleVariant): Integer; dispid 5;
function _NewEnum: IUnknown; dispid -4;
end;
568
A method declared in a dispatch interface cannot contain directives other than dispid. Parameter and
result types must be automatable. In other words, they must be Byte, Currency, Real, Double, Longint,
Integer, Single, Smallint, AnsiString, WideString, TDateTime, Variant, OleVariant, WordBool, or any
interface type.
The following example illustrates Automation method calls. The CreateOleObject function (defined
in ComObj) returns an IDispatch reference to an Automation object and is assignment-compatible
with the variant Word.
var
Word: Variant;
begin
Word := CreateOleObject('Word.Basic');
Word.FileNew('Normal');
Word.Insert('This is the first line'#13);
Word.Insert('This is the second line'#13);
Word.FileSaveAs('c:\temp\test.txt', 3);
end;
You can pass interface-type parameters to Automation methods.
Variant arrays with an element type of varByte are the preferred method of passing binary data
between Automation controllers and servers. Such arrays are subject to no translation of their data, and
can be efficiently accessed using the VarArrayLock and VarArrayUnlock routines.
569
A positional parameter is simply an expression. A named parameter consists of a parameter identifier,
followed by the := symbol, followed by an expression. Positional parameters must precede any named
parameters in a method call. Named parameters can be specified in any order.
Some Automation servers allow you to omit parameters from a method call, accepting their default
values. For example,
Word.FileSaveAs('test.doc');
Word.FileSaveAs('test.doc', 6);
Word.FileSaveAs('test.doc',,,'secret');
Word.FileSaveAs('test.doc', Password := 'secret');
Word.FileSaveAs(Password := 'secret', Name := 'test.doc');
Automation method call parameters can be of integer, real, string, Boolean, and variant types. A
parameter is passed by reference if the parameter expression consists only of a variable reference, and
if the variable reference is of type Byte, Smallint, Integer, Single, Double, Currency, TDateTime,
AnsiString, WordBool, or Variant. If the expression is not of one of these types, or if it is not just a variable,
the parameter is passed by value. Passing a parameter by reference to a method that expects a value
parameter causes COM to fetch the value from the reference parameter. Passing a parameter by value
to a method that expects a reference parameter causes an error.
Dual Interfaces
A dual interface is an interface that supports both compile-time binding and runtime binding through
Automation. Dual interfaces must descend from IDispatch.
All methods of a dual interface (except from those inherited from IInterface and IDispatch) must
use the safecall convention, and all method parameter and result types must be automatable. (The
automatable types are Byte, Currency, Real, Double, Real48, Integer, Single, Smallint, AnsiString,
ShortString, TDateTime, Variant, OleVariant, and WordBool.)
570
Memory Management
This section describes memory management issues related to programming in Delphi on Win32, and
on .NET.
571
Memory Management on the Win32 Platform
The following material describes how memory management on Win32 is handled, and briefly describes
memory issues of variables.
The memory manager is optimized for applications that allocate large numbers of small- to medium-
sized blocks, as is typical for object-oriented applications and applications that process string data. Other
memory managers, such as the implementations of GlobalAlloc, LocalAlloc, and private heap
support in Windows, typically do not perform well in such situations, and would slow down an application
if they were used directly.
To ensure the best performance, the memory manager interfaces directly with the Win32 virtual memory
API (the VirtualAlloc and VirtualFree functions). The memory manager reserves memory from
the operating system in 1Mb sections of address space, and commits memory as required in 16K
increments. It decommits and releases unused memory in 16K and 1Mb sections. For smaller blocks,
committed memory is further suballocated.
Memory manager blocks are always rounded upward to a 4-byte boundary, and always include a 4-byte
header in which the size of the block and other status bits are stored. This means that memory manager
blocks are always double-word-aligned, which guarantees optimal CPU performance when addressing
the block.
The memory manager maintains two status variables, AllocMemCount and AllocMemSize, which
contain the number of currently allocated memory blocks and the combined size of all currently allocated
memory blocks. Applications can use these variables to display status information for debugging.
The System unit provides two procedures, GetMemoryManager and SetMemoryManager, that allow
applications to intercept low-level memory manager calls. The System unit also provides a function
called GetHeapStatus that returns a record containing detailed memory-manager status information.
Variables
Global variables are allocated on the application data segment and persist for the duration of the
program. Local variables (declared within procedures and functions) reside in an application's stack.
Each time a procedure or function is called, it allocates a set of local variables; on exit, the local variables
are disposed of. Compiler optimization may eliminate variables earlier.
On Win32, an application's stack is defined by two values: the minimum stack size and the maximum
stack size. The values are controlled through the $MINSTACKSIZE and $MAXSTACKSIZE compiler
572
directives, and default to 16,384 (16K) and 1,048,576 (1Mb) respectively. An application is guaranteed
to have the minimum stack size available, and an application's stack is never allowed to grow larger
than the maximum stack size. If there is not enough memory available to satisfy an application's
minimum stack requirement, Windows will report an error upon attempting to start the application.
If a Win32 application requires more stack space than specified by the minimum stack size, additional
memory is automatically allocated in 4K increments. If allocation of additional stack space fails, either
because more memory is not available or because the total size of the stack would exceed the maximum
stack size, an EStackOverflow exception is raised. (Stack overflow checking is completely automatic.
The $S compiler directive, which originally controlled overflow checking, is maintained for backward
compatibility.)
Dynamic variables created with the GetMem or New procedure are heap-allocated and persist until they
are deallocated with FreeMem or Dispose.
Long strings, wide strings, dynamic arrays, variants, and interfaces are heap-allocated, but their memory
is managed automatically.
573
Internal Data Formats
The following topics describe the internal formats of Delphi data types.
Integer Types
The format of an integer-type variable depends on its minimum and maximum bounds.
• If both bounds are within the range 128..127 (Shortint), the variable is stored as a signed byte.
• If both bounds are within the range 0..255 (Byte), the variable is stored as an unsigned byte.
• If both bounds are within the range 32768..32767 (Smallint), the variable is stored as a signed word.
• If both bounds are within the range 0..65535 (Word), the variable is stored as an unsigned word.
• If both bounds are within the range 2147483648..2147483647 (Longint), the variable is stored as
a signed double word.
• If both bounds are within the range 0..4294967295 (Longword), the variable is stored as an
unsigned double word.
• Otherwise, the variable is stored as a signed quadruple word (Int64).
Character Types
A Char, an AnsiChar, or a subrange of a Char type is stored as an unsigned byte. A WideChar is stored
as an unsigned word.
Boolean Types
A Boolean type is stored as a Byte, a ByteBool is stored as a Byte, a WordBool type is stored as a Word,
and a LongBool is stored as a Longint.
A Boolean can assume the values 0 (False) and 1 (True). ByteBool, WordBool, and LongBool types can
assume the values 0 (False) or nonzero (True).
Enumerated Types
An enumerated type is stored as an unsigned byte if the enumeration has no more than 256 values and
the type was declared in the {$Z1} state (the default). If an enumerated type has more than 256 values,
or if the type was declared in the {$Z2} state, it is stored as an unsigned word. If an enumerated type
is declared in the {$Z4} state, it is stored as an unsigned double-word.
Real Types
The real types store the binary representation of a sign (+ or -), an exponent, and a significand. A real
value has the form
574
+/- significand * 2exponent
where the significand has a single bit to the left of the binary decimal point. (That is, 0 <= significand < 2.)
In the figures that follow, the most significant bit is always on the left and the least significant bit on the
right. The numbers at the top indicate the width (in bits) of each field, with the leftmost items stored at
the highest addresses. For example, for a Real48 value, e is stored in the first byte, f in the following
five bytes, and s in the most significant bit of the last byte.
1398
sf e
If e = 0, then v = 0.
The Real48 type can't store denormals, NaNs, and infinities. Denormals become zero when stored in a
Real48, while NaNs and infinities produce an overflow error if an attempt is made to store them in a
Real48.
Note: The Real48 type is not supported on the .NET platform.
1823
sef
575
The Double type
An 8-byte (64-bit) Double number is divided into three fields
11152
se f
115163
se i f
Pointer Types
A Pointer type is stored in 4 bytes as a 32-bit address. The pointer value nil is stored as zero.
576
Short String Types
A string occupies as many bytes as its maximum length plus one. The first byte contains the current
dynamic length of the string, and the following bytes contain the characters of the string.
The length byte and the characters are considered unsigned values. Maximum string length is 255
characters plus a length byte (string[255]).
Offset Contents
-8 32-bit reference-count
-4 length in bytes
0..Length - 1character string
Length NULL character
The NULL character at the end of a long string memory block is automatically maintained by the compiler
and the built-in string handling routines. This makes it possible to typecast a long string directly to a null-
terminated string.
For string constants and literals, the compiler generates a memory block with the same layout as a
dynamically allocated string, but with a reference count of -1. When a long string variable is assigned a
string constant, the string pointer is assigned the address of the memory block generated for the string
constant. The built-in string handling routines know not to attempt to modify blocks that have a reference
count of -1.
577
Offset Contents
The string length is the number of bytes, so it is twice the number of wide characters contained in the
string.
The NULL character at the end of a wide string memory block is automatically maintained by the compiler
and the built-in string handling routines. This makes it possible to typecast a wide string directly to a
null-terminated string.
Set Types
A set is a bit array where each bit indicates whether an element is in the set or not. The maximum number
of elements in a set is 256, so a set never occupies more than 32 bytes. The number of bytes occupied
by a particular set is equal to
where Max and Min are the upper and lower bounds of the base type of the set. The byte number of a
specific element E is
E mod 8
where E denotes the ordinal value of the element. When possible, the compiler stores sets in CPU
registers, but a set always resides in memory if it is larger than the generic Integer type or if the program
contains code that takes the address of the set.
578
dynamically allocated block of memory that contains the array in addition to a 32-bit length indicator and
a 32-bit reference count. The table below shows the layout of a dynamic-array memory block.
Dynamic array memory layout
Offset Contents
-8 32-bit reference-count
-4 32-bit length indicator (number of elements)
0..Length * (size of element) -1array elements
Record Types
When a record type is declared in the {$A+} state (the default), and when the declaration does not
include a packed modifier, the type is an unpacked record type, and the fields of the record are aligned
for efficient access by the CPU. The alignment is controlled by the type of each field and by whether
fields are declared together. Every data type has an inherent alignment, which is automatically computed
by the compiler. The alignment can be 1, 2, 4, or 8, and represents the byte boundary that a value of
the type must be stored on to provide the most efficient access. The table below lists the alignments for
all data types.
Type alignment masks
Type Alignment
To ensure proper alignment of the fields in an unpacked record type, the compiler inserts an unused
byte before fields with an alignment of 2, and up to three unused bytes before fields with an alignment
of 4, if required. Finally, the compiler rounds the total size of the record upward to the byte boundary
specified by the largest alignment of any of the fields.
If two fields share a common type specification, they are packed even if the declaration does not include
the packed modifier and the record type is not declared in the {$A-} state. Thus, for example, given
the following declaration
579
type
TMyRecord = record
A, B: Extended;
C: Extended;
end;
A and B are packed (aligned on byte boundaries) because they share the same type specification. The
compiler pads the structure with unused bytes to ensure that C appears on a quadword boundary.
When a record type is declared in the {$A-} state, or when the declaration includes the packed modifier,
the fields of the record are not aligned, but are instead assigned consecutive offsets. The total size of
such a packed record is simply the size of all the fields. Because data alignment can change, it's a good
idea to pack any record structure that you intend to write to disk or pass in memory to another module
compiled using a different version of the compiler.
File Types
File types are represented as records. Typed files and untyped files occupy 332 bytes, which are laid
out as follows:
type
TFileRec = packed record
Handle: Integer;
Mode: word;
Flags: word;
case Byte of
0: (RecSize: Cardinal);
1: (BufSize: Cardinal;
BufPos: Cardinal;
BufEnd: Cardinal;
BufPtr: PChar;
OpenFunc: Pointer;
InOutFunc: Pointer;
FlushFunc: Pointer;
CloseFunc: Pointer;
UserData: array[1..32] of Byte;
Name: array[0..259] of Char; );
end;
Text files occupy 460 bytes, which are laid out as follows:
type
TTextBuf = array[0..127] of Char;
TTextRec = packed record
Handle: Integer;
580
Mode: word;
Flags: word;
BufSize: Cardinal;
BufPos: Cardinal;
BufEnd: Cardinal;
BufPtr: PChar;
OpenFunc: Pointer;
InOutFunc: Pointer;
FlushFunc: Pointer;
CloseFunc: Pointer;
UserData: array[1..32] of Byte;
Name: array[0..259] of Char;
Buffer: TTextBuf;
end;
Handle contains the file's handle (when the file is open).
const
fmClosed = $D7B0;
fmInput= $D7B1;
fmOutput = $D7B2;
fmInOut= $D7B3;
where fmClosed indicates that the file is closed, fmInput and fmOutput indicate a text file that has been
reset (fmInput) or rewritten (fmOutput), fmInOut indicates a typed or untyped file that has been reset or
rewritten. Any other value indicates that the file variable is not assigned (and hence not initialized).
The UserData field is available for user-written routines to store data in.
Name contains the file name, which is a sequence of characters terminated by a null character (#0).
For typed files and untyped files, RecSize contains the record length in bytes, and the Private field is
unused but reserved.
For text files, BufPtr is a pointer to a buffer of BufSize bytes, BufPos is the index of the next character
in the buffer to read or write, and BufEnd is a count of valid characters in the buffer. OpenFunc,
InOutFunc, FlushFunc, and CloseFunc are pointers to the I/O routines that control the file; see Device
functions. Flags determines the line break style as follows:
581
Procedural Types
A procedure pointer is stored as a 32-bit pointer to the entry point of a procedure or function. A method
pointer is stored as a 32-bit pointer to the entry point of a method, followed by a 32-bit pointer to an object.
Class Types
A class-type value is stored as a 32-bit pointer to an instance of the class, which is called an object. The
internal data format of an object resembles that of a record. The object's fields are stored in order of
declaration as a sequence of contiguous variables. Fields are always aligned, corresponding to an
unpacked record type. Any fields inherited from an ancestor class are stored before the new fields
defined in the descendant class.
The first 4-byte field of every object is a pointer to the virtual method table (VMT) of the class. There is
exactly one VMT per class (not one per object); distinct class types, no matter how similar, never share
a VMT. VMT's are built automatically by the compiler, and are never directly manipulated by a program.
Pointers to VMT's, which are automatically stored by constructor methods in the objects they create,
are also never directly manipulated by a program.
The layout of a VMT is shown in the following table. At positive offsets, a VMT consists of a list of 32-
bit method pointersone per user-defined virtual method in the class typein order of declaration. Each
slot contains the address of the corresponding virtual method's entry point. This layout is compatible
with a C++ v-table and with COM. At negative offsets, a VMT contains a number of fields that are internal
to Delphi's implementation. Applications should use the methods defined in TObject to query this
information, since the layout is likely to change in future implementations of the Delphi language.
Virtual method table layout
582
-28 Pointer entry point of AfterConstruction method
-24 Pointer entry point of BeforeDestruction method
-20 Pointer entry point of Dispatch method
-16 Pointer entry point of DefaultHandler method
-12 Pointer entry point of NewInstance method
-8 Pointer entry point of FreeInstance method
-4 Pointer entry point of Destroy destructor
0 Pointer entry point of first user-defined virtual method
4 Pointer entry point of second user-defined virtual method
Variant Types
A variant is stored as a 16-byte record that contains a type code and a value (or a reference to a value)
of the type given by the code. The System and Variants units define constants and types for variants.
The TVarData type represents the internal structure of a Variant variable (on Windows, this is identical
to the Variant type used by COM and the Win32 API). The TVarData type can be used in typecasts of
Variant variables to access the internal structure of a variable.
The VType field of a TVarData record contains the type code of the variant in the lower twelve bits (the
bits defined by the varTypeMask constant). In addition, the varArray bit may be set to indicate that the
variant is an array, and the varByRef bit may be set to indicate that the variant contains a reference as
opposed to a value.
The Reserved1, Reserved2, and Reserved3 fields of a TVarData record are unused.
The contents of the remaining eight bytes of a TVarData record depend on the VType field. If neither
the varArray nor the varByRef bits are set, the variant contains a value of the given type.
If the varArray bit is set, the variant contains a pointer to a TVarArray structure that defines an array.
The type of each array element is given by the varTypeMask bits in the VType field.
If the varByRef bit is set, the variant contains a reference to a value of the type given by the
varTypeMask and varArray bits in the VType field.
The varString type code is private. Variants containing a varString value should never be passed to a
non-Delphi function. On Win32, Delphi's Automation support automatically converts varString variants
to varOleStr variants before passing them as parameters to external functions.
583
Memory Management Issues on the .NET Platform
The .NET Common Language Runtime is a garbage-collected environment. This means the
programmer is freed (for the most part) from worrying about memory allocation and deallocation. Broadly
speaking, after you allocate memory, the CLR determines when it is safe to free that memory. "Safe to
free" means that no more references to that memory exist.
Constructors
In Delphi for .NET, a constructor must always call an inherited constructor before it may access or
initialize any inherited class members. The compiler generates an error if your constructor code does
not call the inherited constructor (a valid situation in Delphi for Win32), but it is important to examine
your constructors to make sure that you do not access any inherited class fields, directly or indirectly,
before the call to the inherited constructor.
Note: A constructor can initialize fields from its own class, prior to calling the inherited
constructor.
Finalization
Every class in the .NET Framework (including VCL.NET classes) inherits a method called Finalize. The
garbage collector calls the Finalize method when the memory for the object is about to be freed. Since
the method is called by the garbage collector, you have no control over when it is called. The
asynchronous nature of finalization is a problem for objects that open resources such as file handles
and database connections, because the Finalize method might not be called for some time, leaving
these connections open.
To add a finalizer to a class, override the strict protectedFinalize procedure that is inherited from
TObject. The .NET platform places limits on what you can do in a finalizer, because it is called when
the garbage collector is cleaning up objects. The finalizer may execute in a different thread than the
thread the object was was created in. A finalizer cannot allocate new memory, and cannot make calls
outside of itself. If your class has references to other objects, a finalizer can refer to them (that is, their
memory is guaranteed not to have been freed yet), but be aware that their state is undefined, as you do
not know whether they have been finalized yet.
When a class has a finalizer, the CLR must add newly instantiated objects of the class to the finalization
list. Further, objects with finalizers tend to persist in memory longer, as they are not freed when the
garbage collector first determines that they are no longer actively referenced. If the object has references
to other objects, those objects are also not freed right away (even if they don’t have finalizers
584
themselves), but must also persist in memory until the original object is finalized. Therefore, finalizers
do impart a fair amount of overhead in terms of memory consumption and execution performance, so
they should be used judiciously.
It is a good practice to restrict finalizers to small objects that represent unmanaged resources. Classes
that use these resources can then hold a reference to the small object with the finalizer. In this way, big
classes, and classes that reference many other classes, do not hoard memory because of a finalizer.
Another good practice is to suppress finalizers when a particular resource has already been released
in a destructor. After freeing the resources, you can call SuppressFinalize, which causes the CLR to
remove the object from the finalization list. Be careful not to call SuppressFinalize with a nil reference,
as that causes a runtime exception.
Delphi for .NET classes are free to use the Finalize method for freeing system resources, however the
recommended method is to implement the dispose pattern. The Delphi for .NET compiler recognizes a
very specific destructor pattern in your class, and implements the IDisposable interface for you. This
enables you to continue writing new code for the .NET platform the same way you always have, while
allowing much of your existing Win32 Delphi code to run in the garbage collected environment of the
CLR.
TMyClass = class(TObject)
destructor Destroy; override;
end;
Your destructor must fit this pattern exactly:
• The name of the destructor must be Destroy.
• The keyword override must be specified.
• The destructor cannot take any parameters.
585
In the compiler's implementation of the dispose pattern, the Free method is written so that if the class
implements the IDisposable interface (which it does), then the Dispose method is called, which in turn
calls your destructor.
You can still implement the IDisposable interface directly, if you choose. However, the compiler's
automatic implementation of the Free-Dispose-Destroy mechanism cannot coexist with your
implementation of IDisposable. The two methods of implementing IDisposable are mutually exclusive.
You must choose to either implement IDisposable directly, or equip your class with the familiar
destructor Destroy; override pattern and rely on the compiler to do the rest. The Free method
will call Dispose in either case, but if you implement Dispose yourself, you must call your destructor
yourself. If you want to implement IDisposable yourself, your destructor cannot be called Destroy.
Note: You can declare destructors with other names; the compiler only provides the
IDisposable implementation when the destructor fits the above pattern.
The Dispose method is not called automatically; the Free method must be called in order for Dispose
to be called. If an object is freed by the garbage collector because there are no references to it, but you
did not explicitly call Free on the object, the object will be freed, but the destructor will not execute.
Note: When the garbage collector frees the memory used by an object, it also reclaims
the memory used by all fields of the object instance as well. This means the most
common reason for implementing destructors in Delphi for Win32 - to release allocated
memory - no longer applies. However, in most cases, unmanaged resources such as
window handles or file handles still need to be released.
To eliminate the possibility of destructors being called more than once, the Delphi for .NET compiler
introduces a field called DisposeCount into every class declaration. If the class already has a field by
this name, the name collision will cause the compiler to produce a syntax error in the destructor.
Unit finalization is subject to the same constraints and difficulties as the Finalize method of objects.
Specifically, unit finalization is asynchronous, and, there no way to determine when it will happen (or if
it will happen, though under most circumstances, it will).
Typical tasks performed in a unit finalization include freeing global objects, unregistering objects that
are used by other units, and freeing resources. Because .NET is a memory managed environment, the
garbage collector will free global objects even if the unit finalization section is not called. The units in an
application domain are loaded and unloaded together, so you do not need to worry about unregistering
objects. All units that can possibly refer to each other (even in different assemblies) are released at the
same time. Since object references do not cross application domains, there is no danger of something
keeping a dangling reference to an object type or code that has been unloaded from memory.
586
Freeing resources (such as file handles or window handles) is the most important consideration in unit
finalization. Because unit finalization sections are not guaranteed to be called, you may want to rewrite
your code to handle this issue using finalizers rather than relying on the unit finalization.
The main points to keep in mind for unit initialization and finalization on the .NET platform are:
1. The Finalize method is called asynchronously (both for objects, and for units).
2. Finalization and destructors are used to free unmanaged resources such as file handles. You do
not need to destroy object member variables; the garbage collector takes care of this for you.
3. Classes should rely on the compiler's implementation of IDisposable, and provide a destructor
called Destroy.
4. If a class implements IDisposable itself, it cannot have a destructor called Destroy.
5. Reference counting is deprecated. Try to use the destructor Destroy; override; pattern
wherever possible.
6. Unit initialization should not depend on side effects produced by initialization of dependent units.
The Delphi for .NET compiler uses CLS-compliant class constructors to implement unit initialization
hooks. The CLR requires that every object type have a class constructor. These constructors, or type
initializers, are guaranteed to be executed at most one time. Class constructors are executed at most
one time, because in order for the type to be loaded, it must be used. That is, the assembly containing
a type will not be loaded until the type is actually used at runtime. If the assembly is never loaded, its
unit initialization section will never run.
Circular unit references also impact the unit initialization process. If unit A uses unit B, and unit B then
uses unit A in its implementation section, the order of unit initialization is undefined. To fully understand
the possibilities, it is helpful to look at the process one step at a time.
1. Unit A's initialization section uses a type from unit B. If this is the first reference to the type, the CLR
will load its assembly, triggering the unit initialization of unit B.
2. As a consequence, loading and initializing unit B occurs before unit A's initialization section has
completed execution. Note this is a change from how unit initialization works under Win32.
3. Suppose that unit B's initialization is in progress, and that a type from unit A is used. Unit A has not
completed initialization, and such a reference could cause an access violation.
The unit initialization should only use types defined within that unit. Using types from outside the unit
will impact unit initialization, and could cause an access violation, as noted above.
587
Unit initialization for DLLs happens automatically; it is triggered when a type within the DLL is referenced.
Applications created with other .NET languages can use Delphi for .NET assemblies without concern
for the details of unit initialization.
588
Program Control
This section describes how parameters are passed to procedures and functions.
589
Program Control
The concepts of passing parameters and function result processing are important to understand before
you undertake your application projects Treatment of parameters and function results is determined by
several factors, including calling conventions, parameter semantics, and the type and size of the value
being passed.
Passing Parameters
Parameters are transferred to procedures and functions via CPU registers or the stack, depending on
the routine's calling convention. For information about calling conventions, see the topic on Calling
Conventions.
Value and constant (const) parameters are passed by value or by reference, depending on the type and
size of the parameter:
• An ordinal parameter is passed as an 8-bit, 16-bit, 32-bit, or 64-bit value, using the same format as
a variable of the corresponding type.
• A real parameter is always passed on the stack. A Single parameter occupies 4 bytes, and a Double,
Comp, or Currency parameter occupies 8 bytes. A Real48 occupies 8 bytes, with the Real48 value
stored in the lower 6 bytes. An Extended occupies 12 bytes, with the Extended value stored in the
lower 10 bytes.
• A short-string parameter is passed as a 32-bit pointer to a short string.
• A long-string or dynamic-array parameter is passed as a 32-bit pointer to the dynamic memory block
allocated for the long string. The value nil is passed for an empty long string.
• A pointer, class, class-reference, or procedure-pointer parameter is passed as a 32-bit pointer.
• A method pointer is passed on the stack as two 32-bit pointers. The instance pointer is pushed
before the method pointer so that the method pointer occupies the lowest address.
• Under the register and pascal conventions, a variant parameter is passed as a 32bit pointer to a
Variant value.
• Sets, records, and static arrays of 1, 2, or 4 bytes are passed as 8-bit, 16-bit, and 32bit values.
Larger sets, records, and static arrays are passed as 32-bit pointers to the value. An exception to
590
this rule is that records are always passed directly on the stack under the cdecl, stdcall, and safecall
conventions; the size of a record passed this way is rounded upward to the nearest double-word
boundary.
• An open-array parameter is passed as two 32-bit values. The first value is a pointer to the array
data, and the second value is one less than the number of elements in the array.
When two parameters are passed on the stack, each parameter occupies a multiple of 4 bytes (a whole
number of double words). For an 8-bit or 16-bit parameter, even though the parameter occupies only a
byte or a word, it is passed as a double word. The contents of the unused parts of the double word are
undefined.
Register Convention
Under the register convention, up to three parameters are passed in CPU registers, and the rest (if any)
are passed on the stack. The parameters are passed in order of declaration (as with the pascal
convention), and the first three parameters that qualify are passed in the EAX, EDX, and ECX registers,
in that order. Real, method-pointer, variant, Int64, and structured types do not qualify as register
parameters, but all other parameters do. If more than three parameters qualify as register parameters,
the first three are passed in EAX, EDX, and ECX, and the remaining parameters are pushed onto the
stack in order of declaration. For example, given the declaration
591
Note: Delphi language procedures and functions are generally invoked with the
assumption that the FPU stack is empty: The compiler tries to use all eight FPU stack
entries when it generates code.
When working with the MMX and XMM instructions, be sure to preserve the values of the xmm and mm
registers. Delphi functions are invoked with the assumption that the x87 FPU data registers are available
for use by x87 floating point instructions. That is, the compiler assumes that the EMMS/FEMMS
instruction has been called after MMX operations. Delphi functions do not make any assumptions about
the state and content of xmm registers. They do not guarantee that the content of xmm registers is
unchanged.
592
• Under the cdecl, stdcall, and safecall conventions, Self behaves as if it were declared before all
other parameters, but after the additional var parameter (if any) passed for a function result. It is
therefore the last to be pushed, except for the additional var parameter.
Constructors and destructors use the same calling conventions as other methods, except that an
additional Boolean flag parameter is passed to indicate the context of the constructor or destructor call.
A value of False in the flag parameter of a constructor call indicates that the constructor was invoked
through an instance object or using the inheritedinherited keyword. In this case, the constructor behaves
like an ordinary method. A value of True in the flag parameter of a constructor call indicates that the
constructor was invoked through a class reference. In this case, the constructor creates an instance of
the class given by Self, and returns a reference to the newly created object in EAX.
A value of False in the flag parameter of a destructor call indicates that the destructor was invoked using
the inherited keyword. In this case, the destructor behaves like an ordinary method. A value of True in
the flag parameter of a destructor call indicates that the destructor was invoked through an instance
object. In this case, the destructor deallocates the instance given by Self just before returning.
The flag parameter behaves as if it were declared before all other parameters. Under the register
convention, it is passed in the DL register. Under the pascal convention, it is pushed before all other
parameters. Under the cdecl, stdcall, and safecall conventions, it is pushed just before the Self
parameter.
Since the DL register indicates whether the constructor or destructor is the outermost in the call stack,
you must restore the value of DL before exiting so that BeforeDestruction or AfterConstruction can be
called properly.
When implemented properly, an exit procedure is part of a chain of exit procedures. The procedures
are executed in reverse order of installation, ensuring that the exit code of one unit isn't executed before
the exit code of any units that depend on it. To keep the chain intact, you must save the current contents
593
of ExitProc before pointing it to the address of your own exit procedure. Also, the first statement in
your exit procedure must reinstall the saved value of ExitProc.
var
ExitSave: Pointer;
procedure MyExit;
begin
ExitProc := ExitSave; // always restore old vector first
.
.
.
end;
begin
ExitSave := ExitProc;
ExitProc := @MyExit;
.
.
.
end.
On entry, the code saves the contents of ExitProc in ExitSave, then installs the MyExit procedure.
When called as part of the termination process, the first thing MyExit does is reinstall the previous exit
procedure.
The termination routine in the runtime library keeps calling exit procedures until ExitProc becomes
nilnil. To avoid infinite loops, ExitProc is set to nil before every call, so the next exit procedure is called
only if the current exit procedure assigns an address to ExitProc. If an error occurs in an exit
procedure, it is not called again.
An exit procedure can learn the cause of termination by examining the ExitCode integer variable and
the ErrorAddr pointer variable. In case of normal termination, ExitCode is zero and ErrorAddr is
nil. In case of termination through a call to Halt, ExitCode contains the value passed to Halt and
ErrorAddr is nil. In case of termination due to a runtime error, ExitCode contains the error code and
ErrorAddr contains the address of the invalid statement.
The last exit procedure (the one installed by the runtime library) closes the Input and Output files. If
ErrorAddr is not nil, it outputs a runtime error message. To output your own runtime error message,
install an exit procedure that examines ErrorAddr and outputs a message if it's not nil; before returning,
set ErrorAddr to nil so that the error is not reported again by other exit procedures.
Once the runtime library has called all exit procedures, it returns to the operating system, passing the
value stored in ExitCode as a return code.
594
Inline Assembly Code (Win32 Only)
This section describes the use of the inline assembler on the Win32 platform.
595
Using Inline Assembly Code (Win32 Only)
The built-in assembler allows you to write assembly code within Delphi programs. The inline assembler
is available only on the Win32 Delphi compiler. It has the following features:
• Allows for inline assembly.
• Supports all instructions found in the Intel Pentium III, Intel MMX extensions, Streaming SIMD
Extensions (SSE), and the AMD Athlon (including 3D Now!).
• Provides no macro support, but allows for pure assembly function procedures.
• Permits the use of Delphi identifiers, such as constants, types, and variables in assembly
statements.
As an alternative to the built-in assembler, you can link to object files that contain external procedures
and functions. See the topic on External declarations for more information. If you have external assembly
code that you want to use in your applications, you should consider rewriting it in the Delphi language
or minimally reimplement it using the inline assembler.
Comments in an asm statement must be in Delphi style. A semicolon does not indicate that the rest of
the line is a comment.
The reserved word inline and the directive assembler are maintained for backward compatibility only.
They have no effect on the compiler.
Using Registers
In general, the rules of register use in an asm statement are the same as those of an external procedure
or function. An asm statement must preserve the EDI, ESI, ESP, EBP, and EBX registers, but can freely
modify the EAX, ECX, and EDX registers. On entry to an asm statement, EBP points to the current stack
frame and ESP points to the top of the stack. Except for ESP and EBP, an asm statement can assume
nothing about register contents on entry to the statement.
596
Understanding Assembler Syntax (Win32 Only)
The inline assembler is available only on the Win32 Delphi compiler. The following material describes
the elements of the assembler syntax necessary for proper use.
• Assembler Statement Syntax
• Labels
• Instruction Opcodes
• Assembly Directives
• Operands
Comments are allowed between assembly statements, but not within them. For example,
Labels
Labels are used in built-in assembly statements as they are in the Delphi languageby writing the label
and a colon before a statement. There is no limit to a label's length. As in Delphi, labels must be declared
in a label declaration part in the block containing the asm statement. The one exception to this rule is
local labels.
Local labels are labels that start with an at-sign (@). They consist of an at-sign followed by one or more
letters, digits, underscores, or at-signs. Use of local labels is restricted to asm statements, and the scope
of a local label extends from the asm reserved word to the end of the asm statement that contains it. A
local label doesn't have to be declared.
597
Instruction Opcodes
The built-in assembler supports all of the Intel-documented opcodes for general application use. Note
that operating system privileged instructions may not be supported. Specifically, the following families
of instructions are supported:
• Pentium family
• Pentium Pro and Pentium II
• Pentium III
• Pentium 4
For an unconditional jump instruction (JMP), the built-in assembler generates a short jump (one-byte
opcode followed by a one-byte displacement) if the distance to the target label is 128 to 127 bytes.
Otherwise it generates a near jump (one-byte opcode followed by a two-byte displacement).
For a conditional jump instruction, a short jump (one-byte opcode followed by a one-byte displacement)
is generated if the distance to the target label is 128 to 127 bytes. Otherwise, the built-in assembler
generates a short jump with the inverse condition, which jumps over a near jump to the target label (five
bytes in total). For example, the assembly statement
JC Stop
where Stop isn't within reach of a short jump, is converted to a machine code sequence that corresponds
to this:
JNC Skip
JMP Stop
Skip:
598
Jumps to the entry points of procedures and functions are always near.
Assembly Directives
The built-in assembler supports three assembly define directives: DB (define byte), DW (define word),
and DD (define double word). Each generates data corresponding to the comma-separated operands
that follow the directive.
The DB directive generates a sequence of bytes. Each operand can be a constant expression with a
value between 128 and 255, or a character string of any length. Constant expressions generate one
byte of code, and strings generate a sequence of bytes with values corresponding to the ASCII code of
each character.
The DW directive generates a sequence of words. Each operand can be a constant expression with a
value between 32,768 and 65,535, or an address expression. For an address expression, the built-in
assembler generates a near pointer, a word that contains the offset part of the address.
The DD directive generates a sequence of double words. Each operand can be a constant expression
with a value between 2,147,483,648 and 4,294,967,295, or an address expression. For an address
expression, the built-in assembler generates a far pointer, a word that contains the offset part of the
address, followed by a word that contains the segment part of the address.
The data generated by the DB, DW, and DD directives is always stored in the code segment, just like
the code generated by other built-in assembly statements. To generate uninitialized or initialized data
in the data segment, you should use Delphi var or const declarations.
asm
DB
FFH
{ One byte }
DB
0,99
{ Two bytes }
DB
'A'
{ Ord('A') }
DB 'Hello world...',0DH,0AH { String followed by CR/LF }
DB 12,
'string' { Delphi
style string }
DW
0FFFFH
{ One word }
599
DW
0,9999
{ Two words }
DW
'A'
{ Same as DB 'A',0 }
DW
'BA'
{ Same as DB 'A','B' }
DW
MyVar
{ Offset of MyVar }
DW
MyProc
{ Offset of MyProc }
DD
0FFFFFFFFH
{ One double-word }
DD
0,999999999
{ Two double-words }
DD
'A'
{ Same as DB 'A',0,0,0 }
DD
'DCBA'
{ Same as DB 'A','B','C','D' }
DD
MyVar
{ Pointer to MyVar }
DD
MyProc
{ Pointer to MyProc }
end;
When an identifier precedes a DB, DW , or DD directive, it causes the declaration of a byte-, word-, or
double-word-sized variable at the location of the directive. For example, the assembler allows the
following:
ByteVar DB ?
WordVar DW ?
IntVar DD ?
.
.
.
MOV AL,ByteVar
MOV BX,WordVar
MOV ECX,IntVar
600
The built-in assembler doesn't support such variable declarations. The only kind of symbol that can be
defined in an inline assembly statement is a label. All variables must be declared using Delphi syntax;
the preceding construction can be replaced by
var
ByteVar: Byte;
WordVar: Word;
IntVar: Integer;
.
.
.
asm
MOV AL,ByteVar
MOV BX,WordVar
MOV ECX,IntVar
end;
SMALL and LARGE can be used determine the width of a displacement:
SMALL can be used to save space. The following example generates an address size override and a
2-byte address (in total three bytes)
Two additional directives allow assembly code to access dynamic and virtual methods: VMTOFFSET
and DMTINDEX.
VMTOFFSET retrieves the offset in bytes of the virtual method pointer table entry of the virtual method
argument from the beginning of the virtual method table (VMT). This directive needs a fully specified
class name with a method name as a parameter (for example, TExample.VirtualMethod), or an interface
name and an interface method name.
DMTINDEX retrieves the dynamic method table index of the passed dynamic method. This directive
also needs a fully specified class name with a method name as a parameter, for example, TExample.
601
DynamicMethod. To invoke the dynamic method, call System.@CallDynaInst with the (E)SI register
containing the value obtained from DMTINDEX.
Note: Methods with the message directive are implemented as dynamic methods and
can also be called using the DMTINDEX technique. For example:
TMyClass = class
procedure x; message MYMESSAGE;
end;
The following example uses both DMTINDEX and VMTOFFSET to access dynamic and virtual methods:
program Project2;
type
TExample = class
procedure DynamicMethod; dynamic;
procedure VirtualMethod; virtual;
end;
procedure TExample.DynamicMethod;
begin
end;
procedure TExample.VirtualMethod;
begin
end;
end;
602
procedure CallVirtualMethod(e: TExample);
asm
// Instance pointer needs to be in EAX
MOV EAX, e
end;
var
e: TExample;
begin
e := TExample.Create;
try
CallDynamicMethod(e);
CallVirtualMethod(e);
finally
e.Free;
end;
end.
Operands
Inline assembler operands are expressions that consist of constants, registers, symbols, and operators.
603
Reserved words always take precedence over user-defined identifiers. For example,
var
Ch: Char;
.
.
.
asm
MOV CH, 1
end;
loads 1 into the CH register, not into the Ch variable. To access a user-defined symbol with the same
name as a reserved word, you must use the ampersand (&) override operator:
MOV&Ch, 1
It is best to avoid user-defined identifiers with the same names as built-in assembler reserved words.
604
Assembly Expressions (Win32 Only)
The built-in assembler evaluates all expressions as 32-bit integer values. It doesn't support floating-
point and string values, except string constants. The inline assembler is available only on the Win32
Delphi compiler.
Expressions are built from expression elements and operators, and each expression has an associated
expression class and expression type. This topic covers the following material:
• Differences between Delphi and Assembler Expressions
• Expression Elements
• Expression Classes
• Expression Types
• Expression Operators
const
X = 10;
Y = 20;
var
Z: Integer;
the following is a valid statement.
asm
MOV Z,X+Y
end;
Because both X and Y are constants, the expression X + Y is a convenient way of writing the constant
30, and the resulting instruction simply moves of the value 30 into the variable Z. But if X and Y are
variables
var
X, Y: Integer;
the built-in assembler cannot compute the value of X + Y at compile time. In this case, to move the
sum of X and Y into Z you would use
605
asm
MOV EAX,X
ADD EAX,Y
MOV Z,EAX
end;
In a Delphi expression, a variable reference denotes the contents of the variable. But in an assembler
expression, a variable reference denotes the address of the variable. In Delphi the expression X + 4
(where X is a variable) means the contents of X plus 4, while to the built-in assembler it means the
contents of the word at the address four bytes higher than the address of X. So, even though you are
allowed to write
asm
MOV EAX,X+4
end;
this code doesn't load the value of X plus 4 into AX; instead, it loads the value of a word stored four
bytes beyond X. The correct way to add 4 to the contents of X is
asm
MOV EAX,X
ADD EAX,4
end;
Expression Elements
The elements of an expression are constants, registers, and symbols.
Numeric Constants
Numeric constants must be integers, and their values must be between 2,147,483,648 and
4,294,967,295.
By default, numeric constants use decimal notation, but the built-in assembler also supports binary,
octal, and hexadecimal. Binary notation is selected by writing a B after the number, octal notation by
writing an O after the number, and hexadecimal notation by writing an H after the number or a $ before
the number.
Numeric constants must start with one of the digits 0 through 9 or the $ character. When you write a
hexadecimal constant using the H suffix, an extra zero is required in front of the number if the first
significant digit is one of the digits A through F. For example, 0BAD4H and $BAD4 are hexadecimal
constants, but BAD4H is an identifier because it starts with a letter.
606
String Constants
String constants must be enclosed in single or double quotation marks. Two consecutive quotation
marks of the same type as the enclosing quotation marks count as only one character. Here are some
examples of string constants:
'Z'
'Delphi'
'Linux'
"That's all folks"
'"That''s all folks," he said.'
'100'
'"'
"'"
String constants of any length are allowed in DB directives, and cause allocation of a sequence of bytes
containing the ASCII values of the characters in the string. In all other cases, a string constant can be
no longer than four characters and denotes a numeric value which can participate in an expression. The
numeric value of a string constant is calculated as
String Value
'a' 00000061H
'ba' 00006261H
'cba' 00636261H
'dcba' 64636261H
'a ' 00006120H
' a' 20202061H
'a' * 2 000000E2H
'a'-'A' 00000020H
not 'a' FFFFFF9EH
Registers
The following reserved symbols denote CPU registers in the inline assembler:
607
CPU registers
32-bit general purposeEAX EBX ECX EDX32-bit pointer or index ESP EBP ESI EDI
16-bit general purposeAX BX CX DX 16-bit pointer or index SP BP SI DI
8-bit low registers AL BL CL DL 16-bit segment registers CS DS SS ES
32-bit segment registers FS GS
8-bit high registers AH BH CH DH Coprocessor register stackST
When an operand consists solely of a register name, it is called a register operand. All registers can be
used as register operands, and some registers can be used in other contexts.
The base registers (BX and BP) and the index registers (SI and DI) can be written within square brackets
to indicate indexing. Valid base/index register combinations are [BX], [BP], [SI], [DI], [BX+SI], [BX+DI],
[BP+SI], and [BP+DI]. You can also index with all the 32-bit registersfor example, [EAX+ECX], [ESP],
and [ESP+EAX+5].
The segment registers (ES, CS, SS, DS, FS, and GS) are supported, but segments are normally not
useful in 32-bit applications.
The symbol ST denotes the topmost register on the 8087 floating-point register stack. Each of the eight
floating-point registers can be referred to using ST(X), where X is a constant between 0 and 7 indicating
the distance from the top of the register stack.
Symbols
The built-in assembler allows you to access almost all Delphi identifiers in assembly language
expressions, including constants, types, variables, procedures, and functions. In addition, the built-in
assembler implements the special symbol @Result, which corresponds to the Result variable within the
body of a function. For example, the function
608
The following symbols cannot be used in asm statements:
• Standard procedures and functions (for example, WriteLn and Chr).
• String, floating-point, and set constants (except when loading registers).
• Labels that aren't declared in the current block.
• The @Result symbol outside of functions.
The following table summarizes the kinds of symbol that can be used in asm statements.
Symbols recognized by the built-in assembler
With optimizations disabled, local variables (variables declared in procedures and functions) are always
allocated on the stack and accessed relative to EBP, and the value of a local variable symbol is its signed
offset from EBP. The assembler automatically adds [EBP] in references to local variables. For example,
given the declaration
MOV EAX,Count
assembles into MOV EAX,[EBP4].
The built-in assembler treats var parameters as a 32-bit pointers, and the size of a var parameter is
always 4. The syntax for accessing a var parameter is different from that for accessing a value
parameter. To access the contents of a var parameter, you must first load the 32-bit pointer and then
access the location it points to. For example,
609
MOV EDX,Y
ADD EAX,[EDX]
MOV @Result,EAX
end;
end;
Identifiers can be qualified within asm statements. For example, given the declarations
type
TPoint = record
X, Y: Integer;
end;
TRect = record
A, B: TPoint;
end;
var
P: TPoint;
R: TRect;
the following constructions can be used in an asm statement to access fields.
MOV EAX,P.X
MOV EDX,P.Y
MOV ECX,R.A.X
MOV EBX,R.B.Y
A type identifier can be used to construct variables on the fly. Each of the following instructions generates
the same machine code, which loads the contents of [EDX] into EAX.
Expression Classes
The built-in assembler divides expressions into three classes: registers, memory references, and
immediate values.
An expression that consists solely of a register name is a register expression. Examples of register
expressions are AX, CL, DI, and ES. Used as operands, register expressions direct the assembler to
generate instructions that operate on the CPU registers.
Expressions that denote memory locations are memory references. Delphi's labels, variables, typed
constants, procedures, and functions belong to this category.
610
Expressions that aren't registers and aren't associated with memory locations are immediate values.
This group includes Delphi's untyped constants and type identifiers.
Immediate values and memory references cause different code to be generated when used as
operands. For example,
const
Start = 10;
var
Count: Integer;
.
.
.
asm
MOV EAX,Start { MOV EAX,xxxx }
MOV EBX,Count { MOV EBX,[xxxx] }
MOV ECX,[Start] { MOV ECX,[xxxx] }
MOV EDX,OFFSET Count { MOV EDX,xxxx }
end;
Because Start is an immediate value, the first MOV is assembled into a move immediate instruction.
The second MOV, however, is translated into a move memory instruction, as Count is a memory
reference. In the third MOV, the brackets convert Start into a memory reference (in this case, the word
at offset 10 in the data segment). In the fourth MOV, the OFFSET operator converts Count into an
immediate value (the offset of Count in the data segment).
The brackets and OFFSET operator complement each other. The following asm statement produces
identical machine code to the first two lines of the previous asm statement.
asm
MOV EAX,OFFSET [Start]
MOV EBX,[OFFSET Count]
end;
Memory references and immediate values are further classified as either relocatable or absolute.
Relocation is the process by which the linker assigns absolute addresses to symbols. A relocatable
expression denotes a value that requires relocation at link time, while an absolute expression denotes
a value that requires no such relocation. Typically, expressions that refer to labels, variables,
procedures, or functions are relocatable, since the final address of these symbols is unknown at compile
time. Expressions that operate solely on constants are absolute.
The built-in assembler allows you to carry out any operation on an absolute value, but it restricts
operations on relocatable values to addition and subtraction of constants.
611
Expression Types
Every built-in assembler expression has a typeor, more correctly, a size, because the assembler regards
the type of an expression simply as the size of its memory location. For example, the type of an Integer
variable is four, because it occupies 4 bytes. The built-in assembler performs type checking whenever
possible, so in the instructions
var
QuitFlag: Boolean;
OutBufPtr: Word;
.
.
.
asm
MOV AL,QuitFlag
MOV BX,OutBufPtr
end;
the assembler checks that the size of QuitFlag is one (a byte), and that the size of OutBufPtr is two
(a word). The instruction
MOV DL,OutBufPtr
produces an error because DL is a byte-sized register and OutBufPtr is a word. The type of a memory
reference can be changed through a typecast; these are correct ways of writing the previous instruction:
In some cases, a memory reference is untyped. One example is an immediate value (Buffer) enclosed
in square brackets:
612
In cases where the type can't be determined from another operand, the built-in assembler requires an
explicit typecast. For example,
Symbol Type
BYTE 1
WORD 2
DWORD 4
QWORD 8
TBYTE 10
Expression Operators
The built-in assembler provides a variety of operators. Precedence rules are different from that of the
Delphi language; for example, in an asm statement, AND has lower precedence than the addition and
subtraction operators. The following table lists the built-in assembler's expression operators in
decreasing order of precedence.
Precedence of built-in assembler expression operators
& highest
(... ), [... ],., HIGH, LOW
+, - unary + and -
:
OFFSET, TYPE, PTR, *, /, MOD, SHL, SHR, +, -binary + and -
NOT, AND, OR, XOR lowest
613
Operator Description
& Identifier override. The identifier immediately following the ampersand is treated as a user-defined
symbol, even if the spelling is the same as a built-in assembler reserved symbol.
(... ) Subexpression. Expressions within parentheses are evaluated completely prior to being treated as a
single expression element. Another expression can precede the expression within the parentheses;
the result in this case is the sum of the values of the two expressions, with the type of the first
expression.
[... ] Memory reference. The expression within brackets is evaluated completely prior to being treated as
a single expression element. Another expression can precede the expression within the brackets; the
result in this case is the sum of the values of the two expressions, with the type of the first expression.
The result is always a memory reference.
. Structure member selector. The result is the sum of the expression before the period and the
expression after the period, with the type of the expression after the period. Symbols belonging to the
scope identified by the expression before the period can be accessed in the expression after the period.
HIGH Returns the high-order 8 bits of the word-sized expression following the operator. The expression must
be an absolute immediate value.
LOW Returns the low-order 8 bits of the word-sized expression following the operator. The expression must
be an absolute immediate value.
+ Unary plus. Returns the expression following the plus with no changes. The expression must be an
absolute immediate value.
- Unary minus. Returns the negated value of the expression following the minus. The expression must
be an absolute immediate value.
+ Addition. The expressions can be immediate values or memory references, but only one of the
expressions can be a relocatable value. If one of the expressions is a relocatable value, the result is
also a relocatable value. If either of the expressions is a memory reference, the result is also a memory
reference.
- Subtraction. The first expression can have any class, but the second expression must be an absolute
immediate value. The result has the same class as the first expression.
: Segment override. Instructs the assembler that the expression after the colon belongs to the segment
given by the segment register name (CS, DS, SS, FS, GS, or ES) before the colon. The result is a
memory reference with the value of the expression after the colon. When a segment override is used
in an instruction operand, the instruction is prefixed with an appropriate segment-override prefix
instruction to ensure that the indicated segment is selected.
OFFSET Returns the offset part (double word) of the expression following the operator. The result is an
immediate value.
TYPE Returns the type (size in bytes) of the expression following the operator. The type of an immediate
value is 0.
PTR Typecast operator. The result is a memory reference with the value of the expression following the
operator and the type of the expression in front of the operator.
* Multiplication. Both expressions must be absolute immediate values, and the result is an absolute
immediate value.
/ Integer division. Both expressions must be absolute immediate values, and the result is an absolute
immediate value.
614
MOD Remainder after integer division. Both expressions must be absolute immediate values, and the
result is an absolute immediate value.
SHL Logical shift left. Both expressions must be absolute immediate values, and the result is an absolute
immediate value.
SHR Logical shift right. Both expressions must be absolute immediate values, and the result is an absolute
immediate value.
NOT Bitwise negation. The expression must be an absolute immediate value, and the result is an absolute
immediate value.
AND Bitwise AND. Both expressions must be absolute immediate values, and the result is an absolute
immediate value.
OR Bitwise OR. Both expressions must be absolute immediate values, and the result is an absolute
immediate value.
XOR Bitwise exclusive OR. Both expressions must be absolute immediate values, and the result is an
absolute immediate value.
615
Assembly Procedures and Functions (Win32 Only)
You can write complete procedures and functions using inline assembly language code, without
including a begin...end statement. This topic covers these issues:
• Compiler Optimizations.
• Function Results.
Compiler Optimizations
An example of the type of function you can write is as follows:
The automatically generated entry and exit code for the routine looks like this:
616
<> 0 or Params <> 0
RET Params ;Always present
If locals include variants, long strings, or interfaces, they are initialized to zero but not finalized.
Function Results
Assembly language functions return their results as follows.
• Ordinal values are returned in AL (8-bit values), AX (16-bit values), or EAX (32-bit values).
• Real values are returned in ST(0) on the coprocessor's register stack. (Currency values are scaled
by 10000.)
• Pointers, including long strings, are returned in EAX.
• Short strings and variants are returned in the temporary location pointed to by @Result.
617
.NET Topics
This section contains information specific to programming in Delphi on the .NET platform.
618
Using .NET Custom Attributes
.NET framework assemblies are self-describing entities. They contain intermediate code that is compiled
to native machine instructions when the assembly is loaded. More than that, assemblies contain a wealth
of information about that code. The compiler emits this descriptive information, or metadata, into the
assembly as it processes the source code. In other programming environments, there is no way to
access metadata once your code is compiled; the information is lost during the compilation process. On
the .NET platform, however, you have the ability to access metadata using runtime reflection services.
The .NET framework gives you the ability to extend the metadata emitted by the compiler with your own
descriptive attributes. These customized attributes are somewhat analogous to language keywords, and
are stored with the other metadata in the assembly.
• Declaring custom attributes
• Using custom attributes
• Custom attributes and interfaces
type
TCustomCodeAttribute = class(TCustomAttribute)
private
Fprop1 : integer;
Fprop2 : integer;
aVal : integer;
procedure Setprop1(p1 : integer);
procedure Setprop2(p2 : integer);
public
constructor Create(const myVal : integer);
property prop1 : integer read Fprop1 write Setprop1;
property prop2 : integer read Fprop2 write Setprop2;
end;
The implementation of the constructor might look like
619
Delphi for .NET supports the creation of custom attribute classes, as shown above, and all of the custom
attributes provided by the .NET framework.
Note that Delphi for .NET supports the use of named properties in the initialization. These can be the
names of properties, or of public fields of the custom attribute class. Named properties are listed after
all of the parameters required by the constructor. For example
The first parameter, 1024, is the value required by the constructor. The second two parameters are the
properties defined in the custom attribute.
When a custom attribute is placed before a list of multiple variable declarations, the attribute applies to
all variables declared in that list. For example
var
[TCustomAttribute(1024, prop1=512, prop2=128)]
x, y, z: Integer;
would result in TCustomAttribute being applied to all three variables, X, Y, and Z.
Custom attributes applied to types can be detected at runtime with the GetCustomAttributes method of
the Type class. The following Delphi code demonstrates how to query for custom attributes at runtime.
var
F: TMyClass; // TMyClass declared above
T: System.Type;
A: array of TObject; // Will hold custom attributes
620
I: Integer;
begin
F := TMyClass.Create;
T := F.GetType;
A := T.GetCustomAttributes(True);
Program HellowWorld2;
[DllImport('user32.dll')]
function MessageBeep(uType : LongWord) : Boolean; external;
begin
MessageBeep(LongWord(-1));
end.
Note the external keyword is still required, to replace the block in the function declaration. All other
attributes, such as the calling convention, can be passed through the DllImport custom attribute.
621
When the compiler sees an interface declaration, the next square bracket construct found is assumed
to be that of a GUID specifier for the interface. The GUID must be in the traditional Delphi form:
['{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}']
Alternatively, you can use the Guid custom attribute of the .NET framework ( GuidAttribute). If you
choose this method, then you should introduce the attribute before the interface, as with any other
custom attribute.
The effect in either case is the same: the GUID is emitted into the metadata for the interface type. Note
that GUIDs are not required for interfaces in the .NET Framework. They are only used for COM
interoperability.
Note: When importing COM interfaces with the ComImport custom attribute, you must
declare the GuidAttribute instead of using the Delphi syntax.
622
IDE command-line options
Describes available options when starting the IDE from the command line.
You use these options with the appropriate IDE startup command: bds.exe
For example:
Starts the IDE with no splash screen and tracks memory allocation.
Starts the IDE and loads c:\test\myprog.exe into the debugger and used c:\test\source as the location
for the source code while debugging. The -td and any other argument that appears after the
-dexename debugger option is used as an argument to c:\test\myprog.exe.
General options
OptionDescription
? Launches the IDE and displays online help for IDE command-line options.
-- Ignore rest of command-line.
hm Heap Monitor. Displays information in the IDE title bar regarding the amount of memory allocated using the
memory manager. Displays the number of blocks and bytes allocated. Information gets updated when the
IDE is idle.
hv Heap Verify. Performs validation of memory allocated using the memory manager. Displays error information
in the IDE title bar if errors are found in the heap.
ns No splash screen. Suppresses display of the splash screen during IDE startup.
np No Project. Supresses loading of any desktop files on IDE startup and suppresses creation of a default
project.
623
Debugger options
Option Description
attach:%1;%2Performs a debug attach, using %1 as the process ID to attach to and %2 as the event ID for that
process. It can be used manually, but is used mostly for Just in Time debugging.
dexename Loads the specified executable (exename) into the debugger. Any parameters specified after the
exename are used as parameters to the program being debugged and are ignored by the IDE. A
space is allowed between the d and the exename.
rregkey Allows you specify an alternate base registry key so you can run two copies of the IDE using different
configurations. This allows component developers to debug a component at design-time by using the
IDE as the hosting application without the debugging IDE interfering by trying to load the component
package being developed
hhostname Hostname. Must be used with the d option. When specified, a remote debug session is initiated using
the specified host name as the remote host to debug on. The remote debug server program must be
running on the remote host.
l (Lowercase L) Assembler startup. Do not execute startup code. Must be used with the d option.
Normally, when you specify the d option, the debugger attempts to run the process to either main or
WinMain. When l is specified, the process is merely loaded and no startup code is executed.
sddirectoriesSource Directories. Must be used with the d option. The argument is either a single directory or a
semicolon delimited list of directories which are used as the Debug Source Path setting (can also be
set using the Project|Options|Directories/Conditionals option page). No space is allowed between sd
and the directory list argument.
td TDGoodies. Implements several features found in the Turbo Debugger, TD32 (available on Windows
only). It must be used with the d option. It causes the CPU and FPU views to stay open when a process
terminates. It causes Run|Program Reset to terminate the current process and reload it in the
debugger. If there is no current process, Run|Program Reset reloads the last process that terminated.
It also causes breakpoints and watches to be saved in the default desktop if desktop saving is on and
no project is loaded.
Project options
Option Description
filename (No preceding dash) The specified filename is loaded in the IDE. It can be a project, project group, or a
single file.
b AutoBuild. Must be used with the filename option. When specified, the project or project group is built
automatically when the IDE starts. Any hints, errors, or warnings are then saved to a file. Then the IDE
exits. This facilitates doing builds in batch mode from a batch file. The Error Level is set to 0 for successful
624
builds and 1 for failed builds. By default, the output file has the same name as the filename specified with
the file extension changed to .err. This can be overridden using the o option.
m AutoMake. Same as AutoBuild, but a make is performed rather than a full build.
ooutputfileOutput file. Must be used the b or m option. When specified, any hints, warnings, or errors are written to
the file specified instead of the default file.
625