Congratulations--you've chosen one of today's hottest programming tools! Before you get started using all that Delphi has to offer, though, you first need to learn a little about the Delphi IDE and about Object Pascal. In this chapter you will find
By now you know that Delphi is Borland's best-selling rapid application development (RAD) product for writing Windows applications. With Delphi, you can write Windows programs more quickly and more easily than was ever possible before. You can create Win32 console applications or Win32 graphical user interface (GUI) programs. When creating Win32 GUI applications with Delphi, you have all the power of a true compiled programming language (Object Pascal) wrapped up in a RAD environment. What this means is that you can create the user interface to a program (the user interface means the menus, dialog boxes, main window, and so on) using drag-and-drop techniques for true rapid application development. You can also drop ActiveX controls on forms to create specialized programs such as Web browsers in a matter of minutes. Delphi gives you all this, and at virtually no cost: You don't sacrifice program execution speed because Delphi generates fast compiled code.
I can hear you saying, "This is going to be so cool!" And guess what? You're right! But before you get too excited, I need to point out that you still have to go to work and learn about Pascal programming. I don't want you to think that you can buy a program like Delphi and be a master Windows programmer overnight. It takes a great deal of work to be a good Windows programmer. Delphi does a good job of hiding some of the low-level details that make up the guts of a Windows program, but it cannot write programs for you. In the end, you must still be a programmer, and that means you have to learn programming. That can be a long, uphill journey some days. The good news is that Delphi can make your trek fairly painless and even fun. Yes, you can work and have fun doing it!
So roll up your sleeves and put on your hiking shoes. Delphi is a great product, so have fun.
This section contains a quick look at the Delphi integrated development environment (IDE). I'll give the IDE a once-over now and examine it in more detail on Day 4, "The Delphi IDE Explored." Because you are tackling Windows programming, I'll assume you are advanced enough to have figured out how to start Delphi. When you first start the program, you are presented with both a blank form and the IDE, as shown in Figure 1.1.
FIGURE 1.1. The Delphi IDE and the initial blank form.
The Delphi IDE is divided into three parts. The top window can be considered the main window. It contains the toolbars and the Component palette. The Delphi toolbars give you one-click access to tasks such as opening, saving, and compiling projects. The Component palette contains a wide array of components that you can drop onto your forms. (Components are text labels, edit controls, list boxes, buttons, and the like.) For convenience, the components are divided into groups. Did you notice the tabs along the top of the Component palette? Go ahead and click on the tabs to explore the different components available to you. To place a component on your form, you simply click the component's button in the Component palette and then click on your form where you want the component to appear. Don't worry about the fact that you don't yet know how to use components. You'll get to that in due time. When you are done exploring, click on the tab labeled Standard, because you'll need it in a moment.
New Term: A component is a self-contained binary piece of software that performs some specific predefined function, such as a text label, an edit control, or a list box.
Below the main window and on the left side of the screen is the Object Inspector. It is through the Object Inspector that you modify a component's properties and events. You will use the Object Inspector constantly as you work with Delphi. The Object Inspector has two tabs: the Properties tab and the Events tab. A component's properties control how the component operates. For example, changing the Color property of a component changes the background color of that component. The list of properties available varies from component to component, although components usually have several common elements (Width and Height properties, for instance).
New Term: A property determines the operation of a component.
The Events tab contains a list of events for a component. Events occur as the user interacts with a component. For example, when a component is clicked, an event is generated that tells you that the component was clicked. You can write code that responds to these events, performing specific actions when an event occurs. As with properties, the events that you can respond to vary from component to component.
New Term: An event is something that occurs as a result of a component's interaction with the user or with Windows.
New Term: An event handler is a section of code that is invoked in your application in response to an event.
The main part of the Delphi IDE is the workspace. The workspace initially displays the Form Designer. It should come as no surprise that the Form Designer enables you to create forms. In Delphi, a form represents a window in your program. The form might be the program's main window, a dialog box, or any other type of window. You use the Form Designer to place, move, and size components as part of the form creation process.
Hiding behind the Form Designer is the Code Editor. The Code Editor is where you type code when writing your programs. The Object Inspector, Form Designer, Code Editor, and Component palette work interactively as you build applications.
Now that you've had a look at what makes up the Delphi IDE, let's actually do something.
It's tradition. Almost all programming books start you off by having you create a program that displays Hello World on the screen. I'm tempted to do something else, but tradition is not a force to be reckoned with, so Hello World it is. You've got some work ahead of you in the next few chapters, so I thought I'd give you a taste of Delphi's goodies before putting you to work learning the seemingly less glamorous basics of the Pascal language. You'll have a little fun first. Delphi (and its cousin, C++Builder) gives you possibly the quickest route to Hello World of any Windows programming environment to date.
Right now you should have Delphi running, and you should be looking at a blank form. By default, the form is named Form1. (The form name is significant in Delphi, but I'll address that a little later.) To the left of the form, the Object Inspector shows the properties for the form. Click on the title bar of the Object Inspector. The Caption property is highlighted, and the cursor is sitting there waiting for you to do something. (If the Caption property is not in view, you might have to scroll the Object Inspector window to locate it. Properties are listed in alphabetical order.) Type Hello World! to change the form's caption.
NOTE: As you modify properties, Delphi immediately displays the results of the property change when appropriate. As you type the new caption, notice that the window caption of the form is changing to reflect the text you are typing.
Now click the Run button on the toolbar (the one with the green arrow). (You can also press F9 or choose Run | Run from the main menu.) Before you even know what has happened, Delphi has built the program. The form is displayed, and the caption shows Hello World!. In this case, the running program looks almost identical to the blank form. You might scarcely have noticed when the program was displayed because it is displayed in the exact location of the form in the Form Designer. (There is a difference in appearance, though, because the Form Designer displays an alignment grid and the running program does not.) Congratulations--you've just written your first Windows program with Delphi. Wow, that was easy!
"But what is it?" you ask. It's not a lot, I agree, but it is a true Windows program. Try it out and see. The program's main window can be moved by dragging the title bar, it can be sized, it can be minimized, it can be maximized, and it can be closed by clicking the Close button. You can even locate the program in Windows Explorer (it will probably be in your \Delphi40\Bin directory as Project1.exe) and double-click on it to run it.
Okay, so maybe displaying Hello World! in the caption was cheating a little. Let's spruce it up a bit. If you still have the Hello World program running, close it by clicking the Close button in the upper-right corner of the window. The Form Designer is displayed again, and you are ready to modify the form (and, as a result, the program).
To make the program more viable, you're going to add text to the center of the window itself. To do this, you'll add a text label to the form:
Because the label is probably not centered on the form, you might want to move it. To move a component, simply click on it and drag it to the position you want it to occupy. When you have the label where you want it, you're ready to recompile and run the program. Click the Run button again and, after a split second, the program runs. Now you see Hello World! displayed in the center of the form as well as in the caption. Figure 1.2 shows the Hello World! program running.
FIGURE 1.2. Your Hello World! program running.
With this little taste of Delphi, you can see that writing Windows programs with Delphi is going to be a great deal more interesting than it was in the good ol' days. To prepare for what you are going to do next, you need to close the current project in the Delphi IDE. Choose File | Close All from the main menu. Click on No when prompted to save changes to Project1, or save the project if you are fond of your new creation.
Before you can move on to learning the Pascal language you need a little more information about how Delphi works. You'll need this information to test the various Pascal language features as you work through the next couple of days. This section will contain just a glimpse into the power of Delphi. On Days 4, 5, and 6, you get a more detailed look into how Delphi works.
The goal of this exercise is to have the words Hello World, Part II appear on the screen when a button is pressed. This exercise will also give you a pattern you can follow when you test various code snippets as you work through the next couple of days. Perform the following steps:
At this point your form should look similar to Figure 1.3. Notice that the label component has a default caption of Label1 and the button has a default caption of Button1.
In the first version of Hello World, you used the Object Inspector to change the Caption property of a label. That change was applied at design time and as such was seen as soon as the program ran. In this exercise, you are going to change the caption of the label through code.
FIGURE 1.3. The new form after placing the button and label components.
NOTE: When you change a component's properties through the Object Inspector and Form Designer, you are said to make a design-time change. When you modify a property through code that executes when the program runs, you are said to make a runtime change.
To change the Caption property at runtime, follow these steps:
procedure TForm1.Button1Click(Sender: TObject); begin end;
Label1.Caption := `Hello World, Part II';
procedure TForm1.Button1Click(Sender: TObject); begin Label1.Caption := `Hello World, Part II'; end;
You'll be doing many such exercises in the next few days so you'll get plenty of practice placing labels, buttons, and other components on the form. I realize that I didn't fully explain what is going on behind the scenes here, but I don't want to get ahead of myself so I'll save that explanation for a later time.
Before you can learn about the RAD features of Delphi, you need to learn the basics of the Object Pascal language. This part of the book will probably not be the most exciting for you, but you need a basic understanding of Object Pascal before you move on.
It would be nice if presenting the Object Pascal language could be handled sequentially. That's not the case, though, because all the features you will learn about are intertwined. I'll take the individual puzzle pieces one at a time and start fitting them together.
By the end of Day 3, you'll have a fairly complete picture of the Object Pascal language. Don't be concerned if you don't instantly grasp every concept that is presented. Some of what is required to fully understand Object Pascal can only come with real-world experience.
During the next few days, you will see short code snippets that illustrate a particular feature of the Object Pascal language. You will also do some exercises that enable you to test your newfound knowledge. In the first few days, you will only see your Delphi applications in small sections. I don't want to get ahead of myself and go too far into the Delphi IDE or the Visual Component Library (VCL) at this early stage. You will have to settle for bits and pieces until later in the book when you start to get the complete picture. The code that you can download from the book's site contains complete programs for some of the exercises that you will perform over the next several days. (Go to http://www.mcp.com/info and type 0-672-31286-7.)
Back in 1994 or so, Borland began working on a RAD tool that it code-named Delphi. When it was decided that the component model architecture was the best way to implement RAD, it was then necessary to settle on the programming language that would be the heart of the system.
At that time, Borland was the only compiler vendor mass marketing a Pascal compiler. Borland was known as the company that produced the best Pascal tools. If you were a Pascal programmer, you probably used Borland's TurboPascal in one flavor or another. Borland more or less "owned" Pascal. Although Borland didn't own the Pascal language in a legal sense, it no doubt felt that because of its position in the Pascal world, it could take considerable liberties in implementing new language features and enhancements. In addition, there was no Pascal standards committee, nor even a written standard defining the Pascal language. So Borland created Delphi using Pascal as the base language (the Borland internal code name stuck and became the official product name).
Before Delphi came into being, Borland had already modified the Pascal language in positive ways. For example, Borland had already extended Pascal by creating a new language called Object Pascal. It can be said that Object Pascal is to Pascal what C++ is to C. Object Pascal added classes to Pascal, thereby hurling Pascal into the world of object-oriented programming (OOP) languages. As Delphi was being developed, new language behavior and keywords were added to deal with the component model. Keywords such as published and property were added, as were others. This enabled Borland to fully implement the power of the component model. By modifying the Pascal language to suit the component model, Borland was able to implement RAD the right way. In essence, the Object Pascal language was modified as needed when design issues came up during the development of the then-unknown product called Delphi. The result is a language that works seamlessly with the component model.
Although modifying the Pascal language could be considered a bold step for Borland, it was not without precedent. Previously, Microsoft had taken the BASIC language and modified it to produce a new language called Visual Basic. This new language was nearly unrecognizable when compared to the original BASIC language that served as its base.
Borland took a risk in modifying Pascal. After all, it had a loyal base of customers that might not take kindly to enhancements to the language they had come to know and love. Still, Borland was in a solid position in the Pascal market and went ahead with its plans. The result was a smash hit, of course.
Make no mistake about it, Object Pascal is a powerful programming language, and I don't make that statement lightly. I have a C/C++ background and, like other C/C++ programmers, I viewed Delphi with a bit of skepticism at first. I found out quickly, though, that the Object Pascal language is very capable. In fact, in the hands of the average programmer there is almost no difference in the two languages in terms of power. Object Pascal is unique in that it is both powerful and relatively easy to learn. I don't in any way want to leave the impression that Object Pascal is a not a full-featured programming language. Pascal has often been knocked as a less-than-serious programming language. That has never been true, and is even less true with today's Object Pascal.
NOTE: Several different terms have been adopted by Delphi programmers to describe what they do. The base language of Delphi is, of course, Object Pascal, and some folks call it exactly that. Others might say, "I program in Pascal," or even just, "I'm a Delphi programmer." In the end it's up to you to decide what terminology you will use. I'll use the terms Object Pascal and Pascal interchangeably throughout this book and will typically reserve use of the word Delphi to refer to the Delphi IDE or its tools.
Object Pascal enables you to take advantage of object-oriented programming to its fullest. OOP is not just a buzzword. It has real benefits because it enables you to create objects that can be used in your current program and reused in future programs.
New Term: An object, like components described earlier, is a binary piece of software that performs a specific programming task. (Components are objects, but not all objects are components. I'll explain that later.)
An object reveals to the user (the programmer using the object) only as much of itself as needed; therefore, using the object is simplified. All internal mechanisms that the user doesn't need to know about are hidden from sight. All this is included in the concept of object-oriented programming. OOP enables you to take a modular approach to programming, thus keeping you from constantly re-inventing the wheel. Delphi programs are very OOP-centric because of Delphi's heavy use of components. After a component is created (either one of your own or one of the built-in components), it can be reused in any Delphi program. A component can also be extended by inheritance to create a new component with additional features. Best of all, components hide their internal details and let the programmer concentrate on getting the most out of the component. Objects and classes are discussed in detail on Day 3, "Classes and Object-Oriented Programming."
Programming is more than just typing code. Ultimately, it is the combination of conceptualizing a programming task and then typing code to carry out that task. The code you type simply goes into a text file. The compiler takes that text file and compiles it into machine code that the computer can understand. The text file that Delphi compiles into machine code is called a unit.
New Term: A unit is a text file that can be compiled into a module of code.
A Delphi GUI application will contain at least two units. The project source unit contains the project source code. Project source code units have an extension of DPR. You can view the project source unit by choosing Project | View Source from the main menu. It is not normally necessary to modify the project source unit. In fact, you shouldn't modify the project source unit unless you know exactly what you are doing. If you accidentally modify the project source unit in undesirable ways, you might find that your application won't compile anymore. (Certain advanced programming techniques require modification of the project source code, but that's not something you need to be concerned with at this time.)
The second type of unit that a Delphi GUI application always has is the main form's unit. A form unit, as its name implies, is a source code unit with an associated form. This type of unit has a filename extension of PAS. This is the type of unit you will use most often in your Delphi programs. A Delphi GUI application will always have one form unit (for the main form), but it can have one or more additional form units as well. For example, an application that displays an About box will have the main form unit and a unit for the About box.
NOTE: You might have noticed that I keep saying "Delphi GUI application." This is because I want to distinguish a GUI application from a console mode application. A console mode application is a 32-bit Windows application that runs in a console window (DOS box). A console application has no main form and may or may not contain other forms. A console application does, however, have one or more units.
There is a third type of unit you can use in Delphi applications. This type of unit is a unit that contains only source code. A code-only unit contains code that is called from other units in the project. I won't go into any more detail than that right now, but you'll learn more about this type of unit in later chapters.
Delphi units must follow a predefined format. This shouldn't come as a surprise to you. The unit has to be in a predefined format so that the compiler can read the unit and compile the unit's code.
A Delphi project unit contains the program keyword followed by the name of the unit and a code block marked by the begin and end keywords. You can see how a basic unit looks by choosing View | Project Source from the Delphi main menu. The project source unit for a default Delphi project looks like Listing 1.1.
NOTE: The line numbers in Listing 1.1 are not part of the unit itself. I have put them there for reference only. Some of the listings you see in this book will have line numbers for reference and others will not. In either case, be sure to understand that the Pascal language does not use line numbers as some other languages do (most notably, BASIC).
01: program Project1; 02: 03: uses 04: Forms, 05: Unit1 in `Unit1.pas' {Form1}; 06: 07: {$R *.RES} 08: 09: begin 10: Application.Initialize; 11: Application.CreateForm(TForm1, Form1); 12: Application.Run; 13: end.
On line 1, the program keyword identifies this unit as a program's main source unit. You can see that the unit name, Project1, follows the program keyword (Delphi gives the project a default name until you save the project with a more meaningful name). Beginning on line 3, you see a section identified by the uses keyword. Any unit names following the uses keyword, up to the semicolon, are other units that this unit requires in order to compile. The uses keyword is described in more detail a little later in the section, "The uses List."
On line 7 you see a compiler directive that tells Delphi to include this project's resource file. Resource files are discussed in more detail on Day 8, "Creating Applications in Delphi."
Line 9 contains the begin keyword, and line 13 contains the end keyword. Notice that the final end keyword in the unit is followed by a period. (A unit can have many code blocks marked with begin and end, but only one final end statement.) The code on lines 10, 11, and 12 is code that initializes the application, creates the application's main form, and starts the application running. You don't need to be concerned about the details of this code to write Delphi programs.
NOTE: The begin and end keywords mark a code block. A code block can contain just a few lines of code, or it can contain several hundred lines of code (or even thousands of lines). You will see the begin and end keywords used throughout the book. As you work through the book, you will get a better handle on how and when the begin and end keywords are used.
Let's take a look at another basic Pascal unit. Choose File | New from the main menu. When the New Items dialog comes up, locate the icon labeled Unit and double-click it. Delphi will create a new unit and display it in the Code Editor. Listing 1.2 shows the code generated for this unit.
01: unit Unit2; 02: 03: interface 04: 05: implementation 06: 07: end.
There isn't much here, is there? This unit has two things in common with the unit shown in Listing 1.1. First, the unit starts with the unit keyword followed by the unit name Unit2 (again, a default name created by Delphi). I realize the code in Listing 1.1 starts with the program keyword and this code starts with the unit keyword, but there are a few common elements: A Pascal unit starts with one of these two keywords followed by the unit name, and the end keyword appears at the end of both listings. Here again, the end keyword is followed by a period to mark the end of the unit.
The code in Listing 1.2 differs from that of Listing 1.1 in that it has sections marked interface and implementation. A unit that is not the program's main source unit must contain an interface section and an implementation section. These two keywords will be described in more detail in the sections entitled, "The interface Section" and "The implementation Section," respectively. Listing 1.2 also differs from Listing 1.1 in that there is no begin statement. A program's main unit must have both begin and end statements, but a source unit only has to contain a final end statement.
The following sections describe keywords that are used within a Pascal unit.
New Term: The uses list is a list of external units that this unit references.
Refer to Listing 1.1. Notice the uses keyword on line 3. The uses keyword designates the start of a section that will contain a list of other units that this unit is dependent on. For example, line 11 of Listing 1.1 looks like this:
Application.CreateForm(TForm1, Form1);
This line of code contains information that is located in other units and cannot be found in this unit. The procedure identified by Application.CreateForm is located in a Delphi unit called Forms.pas, and the identifiers TForm1 and Form1 are located in the project's main form unit, which is called Unit1.pas. Do you see the connection? The uses list tells Delphi where to look for additional information that it will need to compile this unit. Here's another look at the uses list:
uses Forms, Unit1 in `Unit1.pas' {Form1};
Notice that the uses list contains two unit names, Forms and Unit1. In some ways this is not a good example of a uses list because the second unit listed contains additional text not usually found in a uses list (Unit1 in `Unit1.pas' {Form1}).
This text is used to specify a form that is contained in a unit and is only used by the project's main source unit. (The text between the curly braces is a comment used for reference and has no bearing on the rest of the code. Comments are discussed later in the section "Comments in Code.")
There are two rules you need to be aware of when constructing the uses list:
Naturally the list must contain valid unit names. The uses list, then, is designated by the uses keyword and ends with a semicolon. Other than that, it doesn't matter how the uses list is organized. For example, the following two uses lists are identical as far as the compiler is concerned:
uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
A unit can have any number of uses lists. It is not required that all units needed by this unit be in a single uses list.
NOTE: In some cases, Delphi will add units to your uses list for you. This is done via the File | Use Unit menu item. This feature will be discussed in more detail on Day 4.
Take another look at Listing 1.2. Notice that this listing has a section marked by the interface keyword. This keyword marks the start of the interface section for the unit.
The interface section is the section of a unit in which identifiers exported from this unit are declared. An exported identifier is one that can be accessed by other units in the project.
Most units will contain code that other units use. The code might be implemented as a class, a procedure, a function, or a data variable. Any objects that are available to other units from this unit must be declared in the interface section. You could say that the interface section contains a list of items in this unit that other units can use. The interface section starts with the interface keyword and ends at the implementation keyword.
New Term: The implementation section of a unit is the section that contains the actual code for the unit.
The implementation section starts with the implementation keyword and ends with the next unit keyword. The next unit keyword is usually the unit's final end keyword, but could be the initialization keyword in units that have an initialization section. It's difficult to say more than that right now, because there are other aspects of Pascal that I need to discuss before tying all of this together. However, let me give you an example that will illustrate the use of the interface and implementation sections.
Let's say that you create a unit that has a procedure called DoSomething. Let's further say you want DoSomething to be available to other units in your project. In that case, you would declare the DoSomething procedure in the interface section and then define the procedure in the implementation section. The entire unit would look like Listing 1.3.
unit Unit2; interface procedure DoSomething; implementation procedure DoSomething; begin
{ Code for DoSomething goes here. }
end; end.
Notice that the DoSomething procedure is declared in the interface section and defined later in the implementation section. I realize I'm getting a little ahead of myself here. Functions and procedures will be discussed more tomorrow, and I'll go over declarations and definitions in detail at that time.
The initialization and finalization sections can be used to perform any startup and cleanup code that a unit requires. Any code in the initialization section will be executed when the unit is loaded into memory. Conversely, any code in the finalization section will be executed just before the unit is unloaded from memory. You can have just an initialization section, but you cannot have a finalization section without an initialization section. The initialization and finalization sections are optional.
A Pascal unit can contain other, optional keywords that mark sections set aside for a particular purpose. Some of these keywords have multiple uses. The following sections describe those keywords only as they pertain to units.
The const Keyword
A unit can optionally have one or more const sections. The const section is designated with the const keyword. The const section describes a list of variables that are known as constants.
A constant is an identifier that cannot change. For example, let's say you have certain values that your program uses over and over. You can set up constant variables for those values. To illustrate, let's add a const section to the program in Listing 1.3. You'll add one const section for constants that are public (available to other units) and another const section for constants that are available only to this unit. Listing 1.4 shows the unit with the two const sections added.
unit Unit2; interface const AppCaption = `My Cool Program 1.0'; procedure DoSomething; implementation const BaseX = 20; BaseY = 200; procedure DoSomething; begin { Code for DoSomething goes here. } end; end.
Because the AppCaption constant is declared in the interface section, it can be used anywhere in the unit and in any unit that has this unit in its uses list. The BaseX and BaseY constants, however, are only available within this unit because they are declared in the implementation section.
The const keyword has other uses besides the one described here. I'll discuss one of those uses tomorrow in the section, "Value, Constant, and Variable Parameters."
The type Keyword
New Term: The type keyword is used to declare new types that your program will use.
Declaring a new type is an esoteric programming technique that is difficult to explain at this stage of the game, so perhaps an example will help. Let's say that your application needs an array (a collection of values) of 20 bytes and that this type of array will be used over and over again. You can declare a new type as follows:
type TMyArray = array [0..19] of Byte;
Now you can use the identifier TMyArray instead of typing out array [0..19] of Byte every time you want an array of 20 bytes. I'll have to leave it at that for now, but you'll see more examples of declaring types later in the book.
The var Keyword
New Term: The var keyword is used to declare a section of code in which variables are declared.
You use the var keyword to declare variables (variables are discussed in detail in the section entitled "Variables"). There are several places you can declare a var section. You can have a var section at the unit level, you can have a var section for a procedure or function, or both. You can even have multiple var sections in a unit. Listing 1.5 shows the sample unit with type and var sections added.
unit Unit2; interface type
TMyArray = array [0..19] of Byte;
const AppCaption = `My Cool Program 1.0'; var X : Integer; MyArray : TMyArray; procedure DoSomething; implementation const BaseX = 20; BaseY = 200; procedure DoSomething; begin { Code for DoSomething goes here. } end; end.
As with the const keyword, the var keyword has more than one use. It is also used to declare function and procedure parameters as variable parameters. Rather than go into that now, I'll save that discussion for tomorrow when you read about functions and procedures.
NOTE: The sections described by the var, const, and type keywords begin at the keyword and end at the next keyword in the unit.
Before getting into the Pascal language in detail, let me talk briefly about commenting code. Comments are lines of text in your source code that are there for documentation purposes. Comments can be used to describe what the code does, to supply copyright information, or simply to make a note to yourself or other programmers.
Comments can be designated in as many as three different ways. The following are all valid comments lines:
{ Don't forget to free this memory! } { ADTAPI.PAS 2.50 Copyright (c) TurboPower Software 1996-98 } (* Mason needs to fix this section of code *) // This is really good code! { This code needs to be reworked later }
Probably the most common type of comment used in Delphi programs uses curly braces as illustrated in the first two cases above. The opening brace is used to start a comment, and the closing brace is used to end a comment. Another type of comment uses (* to start the comment, and *) to end the comment. There is one difference between comments designated this way as opposed to using curly braces: The (*/*) comment pair can be used to block out large sections of code containing other comment lines. These two comment types can be used to comment single lines of code or multiple lines.
NOTE: Curly braces have another use in Pascal. When used in conjunction with a dollar sign, the braces signify a compiler directive. To tell the compiler not to generate compiler hints, you can put a line like this in your source code:{$HINTS OFF}
When the compiler sees this line, it stops generating hints in this unit until a corresponding {$HINTS ON} directive is encountered. I'll talk about individual compiler directives at different points in the book as the need arises.
The third type of comment is designated by the double slash. This is often called the C-style comment because it is used by C and C++. This type of comment can only be used on single lines of code. You should also be aware that this type of comment is not valid in all versions of Delphi. If you are writing code that might be used in Delphi 1 as well as later versions, you should be sure not to use this style of comment.
NOTE: I use the curly brace style of comment for production code (code that others will see). I use the double slash type of comment for quickly commenting out a line or two for testing purposes, but only as a temporary measure. I rarely use the (*/*) style of comment.
Any commented text is ignored by the compiler. If you are using the default Delphi IDE settings, all comment lines will show up in italicized, blue text. This makes it easy to quickly identify comment lines.
NOTE: If you work in a team programming environment, you might have to read your coworkers' code and vice versa. Concise comments in the code can save hours of time for any programmer who has to read and maintain another programmer's code. Even if you work in a single-programmer environment, commenting your code is a good idea. You'd be surprised how quickly you forget what code you wrote is supposed to do. Good code commenting can save you and your coworkers hours of time, so don't forget to comment your code!
Variables have to be declared before they can be used. You declare a variable in a special section of code designated with the var keyword, as described earlier--for example,
var X : Integer; { variable X declared as an integer variable } Y : Integer; { variable Y declared as an integer variable }
Earlier, I talked about the var keyword in terms of a Pascal unit. In that section, I said that variables used in the unit are declared in the unit's var section. That's true, but you can also have a var section in a function or procedure. This enables you to declare variables in functions and procedures as well as in units. Here's an example of a var section in a procedure:
procedure TForm1.Test; var S : string; begin S := `Hello World!'; Label1.Caption := S; end;
After you declare a variable, you can then use it to manipulate data in memory. That probably doesn't make much sense to you, so let me give you a few examples. The following code snippet uses the variables called X and Y declared earlier. At the end of each line of code is a comment that describes what is happening when that line executes:
X := 100; { `X' now contains the value 100 } X := X + 50; { `X' now contains the value 150 } Y := 150; { `Y' now contains the value 150 } X := X + Y; { `X' now contains the value 300 } Inc(X); { Increment. `X' now contains the value 301 }
A variable is a location set aside in computer memory to contain some value.
I want you to notice several things about this code. First, notice that the value of X changes as the variable is manipulated. (A little later I'll discuss the Object Pascal operators, functions, and procedures used to manipulate variables.) You can see that the variables are assigned values, added together, incremented, and so on.
Notice also that each statement in this code segment ends in a semicolon. The semicolon
is used at the end of every statement in a Pascal program.
NOTE: Very early in the process of learning the Pascal language, the budding programmer must learn the difference between an expression and a statement. The official definition of a statement is an expression that is followed by a semicolon. An expression is a unit of code that evaluates to some quantity. Confused? Consider the following statement:c := a + b;
In this example, the portion to the right of the assignment operator, a + b, is an expression. The entire line is a statement. You could say that an expression is a subset of a statement. A single statement can be made up of several expressions. I know this might be a bit confusing at the moment, but it will become clearer as you go along. For now just remember that a statement is followed by a semicolon. (There are some cases in which a semicolon is not used at the end of each line, but this does not violate the rule that a semicolon is placed at the end of each statement. I'll go over those exceptions later in the book as we encounter them.)
Variable names follow the rules described for identifiers. In addition to variables, identifiers are used for function names, procedure names, fields in records, unit names, and more. Identifiers can mix uppercase and lowercase letters and can include numbers and the underscore (_), but they cannot contain spaces or other special characters. The identifier must start with a character or the underscore. There is no maximum allowable length for identifiers, but anything over 255 characters is ignored. In reality, anything more than about 20 characters is too long to be useful anyway. The following are examples of valid variable names:
aVeryLongVariableName : Integer; { a long variable name } my_variable : Integer; { a variable with an underscore } x : Integer; { single digit variable name } X : Integer; { same as above } Label2 : string; { a variable name containing a number }
NOTE: The Pascal language is not case sensitive. The following statements are all valid:var XPos : Integer; { ...later } XPos := 20; XPOS := 200; xpos := 110; XpoS := 40;
If you are coming from a language where case counts (C or C++, for instance), the case-insensitive nature of Object Pascal might seem a bit odd at first, but you'll get used to it quickly enough.
NOTE: Even though Pascal is case insensitive, you should strive to use consistent capitalization in your programs. Using proper capitalization makes a program easier to read and will save more than a few headaches if you ever need to port your application to other programming languages later on (porting a Delphi program to C++Builder, for example).
New Term: In Object Pascal, a data type defines the way the compiler stores information in memory.
In some programming languages, you can get by with assigning any type of value to a variable. For example, look at the following examples of BASIC code:
X = -1; X = 1000; X = 3.14;
In BASIC, the interpreter takes care of allocating enough storage to fit any size or type of number.
In Object Pascal, you must declare a variable's type before you can use the variable:
var X1 : Integer; X : Integer; Y : Double; Z : Byte; { ...later } X1 := -1; X := 1000; Y := 3.14; Z := 27;
This enables the compiler to do type-checking and to make sure that things are kept straight when the program runs. Improper use of a data type will result in a compiler error or warning that can be analyzed and corrected so that you can head off a problem before it starts.
Some data types are signed and some are unsigned. A signed data type can contain both negative and positive numbers, whereas an unsigned data type can contain only positive numbers. Table 1.1 shows the basic data types in Object Pascal, the amount of memory each requires, and the range of values possible for each data type. This table does not include the string types. Those are discussed later in the section, "Strings."
Data Type | Size in Bytes | Possible Range of Values |
ShortInt | 1 | -128 to 127 |
Byte | 1 | 0 to 255 |
Char | 1 | 0 to 255 (same as Byte) |
WideChar | 2 | 0 to 65,535 (same as Word) |
SmallInt | 2 | -32,768 to 32,767 |
Word | 2 | 0 to 65,535 |
LongInt | 4 | -2,147,483,648 to 2,147,483,647 |
Int64 | 8 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
Integer | 4 | Same as LongInt |
Cardinal | 4 | 0 to 2,147,483,647 |
Single | 4 | 1.5 ¥ 10-45 to 3.4 ¥ 1038 |
Double | 8 | 5.0 ¥ 10-324 to 1.7 ¥ 10308 |
Real | 8 | 5.0 ¥ 10-324 to 1.7 ¥ 10308 (same as Double) |
Extended | 10 | 3.4 ¥ 10-4932 to 1.1 ¥ 104932 |
Comp | 8 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
Currency | 8 | -922,337,203,685,477.5808 to 922,337,203,685,477.5807 |
Boolean | 1 | True or False |
Variant | 16 | Varies |
Examining Table 1.1, you might notice that an Integer is the same as a LongInt. So why does Object Pascal have two different data types that are exactly the same? Essentially, it's a holdover from days gone by. In a 16-bit programming environment, an Integer requires 2 bytes of storage and a LongInt requires 4 bytes of storage.
In a 32-bit programming environment, however, both require 4 bytes of storage and have the same range of values. Delphi 4 produces only 32-bit programs, so an Integer and a LongInt are identical. Most programmers use Integer rather than LongInt.
You might also notice that the Int64 and Comp (computational) types have an identical range of values. The difference between these two types is in the way they are treated internally by the compiler. The Int64 type is an integer type, whereas the Comp type is a real type. Probably you will have very little reason to use the Comp type in your programs.
Notice also that the Real and Double data types are identical. In previous versions of Delphi, the Real type was a 6-byte variable. Now it is an 8-byte variable. This change was made to make the Real data type compatible with today's processors. The Real type is considered obsolete and you should use Double rather than Real in your Delphi applications.
NOTE: The Int64 data type is new to Delphi 4. There are many reasons for an integer type of this size. One of the most compelling is the need for an integer value that can hold the huge values required by today's larger hard drives. For example, Windows contains a function called GetDiskFreeSpaceEx, which can return values much larger than 2,147,483,647 (the maximum value of an Integer). A 64-bit integer data type was needed for reasons like this.
NOTE: The Single, Double, Extended, and Currency data types use floating-point numbers (numbers with decimal places). The other data types deal only with integer values. You cannot assign a value containing a decimal fraction to an integer data type. For example, the following code will generate a compiler error:var X : Integer; { Later... } X := 3.75;
You don't really have to worry about this too much, because the compiler is very good at telling you what you can and cannot do. By the way, you'd be surprised how few times you need floating-point numbers in most Windows programs.
Object Pascal performs conversion between different data types when possible. Take the following code snippet for an example:
var Res : SmallInt; Num1 : Integer; Num2 : Integer; { Later... } Num1 := 200; Num2 := 200; Res := Num1 * Num2;
In this case I am trying to assign the result of multiplying two Integers to a SmallInt. Even though this formula mixes two data types, Object Pascal is able to perform a conversion. Would you like to take a guess at the result of this calculation? You might be surprised to find out that the result is -25,536. What!? If you look at Table 1.1, you'll see that a SmallInt can have a maximum value of 32,767. What happens if you take a SmallInt with a value of 32,767 and add 1 to it? You will get a value of -32,768. This is essentially the same as the odometer on a car turning over from 99,999 to 00,000 when you drive that last mile. To illustrate, perform the following steps:
procedure TForm1.Button1Click(Sender: TObject); var X : SmallInt; begin X := 32767; X := X + 1; Label1.Caption := IntToStr(X); end;
You should see the caption of the label change to -32768 when you click the button (in case you wondering, the IntToStr function translates an integer value to a string). This exercise illustrates that 32767 plus 1 equals -32768! Okay, maybe not quite.
This example really illustrates what is known as overflow or wrapping. You should be aware of the maximum possible values your variables can contain and choose the data type that is large enough to guarantee that the variable will contain the value without overflowing. For the most part, you won't go too far wrong if you use the Integer data type as your data type of choice. You are unlikely to run into the problem of wrapping because the Integer data type gives you an approximate range of -2 billion to +2 billion.
Okay, where was I? Oh, yes, I was talking about automatic type conversion. In some cases, Object Pascal cannot perform a conversion. If that is the case, you will get a compiler error that says something along the lines of Incompatible types: `Integer' and `Real'. This compiler error is telling you that you are trying to assign a value that cannot be stored by this particular data type. Another compiler error you might see has to do with what is called range checking. Take this code, for instance:
var X : Byte; begin X := 1000; end;
This code will generate a compiler error that states Constant expression violates subrange bounds. The compiler is telling you that you can't assign a value of 1000 to the variable X because X is declared as a Byte and a Byte can only hold values from 0 to 255.
TIP: Learn to treat compiler hints and warnings as errors. The compiler is trying to tell you that something is not quite right in your code, and you need to respect that warning. Ultimately, you should strive for warning-free compiles. In rare cases, a warning cannot be avoided, but be sure to examine all warnings closely. Do your best to understand the reason for the warning and correct it if possible.
Operators are used to manipulate data. Operators perform calculations, check for equality, make assignments, manipulate variables, and perform other, more esoteric duties that most programmers never do. There are a lot of operators in Object Pascal. Rather than present them all here, I will list only the most commonly used ones. Table 1.2 contains a list of those operators.
Operator | Description | Example |
Mathematical Operators |
||
+ | Addition | x := y + z; |
- | Subtraction | x := y - z; |
* | Multiplication | x := y * z; |
/ | Real number division | x := y / 3.14; |
div | Integer division | x := y div 10; |
Assignment Operators |
||
:= |
Assignment |
x := 10; |
Logical Operators |
||
and |
Logical AND |
if (x = 1) and (y = 2) then ... |
or |
Logical OR |
if (x = 1) or (y = 2) then ... |
Equality Operators |
||
= |
Equal to |
if (x = 10) then ... |
<> |
Not equal to |
if (x <> 10) then ... |
< |
Less than |
if (x < 10) then ... |
> |
Greater than |
if (x > 10) then ... |
<= |
Less than or equal to |
if (x <= 10) then ... |
>= |
Greater than or equal to |
if (x >= 10) then ... |
Unary Operators |
||
^ |
Pointer operator |
MyObject.Data^; |
@ |
Address of operator |
ptr := @MyRecord; |
and |
Bitwise AND |
x := x and $02; |
or |
Bitwise OR |
x := x or $FF; |
not |
Bitwise NOT |
x := x and not $02; |
not |
Logical NOT |
if not Valid then ... |
Miscellaneous Operators |
||
$ |
Hex value operator |
X := $FF; |
[] |
Array subscript operator |
X := MyArray[5]; |
. |
Membership (dot) operator |
X := Record.Data; |
As you can see, the list of operators is a bit overwhelming. Don't worry about trying to memorize each one. As you work with Object Pascal, you will gradually learn how to use all the operators. Some operators you will rarely, if ever, use, and others you will use all the time.
You will notice that the and, or, and not keywords are used in two contexts: logical and bitwise. For example, the and keyword can be used to specify a logical AND operation or a bitwise AND operation. Take a look at this code:
if (Started = True) and (X > 20) then Z := X and Y;
In this example, the and keyword is being used in two completely different contexts. Without question, this can be confusing at first. Rest assured that the compiler knows how the keyword is being used and will do the right thing. I'm getting a bit too far ahead this early in the book, so don't worry if this isn't making much sense right now. Later on it will almost certainly make more sense than it does right now.
You will see many examples of these operators as you go through this book. Rather than try to memorize the function of each operator, try instead to learn through careful study of the sample programs and code snippets.
As I said earlier, a constant is an identifier assigned to a value that does not change. The terms "variable" and "constant" were not chosen at random. A variable's value can be changed by the programmer; a constant's value cannot be changed. Constants are declared using the const keyword. To declare a constant, simply list the constant's name and its value--for example,
const DefaultWidth = 400; DefaultHeight = 200; Description = `Something really cool.';
Notice that when declaring a constant, the equal sign is used and not the assignment operator (:=). Notice also that no data type is specified. The compiler determines the data type of the constant based on the value being assigned. The constants can then be used in your code where you would normally have used a literal value.
Judicious use of constants makes the behavior of a program easy to change at a later date if change becomes necessary. To change the behavior of the program, it is only necessary to change the value of one or more constants at the top of the unit, rather than hunting through the unit for every occurrence of 100 and changing it to 120.
You can place any of the intrinsic Object Pascal data types into an array. An array is simply a collection of values. For example, let's say you want to keep an array of Integers that holds five integer values. You would declare the array as follows:
var
MyArray : array[0..4] of Integer;
In this case, the compiler allocates memory for the array, as illustrated in Figure 1.4. Because each integer requires 4 bytes of storage, the entire array will take up 20 bytes in memory.
FIGURE 1.4. Memory allocation for an array of five integers.
Now that you have the array declared, you can fill it with values using the subscript operator ([]) as follows:
MyArray[0] := -200; MyArray[1] := -100; MyArray[2] := 0; MyArray[3] := 100; MyArray[4] := 200;
Later in your program, you can access the individual elements of the array, again by using the subscript operator:
X := MyArray[3] + MyArray[4]; { result will be 300 }
Arrays can be multidimensional. To create a two-dimensional array of integers, you would use code like this:
var MdArray : array[0..2, 0..4] of Integer;
This allocates storage for 15 Integers (a total of 60 bytes, if you're keeping score). You access elements of the array like you do a simple array, with the obvious difference that you must supply two subscript operators. There are two ways of doing this. The following two lines have the same result:
X := MdArray[1][1] + MdArray[2][1]; X := MdArray[1, 1] + MdArray[2, 1];
Figure 1.5 illustrates how a two-dimensional array might look in memory.
FIGURE 1.5. A two-dimensional array in memory.
NOTE: Under normal circumstances, range checking will keep you from attempting to write beyond the end of an array. For example, the following code will result in a compiler error:var MyArray : array[0..4] of Integer; X : Integer; begin X := MyArray[3] + MyArray[5]; { Oops! 5 outside of range. } end;
The error will state Constant expression violates subrange bounds because MyArray[5] is outside of the declared range for the array.
The array range is defined when you declare the array. For example, if you want to create an array with a lower bound of 10 and an upper bound of 20, you declare it like this:
var MyArray : array[10..20] of Integer;
Now the only elements of the array that can be accessed are elements 10 (the first element in the array) through 20 (the last element in the array). Array constants must be declared and initialized all at one time. The syntax looks like this:
const myArray : array[0..4] of Integer = ( -200, -100, 0, 100, 200 );
The Low and High functions are used frequently when dealing with arrays. As I said earlier, an array can be declared with variable lower and upper bounds. The Low function will return the lower bound of an array, and the High function will return the upper bound of the array--for example,
var X, I, Lower, Upper : Integer; MyArray : array[10..20] of Integer; begin { Code to initialize MyArray here. } Lower := Low(MyArray); { Lower now contains 10 } Upper := High(MyArray); { Upper now contains 20 } X := 0; for I := Lower to Upper do X := X + MyArray[I]; { Now do something with X. } end;
Using the Low and High functions ensures that you don't attempt to access an array value outside of the array bounds.
Delphi 4 introduces the concept of dynamic arrays. A dynamic array is declared without an initial size, and no storage is set aside for the array at the time of declaration. Later the array can be created with a specified size using the SetLength function. Here's how it would look:
var BigArray : array of Integer; { no size } X : Integer; begin X := GetArraySize; { function which returns the needed size } SetLength(BigArray, X); { dynamically allocate array } { Now fill in and use BigArray } end;
New Term: A dynamic array is an array for which memory is allocated at runtime. A dynamic array can be made larger or smaller depending on the needs of the program.
The significance is that the array can be allocated based on exactly the number of elements required. To illustrate, let's say that you need an array of integers. Let's further say that in some cases you might only need to allocate enough memory for 10 integers, but in other cases you might need to allocate as many as 1,000 integers.
Your program doesn't know at compile time how many elements will be needed--that number will not be known until runtime. Before the advent of dynamic arrays, you would have been forced to declare an array with a size of 1,000 integers, wasting a lot of memory if your application really only needs 10, 20, or 30 integers. With dynamic arrays you can allocate only as much storage as is required at a given time.
You can reallocate an array using the Copy function. For example, let's say you initially created an array with a size of 100 elements, and you now need to reallocate the array to a size of 200 elements. In that case, the code would look like this:
Copy(BigArray, 200);
The contents of the array are retained and the array size is increased by 100 elements to a total of 200 elements.
Two-dimensional dynamic arrays are created in much the same way. To create a two-dimensional array, you use code like the following:
var BigArray : array of array of Integer; begin SetLength(BigArray, 20, 20); BigArray[0][0] := 200; { More code here. } end;
After a dynamic array is created, its elements are accessed just like a regular array.
Strings are used heavily in programming. Object Pascal has three distinct string types: long string, short string, and wide string. In addition to these string types, Pascal also uses null-terminated strings. I'll go over each of these types briefly, and then I'll discuss some of the string-manipulation functions.
The short string type is a fixed-length string of characters with a maximum size of 255 characters. You declare a short string in one of two ways. One way is to use the predefined type ShortString to declare a short string with a size of 255 bytes. You can also use the string keyword with the subscript operator to specify a size when you declare the string:
var S1 : ShortString; { 255 characters long } S2 : string[20]; { 20 characters long }
String manipulation using short strings is fast because the size of the memory allocated for the string doesn't change. Still, the short string is considered an obsolete type and it is recommended that long strings be used instead. Short strings are termed length-byte strings because the first element of the string contains the length of the string (the number of characters in the string). You can read the value of the first element of a short string to determine the string's length--for example,
var
S : ShortString; { 255 characters long } Len : Integer; begin S := `Hello'; Len := Ord(S[0]); { `L' now contains the length of S, or 5 } end;
This example reads the value of S[0] to determine the string's length. You can also use the Length function to determine the length of a short string. I'll discuss the Length function in just a bit.
NOTE: You use the Ord function to convert the value of a Char type to an integer value (and ordinal value). The Ord function is also used with enumerations.
If needed, you can write to the first element of a short string to specifically set the length of the string. This is required in certain programming situations, which I won't go into here. I should add that, in general, use of the 0 byte of a short string is an advanced programming technique and is not recommended for beginning programmers.
The long string data type is a dynamically allocated string object. The size of a long string is limited only by available memory. Object Pascal allocates and de-allocates memory for the string as needed. Long strings are very flexible but are sometimes slower than short strings when a lot of string-manipulation is being done. This is due to the overhead needed to dynamically allocate storage for the long string as the string's contents change. Still, unless execution speed is critical, you should generally stick to using long strings in your applications.
To declare a long string, simply use the string keyword without a size parameter:
var S : string; { long string, dynamically allocated }
Because the string is dynamically allocated, you can modify the string in any way you want and never have to worry about what is going on behind the scenes. The long string is very easy to use because you don't have to worry about running out of space or about memory allocation for the string. It's all more or less automatic.
Long strings do not have a 0 element as short strings do. Attempting to access the 0 element of a long string will result in a compiler error. Instead, you get the length of a long string using the Length function and set the length using the SetLength procedure. I'll discuss the string manipulation functions in the section "String Functions."
The wide string type is used when dealing with Windows API functions that require double-byte character strings (Unicode character strings). The wide string is like the long string in that the size is limited only by available memory and memory for the string is dynamically allocated. I won't go into any detail on wide string because its use is limited primarily to dealing with OLE functions.
Unlike Object Pascal, the C and C++ languages do not have true string data types. In C and C++, strings are implemented as an array of characters terminated with a terminating null (a 0 at the end of the string). Character arrays don't have a length byte, so the terminating null is used to mark the end of the string of characters. Because Windows was written in C, many Windows functions require a character array as a parameter. The Pascal string types are not character arrays, so a way of enabling Pascal strings to work with Windows functions requiring a character array is needed. The PChar type fills this need. A PChar can be used anywhere a character array is needed. An example is the Windows MessageBox function. This function, which displays a standard Windows message dialog, has the following declaration:
function MessageBox(hWnd: HWND; lpText, lpCaption: PChar; uType: UINT): Integer;
The second and third parameters require a pointer to a character array (the second for the message box text and the third for the message box caption). In order to call this function from a Delphi program, you have to use the PChar type as follows:
var Text : string; Caption : string; begin Text := `This is a test.; Caption := `Test Message'; MessageBox(0, PChar(Text), PChar(Caption), 0); end;
Here the PChar is used to cast the Pascal long string to a null-terminated string. You can also use a PChar by itself. The following illustrates:
var Text : PChar; begin Text := `This is a test.'; MessageBox(0, Text, `Message', 0); end;
Because the strength of the Pascal string types is in string manipulation, you probably won't use a PChar like this very often. You will typically use a PChar to convert a long string to a null-terminated string as in the previous example. Note that you can pass a string literal (a string of characters within single quotes) to a Windows API function expecting a PChar.
Finally, you can use an array of the Char data type in place of a PChar. Once again, the previous code snippet is modified to illustrate:
var Text : array [0..20] of Char; begin Text := `This is a test.'; MessageBox(0, Text, `Message', 0); end;
It really doesn't matter which of these methods you use. Just understand that you cannot use a Pascal string data type to call Windows API functions that require a null-terminated string as a parameter. In those cases, you have to use PChar or an array of Char.
The Pascal string types have several elements in common. The following sections describe general string operations that apply to all string types.
String Concatenation Using the + Operator
A common programming task is that of concatenating (adding together) strings. Strings can be concatenated using the + operator--for example,
var S1 : string; S2 : string; begin S1 := `Mallory Kim'; S2 := `Reisdorph'; Label1.Caption := S1 + ` ` + S2; end;
This code concatenates three strings (the variable S1, a string literal containing a space, and the variable S2) and assigns the result to a label's Caption property. Any expression or function that evaluates to a string can be used in concatenation. Here's another example:
var X : Integer; begin X := 199; Label1.Caption := `The result is: ` + IntToStr(X); end;
In this case, the IntToStr function returns a string so that you can use the result from that function anywhere a string is required.
The Subscript Operator
Another common aspect of Pascal strings is the subscript operator ([]). You can extract an individual character from a string using the subscript operator, as follows:
var S1 : string; S2 : Char; begin S1 := `Hello World!'; S2 := S1[1]; Label1.Caption := S2; end;
The variable S2 in this example is a Char, but it could have been a long string, a short string, or a wide string. Object Pascal makes the proper conversions behind the scenes so you don't have to deal with the different string types at the application level. The subscript operator is handy when you need to search through a string one character at a time.
Strings are one-based: the first character in the string is at S[1]. Remember that the 0 element of a short string (S[0]) contains the length of the string and not the first character in the string. You cannot access S[0] in long strings or wide strings.
Control Characters in Strings
Object Pascal enables you to embed control characters in strings. This is useful if you need to add non-printing characters to your strings. This could be as simple as starting a new line in a character string, or it could be more complex, such as embedding control characters in a string sent to a serial device.
You add control characters to a string using the # character. If, for example, you want to embed an escape character (ASCII 27) in your string, you would do so as follows:
S := `This is a test. Escape follows.'#27'Finished.';
Notice that the embedded character, #27, is placed outside of any literal character string, and that no spaces are between the embedded character and the preceding and following strings. You must follow this structure when using embedded characters. Of course, you don't have to use literal strings, you could use string variables as well:
S1 := `This is a test. Escape follows.'; S2 := `Finished.'; S3 := S1 + #27 + S2;
You can easily test this theory. Place a button and a label on a form. Double-click the button and add this line to the button's OnClick event handler:
Label1.Caption := `Line 1' + #10 + `Line 2';
Now run the program and click the button. The label will contain two lines of text, as shown in Figure 1.6. This code simply embeds a carriage return character (ASCII 10) in the string, thereby breaking the label into two lines.
FIGURE 1.6. A label with two lines.
Extending Strings Across Multiple Code Lines
It is often necessary to break a literal string across two or more code lines to increase readability and maintainability of your code. A long text message, for example, might be well over 200 characters. You could put all of those characters on one line of code (the maximum line length of the Delphi Code Editor is 1,024 characters), but that would make the code almost impossible to read. Instead you can split the string across multiple lines. To do that you need to use the + operator--for example,
MessageBox(0, `This is a very, very long message ` + `that seems to go on and on forever. In order ` + `to make the code more readable the message has ` + `been split across several lines of code.', `Message', 0);
Remember earlier when I talked about semicolons at the end of each code statement? Here's an example where a statement is spread across multiple lines. It's still a single statement as far as the compiler is concerned, so the semicolon is at the end of the statement and not at the end of each line.
String Comparison
Strings can be compared using the comparison operators. Table 1.3 lists the usual operators and their descriptions.
Operator | Description |
= | Equal to |
<> | Not equal to |
< | Less than |
> | Greater than |
<= | Less than or equal to |
>= | Greater than or equal to |
Note that these operators compare strings based on their ASCII values. Most of the time you will use just the equality operators to see whether a string is equal to a certain value or not equal to a certain value. If you are doing string sorting, you will probably use the other string comparison operators as well. The following example checks to see whether a string contains a certain value:
if FileName = `TEST.TXT' then OpenFile(FileName) else ReportError;
Object Pascal includes many functions and procedures for string manipulation. Table 1.4 lists a few of the most commonly used string functions and procedures; this is by no means a complete list. Consult the Delphi online help for a list of all string functions and procedures.
Name | Description |
Copy | Returns a sub-string within a string. |
Delete | Deletes part of a string. |
Format | Formats and returns a string based on the format string and arguments passed. |
Insert | Inserts text into a string. |
IntToStr | Converts an integer value to a string. |
Length | Returns the length of a string. |
LowerCase | Converts a string to lowercase. |
Pos | Returns the position of a search string within a string. |
StringOfChar | Returns a string filled with the given number of a particular character. |
StrPas | Converts a null-terminated string (PChar or array of Char) to a Pascal-style string. |
StrPCopy | Converts a Pascal-style string to a null-terminated string. |
StrToInt | Converts a string to an integer. If the string cannot be converted, an exception is thrown. |
StrToIntDef | Converts a string to an integer and supplies a default value in case the string cannot be converted. No exception is thrown if the string cannot be converted. |
StrToXXX | Additional conversion functions that convert a string to a floating point, Currency, Date, or Time value. |
Trim | Trims leading and trailing blank space from a string. |
UpperCase | Converts a string to uppercase. |
XXXToSTr | Additional conversion functions that convert a floating point, Currency, Date, or Time value to a string. |
NOTE: Object Pascal has an additional set of functions that operates on null- terminated strings. I won't list all of those here, because most of the time you will be working with Pascal strings and not null-terminated strings. Check the Delphi help for additional information on those functions. Most of the functions that operate on null-terminated strings begin with Str.
A few of the functions and procedures listed in Table 1.4 deserve special mention. The StrToInt function converts a string to an integer value. Let's say you have an edit component on a form that will be used to retrieve an integer value from the user. Because an edit component only holds text, you need to convert that text to an integer. You can do it like this:
Value := StrToInt(Edit1.Text);
The other StrToXXX functions (StrToFloat, StrToDate, and so on) work in exactly the same way. Note that these functions will throw an exception if the conversion cannot be made. If, for example, the user enters S123, an exception will be thrown because the letter S cannot be converted to an integer. I haven't talked about exceptions yet, so I won't go into detail on exceptions at this time.
The Format function enables you to build a string by passing a format string and additional arguments. The following is an example that adds two numbers and then uses Format to build a string to report the result:
var S : string; X : Integer; begin X := 10 * 20; S := Format(`The result is: %d', [X]); Label1.Caption := S; end;
When this section of code executes, the label contains this text:
The result is: 200
In this example, the %d tells the Format function, "An integer value will go here." At the end of the format string, the variable X is inserted to tell Format what value to put at that location in the string (the contents of the variable X).
Format is a unique function in that it can take a variable number of arguments. (That is why the variable X is in square brackets; the arguments passed are in the form of an array of const.) You must supply the format string, but the number of arguments that come after the format string is variable. Here is an example of Format that uses three additional arguments:
var X : Integer; Y : Integer; begin X := 20; Y := 5; Label1.Caption := Format(`%d + %d = %d', [X, Y, X + Y]); end;
When this piece of code executes, the result displayed in the label will be:
20 + 5 = 25
Notice that in this example I am assigning the return value from Format directly to the Caption property of a label. In the previous example I assigned the return value from Format to a variable, but that step was not strictly necessary.
Additional format specifiers are used to display a number as a floating point, in scientific notation, in hexadecimal, or to display characters and strings. You can specify the number of decimal places to use for floating-point numbers and the number of digits to display for integer values. See the "Format Strings" topic in the Delphi help for full details.
You've covered a lot of ground today. First, you got to tinker with the Delphi IDE by creating a Hello World! program. Following that, you got to do a little more interesting programming when you created Hello World!, Part II. After the initial playing around, you were put to work learning the basics of the Object Pascal language. There is a lot of material to absorb in this chapter. Don't feel bad if you can't remember it all. Go back and review if you are unclear about anything presented today.
The Workshop contains quiz questions to help you solidify your understanding of the material covered and exercises to provide you with experience in using what you have learned. You can find answers to the quiz questions in Appendix A, "Answers to the Quiz Questions."
var top : Integer; Top : Integer;
8. What is the maximum length of a short string?
MyArray : array [0..10] of Byte;
© Copyright, Macmillan Computer Publishing. All rights reserved.