📄 ch01.htm
字号:
<H4>LISTING 1.5. THE UNIT WITH type AND var SECTIONS ADDED.</H4><PRE>unit Unit2;interfacetype</PRE><PRE> TMyArray = array [0..19] of Byte;</PRE><PRE>const AppCaption = `My Cool Program 1.0';var X : Integer; MyArray : TMyArray; procedure DoSomething;implementationconst BaseX = 20; BaseY = 200; procedure DoSomething; begin { Code for DoSomething goes here. } end;end.</PRE><P>As with the const keyword, the var keyword has more than one use. It is also usedto declare function and procedure parameters as variable parameters. Rather thango into that now, I'll save that discussion for tomorrow when you read about functionsand procedures.</P><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> The sections described by the var, const, and type keywords begin at the keyword and end at the next keyword in the unit. <HR></BLOCKQUOTE><P><H3><A NAME="Heading15"></A>Comments in Code</H3><P>Before getting into the Pascal language in detail, let me talk briefly about commentingcode. <I>Comments</I> are lines of text in your source code that are there for documentationpurposes. Comments can be used to describe what the code does, to supply copyrightinformation, or simply to make a note to yourself or other programmers.</P><P>Comments can be designated in as many as three different ways. The following areall valid comments lines:</P><P><PRE>{ 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 }</PRE><P>Probably the most common type of comment used in Delphi programs uses curly bracesas illustrated in the first two cases above. The opening brace is used to start acomment, and the closing brace is used to end a comment. Another type of commentuses (* to start the comment, and *) to end the comment. There is one differencebetween 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 commentlines. These two comment types can be used to comment single lines of code or multiplelines.</P><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> 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:</P> <PRE>{$HINTS OFF}</PRE></BLOCKQUOTE><PRE></PRE><BLOCKQUOTE> <P>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. <HR></BLOCKQUOTE><P>The third type of comment is designated by the double slash. This is often calledthe C-style comment because it is used by C and C++. This type of comment can onlybe used on single lines of code. You should also be aware that this type of commentis not valid in all versions of Delphi. If you are writing code that might be usedin Delphi 1 as well as later versions, you should be sure not to use this style ofcomment.</P><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> 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. <HR></BLOCKQUOTE><P>Any commented text is ignored by the compiler. If you are using the default DelphiIDE settings, all comment lines will show up in italicized, blue text. This makesit easy to quickly identify comment lines.</P><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> 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! <HR></BLOCKQUOTE><H3></H3><H3><A NAME="Heading16"></A>Variables</H3><P>Variables have to be declared before they can be used. You declare a variablein a special section of code designated with the var keyword, as described earlier--forexample,</P><P><PRE>var X : Integer; { variable X declared as an integer variable } Y : Integer; { variable Y declared as an integer variable }</PRE><PRE>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:</PRE><PRE>procedure TForm1.Test;var S : string;begin S := `Hello World!'; Label1.Caption := S;end;</PRE><P>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. Atthe end of each line of code is a comment that describes what is happening when thatline executes:</P><P><PRE>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 }</PRE><P>A <I>variable</I> is a location set aside in computer memory to contain some value.</P><P>I want you to notice several things about this code. First, notice that the valueof X changes as the variable is manipulated. (A little later I'll discuss the ObjectPascal operators, functions, and procedures used to manipulate variables.) You cansee that the variables are assigned values, added together, incremented, and so on.</P><P>Notice also that each statement in this code segment ends in a semicolon. Thesemicolon</P><P>is used at the end of every statement in a Pascal program.</P><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> 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 <I>statement</I> is an expression that is followed by a semicolon. An <I>expression</I> is a unit of code that evaluates to some quantity. Confused? Consider the following statement:</P> <PRE>c := a + b;</PRE></BLOCKQUOTE><PRE></PRE><BLOCKQUOTE> <P>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.) <HR></BLOCKQUOTE><P>Variable names follow the rules described for <I>identifiers</I>. In additionto variables, identifiers are used for function names, procedure names, fields inrecords, unit names, and more. Identifiers can mix uppercase and lowercase lettersand can include numbers and the underscore (_), but they cannot contain spaces orother special characters. The identifier must start with a character or the underscore.There is no maximum allowable length for identifiers, but anything over 255 charactersis ignored. In reality, anything more than about 20 characters is too long to beuseful anyway. The following are examples of valid variable names:</P><P><PRE>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 }</PRE><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> The Pascal language is not case sensitive. The following statements are all valid:</P> <PRE>var XPos : Integer;{ ...later }XPos := 20;XPOS := 200;xpos := 110;XpoS := 40;</PRE></BLOCKQUOTE><PRE></PRE><BLOCKQUOTE> <P>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. <HR></P> <P><HR><strong>NOTE:</strong> 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). <HR></BLOCKQUOTE><H3></H3><H3><A NAME="Heading17"></A>Object Pascal Data Types</H3><P><strong>New Term:</strong> In Object Pascal, a <I>data type</I> defines the way the compilerstores information in memory.</P><P>In some programming languages, you can get by with assigning any type of valueto a variable. For example, look at the following examples of BASIC code:</P><P><PRE>X = -1;X = 1000;X = 3.14;</PRE><PRE>In BASIC, the interpreter takes care of allocating enough storage to fit any size or type of number.</PRE><H4>Declaring a Variable</H4><P>In Object Pascal, you must declare a variable's type before you can use the variable:</P><P><PRE>var X1 : Integer; X : Integer; Y : Double; Z : Byte;{ ...later }X1 := -1;X := 1000;Y := 3.14;Z := 27;</PRE><P>This enables the compiler to do type-checking and to make sure that things arekept straight when the program runs. Improper use of a data type will result in acompiler error or warning that can be analyzed and corrected so that you can headoff a problem before it starts.</P><P>Some data types are signed and some are unsigned. A <I>signed</I> data type cancontain both negative and positive numbers, whereas an <I>unsigned</I> data typecan contain only positive numbers. Table 1.1 shows the basic data types in ObjectPascal, the amount of memory each requires, and the range of values possible foreach data type. This table does not include the string types. Those are discussedlater in the section, "Strings."</P><P><H4>TABLE 1.1. DATA TYPES USED IN OBJECT PASCAL (32-BIT PROGRAMS).</H4><P><TABLE BORDER="1"> <TR ALIGN="LEFT" VALIGN="TOP">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -