Table Of ContentHoward Page-Clark
Learn to Program
using Lazarus
BBBLLLAAAIIISSSEEE PPPAAASSSCCCAAALLL MMMAAAGGGAAAZZZIIINNNEEE
PUBLISHER
I
Pascal
Learn to program
using Lazarus
by Howard Page Clark
II
Learn to program using Lazarus
Developmental Editor: Detlef Overbeek
Production Editor: Detlef Overbeek
Proofreaders: Peter Bijlsma,
Correctors: Detlef Overbeek
Cover Designer: Detlef Overbeek
Copyright © 2013 by Blaise Pascal Magazine
Copyright © 2012.
All rights reserved by Blaise Pascal Magazine
Email: Office@blaisepascal.eu http://www.blaisepascalmagazine.eu
Blaise Pascal Magazine grants readers limited permission to reuse the code found in this publication so
long as the author(s) are attributed in any application containing the reusable code and the code itself is
never distributed, posted online by electronic transmission, sold, or commercially exploited as a stand-
alone product.
Aside from this specific exception concerning reusable code, no part of this publication may be stored in a
retrieval system, transmitted, or reproduced in any way, including but not limited to photocopy,
photograph, magnetic, or other record, without the prior agreement and written permission of the
publisher.
This edition is registered by the Dutch Royal Library
Nederlandse Koninklijke Bibliotheek
ISBN: 978-94-90968-04-5
Blaise Pascal Magazine and the Blaise Pascal Magazine logo are either registered trademarks or trademarks
of the Pro Pascal Foundation in the Netherlands and/or other countries.
TRADEMARKS: Blaise Pascal Magazine has attempted throughout this book to distinguish proprietary
trademarks from descriptive terms by following the capitalization style used by the manufacturer.
The authors and publisher have used their best efforts in producing this book, whose content is based on
the latest software releases wherever possible. Portions of the manuscript may be based upon pre-release
versions supplied by software manufacturer(s).
The authors and the publisher make no representation or warranties of any kind with regard to the
completeness or accuracy of the contents herein and accept no liability of any kind including but not
limited to performance, merchantability, fitness for any particular purpose, or any losses or damages of any
kind caused or alleged to be caused directly or indirectly from this book.
III
Foreword Learn to program using Lazarus
Pascal is enjoying something of a revival, and this is due in no small part to the quality and
success of the Free Pascal Compiler, and the Lazarus project, which depends on it.
That success, in turn, owes much to the skill and dedication of the core team members of the
two projects.
This book is a tribute to the guys (so far there are no significant gals) starting with Florian
Klaempfl who have carried the development of these projects to the significant phase each has
reached.
One measure of the lasting value of open source projects is the quality of the programming
communities that are drawn together through participation in such projects, and both Free
Pascal and Lazarus shine in this respect.
I share the passion of a few in the programming community for good documentation, and
particularly for material which makes programming more accessible to newcomers or those
who (like me) don't have a formal education in information technology or computer science.
The Pascal language came to birth in the imagination of a Swiss software architect, a rare
academic who values clarity and simplicity enough to make both aspects a hallmark of his
designs. Pascal later became a widely available compiler (for CP/M, DOS and then Windows)
largely through a Danish software architect. Borland incorporated that Danish compiler into an
IDE, where Anders Hejlsberg and Chuck Jazdzewski among others oversaw adaptation of the
language and support libraries for GUI programming on Windows.
Part of Free Pascal's achievement has been to liberate Pascal from its attachment principally to
the Windows/.NET platform (which characterised the major years of US-based Object Pascal
Delphi development, since the Kylix fork was not maintained). It was FPC (for a time) that
enabled the Delphi IDE to generate code to run on iOS. A maturing Lazarus has also helped
slowly to convince developers for MacOS, Linux and more recently Android and other
platforms that Pascal can truly serve their needs.
I am grateful to Mattias Gaertner, who encouraged me to write this book during our first
conversation, to Michaël van Canneyt, who saw an early draft of the manuscript and whose
incisive comments helped enormously in shaping the organisation of the material, and to Detlef
Overbeek, whose generous hospitality enabled me to see the obvious enthusiasm for Pascal
development among programmers gathered in Utrecht. He is a friend who is ready to take risks
in publishing.
Mistakes you find in the following pages are, of course, mine alone. I would be grateful for
notification of any needed corrections, so that they can be included in any future printing.
Please direct any comments to me via the publisher's website: www.blaisepascal.eu
Howard Page-Clark
Christmas 2012
IV
Learn to program using Lazarus
V
Contents Learn to program using Lazarus
CHAPTER 1 STARTING TO PROGRAM
1.a What to expect in this book 1
1.b What is programming? 1
1.c The worlds inside and outside the CPU 2
1.d Not only digital data, but also digital code 3
1.e Different computer languages at different levels 3
1.f Pascal: a universal computer language 4
1.g Lazarus: an IDE for Pascal 5
1.h An open source approach to software 6
1.i Getting help 7
1.j Review Questions 8
CHAPTER 2 LAZARUS AND PASCAL
2.a The layout of the Lazarus IDE 9
2.b Two different sorts of program 10
2.c Writing, compiling and running firstproject 12
2.d The structure of a Pascal program 14
2.e Comments in Pascal code 16
2.f Use of names in Pascal 16
2.g Compiler directives 17
2.h Review Questions 18
CHAPTER 3 TYPES, VARIABLES, CONSTANTS AND ASSIGNMENTS
3.a Pascal types 19
3.b Ordinal types 20
3.c The boolean type 21
3.d Enumerated types 22
3.e Type conversion 24
3.f Typecasts 25
3.g Variables 26
3.h Initialised variables 26
3.i Assignment: placing a value in a variable 27
3.j Extended numerical assignment operators 28
3.k Constants and literal values 28
3.l A program example: simple_types 29
3.m Typed constants 31
3.n Pointers 33
3.o Review Questions 35
CHAPTER 4 STRUCTURED TYPES
4.a Static arrays 36
4.b Unnamed (anonymous) types 37
4.c Pascal shortstrings 38
4.d Dynamic arrays 39
4.e Ansistrings 39
4.f Records 41
4.g The with . . . do statement 42
4.h Set types 44
4.i Binary files 45
4.j Text files 47
4.k Review Questions 48
VI
Contents Learn to program using Lazarus
CHAPTER 5 EXPRESSIONS AND OPERATORS
5.a Operators: forming Pascal expressions 49
5.b Mathematical operators 49
5.c Boolean operators: not, and, or, xor 50
5.d Comparison (relational) operators 50
5.e Bitwise (logical) operators 51
5.f A program example: simple_expressions 51
5.g Review Questions 52
CHAPTER 6 PASCAL STATEMENTS
6.a Conditional statement: if 54
6.b Conditional statement: case of end 55
6.c Looping statement: for to do 56
6.d Looping statement: for downto do ; Break and Continue 57
6.e Looping statement: for in do 57
6.f Looping statement: while do 58
6.g Looping statement: repeat until 58
6.h Exception statements: raise, on, try 58
6.i Review Exercises 61
CHAPTER 7 ROUTINES: FUNCTIONS AND PROCEDURES
7.a Routines and methods 62
7.b Calling a routine 62
7.c Passing data to a routine: parameters 63
7.d Picking up the value returned from a function 63
7.e Parameter classification: var, const, out 64
7.f Default parameters 65
7.g Declaring procedures and functions 65
7.h A program example: function_procedure 66
7.i The Exit() procedure 66
7.j Review Questions 68
CHAPTER 8 CLASS: AN ELABORATE TYPE
8.a Generations of classes 69
8.b Class data fields 70
8.c Class memory management 71
8.d Exercising simple class methods 73
8.e Properties: special access to class data and events 75
8.f Private, protected, public and published 80
8.g Events 81
8.h Event properties 83
8.i Object oriented design 85
8.j Review Exercises 85
CHAPTER 9 POLYMORPHISM
9.a Cross-platform polymorphism 86
9.b Polymorphic methods in classes 87
9.c Polymorphic graphic classes 89
9.d Overloading 94
9.e Default parameters 95
9.f Review Questions 95
VII
Contents Learn to program using Lazarus
CHAPTER 10 UNITS, GUI PROGRAMS AND THE IDE
10.a Unit structure and scope 96
10.b The GUI program skeleton 99
10.c Packages 101
10.d Changing the program icon 101
10.e The main form file 102
10.f Editor Auto-completion 103
10.g Using the Designer 106
10.h The Object Inspector 107
10.i OI Favorites and shortcuts 109
10.j The OI Restricted page 110
10.k The Component Palette 111
10.l Finding a Palette component 111
10.m Regular, DB and RTTI component types 112
10.n Non-visual LCL and FCL support classes 113
10.o Review Exercises 113
CHAPTER 11 DISPLAY CONTROLS
11.a Display controls: TLabel 114
11.b Display controls: exploring TLabel properties 115
11.c Display controls: TStaticText 119
11.d Display controls: TBevel and TDividerBevel 120
11.e Display controls: TListBox 121
11.f Display controls: TStatusBar 122
11.g Display controls: further options 123
11.h Review Questions 123
CHAPTER 12 GUI EDIT CONTROLS
12.a Editing short phrases: TEdit and TLabeledEdit 124
12.b Editing or choosing short phrases: TComboBox 126
12.c Editing integers and floating point numbers 126
12.d Multiple-line editors 129
12.e A component browser 130
12.f Getting RTTI information for a component 135
12.g Adding external tools to the IDE 138
12.h Review Questions 140
CHAPTER 13 LAZARUS GUI PROJECTS
13.a Planning a project 141
13.b Creating a project task list 141
13.c ToDo functionality 142
13.d Version control 142
13.e Test-driven software development 142
13.f Naming 143
13.g Project directory structure 143
13.h A template project: SetDemo 144
13.i Encapsulating set interaction within a new class 146
13.j The setdemo UI 149
13.k Review Questions 156
VIII
Contents Learn to program using Lazarus
CHAPTER 14 COMPONENT CONTAINERS
14.a Non-visual RTL classes 157
14.b Creating new forms 158
14.c Ownership and Parentage 160
14.d Programmatic form creation 160
14.e TGroupBox, TPanel 163
14.f Resizeable children 164
14.g TFrame 166
14.h TDatamodule 167
14.i Review Exercises 167
CHAPTER 15 NON-VISUAL GUI SUPPORT CLASSES
15.a TPersistent descendants 168
15.b A chemical TCollection 169
15.c The TStringList class 172
15.d Sorting lines in a text file 173
15.e Streams 176
15.f TFileStream 177
15.g TMemoryStream, TStringStream and Blowfish 180
15.h Visualising a stream 182
15.i Review Exercises 187
CHAPTER 16 FILES AND ERRORS
16.a File access in Pascal 188
16.b Run-time errors and exceptions 188
16.c An example of string error-handling 189
16.d File name encoding issues 192
16.e User-directed file searching and naming – the Dialogs Palette page 193
16.f Discriminating between text and binary files 196
CHAPTER 17 WORKING WITHIN KNOWN LIMITS
17.a Using recursion to evaluate factorials 200
17.b Catching a specific exception 203
17.c Permutations 204
17.d Time-consuming routines 205
17.e Generating anagrams 206
17.f Review Questions 210
CHAPTER 18 ALGORITHMS AND UNIT TESTS
18.a Collaboration 211
18.b The algorithm – a specific plan 211
18.c A parsing algorithm 212
18.d Testing the ParseToWords function: the FPCUnit Test 215
18.e Example tests 218
18.f Test-driven development 220
18.g Optimising debugged routines 220
18.h Profiling and compiler optimisation 220
18.i Review Questions 221
IX