Chapter II-delphi Object-oriented Programming (II.) (2)

Source: Internet
Author: User
Tags constant count integer ord reserved

2.1.7 about the scope of the action

Scope of 2.1.7.1 identifiers

The scope of a variable, constant, method, type, or other identifier defines the active region of the identifier. This identifier is local to the smallest program module that describes this identifier. When your application executes outside of a program module that describes an identifier, the identifier is not in this range. This means that the program that is executing at this time cannot access the identifier, and it can only be accessed if the program enters the program module that describes the identifier again.

The diagram below represents a project with two library units, each of which has three process or event processes in each library unit.

2.1.7.2 access to instructions in other program modules

You can access the instructions in the other program modules in the current program module. For example, you write an event-handling procedure in a library unit to calculate interest rates, and other library units can access this event-handling process. To access a description that is not in the current library unit, precede this description with the name of the other application and a point number (.). For example, there is an event-handling process calculateinterest procedure in the library unit Unit1, and now you want to call this procedure in the Library unit Unit2, you can add uses to the UNIT2 UNIT1 clause and use the following description:

Unit1.calculateinterest (principalinterestrate:double);

The application's code cannot access the variables it describes outside of a module. In fact, when a program executes out of a module, the variables do not exist in memory. This is the same for any identifier, regardless of the event-handling process, procedure, function, or method. Such identifiers are called local variables.

2.1.7.3 by Scope Description identifier

You can describe an identifier in different parts of your application, only to make sure that they have a different range of validity. The compiler automatically accesses the identifier closest to the current scope.

The global variables of the library unit can generally be explained after the reserved character implementation. For example, the following routine implementation adds the integers in the two edit boxes and displays them in the third edit box. The global variable count with an integer was used:

... implememntation

Var

Count:integer;

Procedure Tform1.addclick (Sender:tobject);

Var

Firstnumber,secondnumber:integer;

Begin

Count: = count + 1;

Counter.text: = IntToStr (Count);

Firstnumber: = Strtoint (Edit1.text);

Secondnumber: = Strtoint (Edit2.text);

Edit3.text: = IntToStr (Firstnumber+secondnumber);

End

...

In order to achieve a button count added once per press, the whole variable count must be initialized. At the end of the library unit, add the reserved word initialization and the code that initializes the count before the last. Reserved word:

...

Initialization

Count: = 0;

This way, when the event-handling process AddClick is triggered, count is incremented one time to represent the number of computations. If you are programming with object-oriented objects, count can be described as a field in a form, as described in the next section.

2.1.8 write a procedure or function

When you develop a Delphi application, most of the code you need is written in the event-handling process, but sometimes you still need to write a function or procedure that is not an event-handling process. For example, you can write a procedure with the resulting statement in multiple event handlers, and any event-handling procedure, procedure, function can call it directly as if it were a procedure or function that already exists. The advantage is that you just write the code once, and the program code is more clear.

2.1.8.1 a self-written function routine

In a program that adds two numbers above, if there is no value in the edit box, the program will be interrupted by an error. To avoid this, write the following function to check if there is a value in the edit box, such as no value, to remind the user to enter:

function Novalue (aneditbox:tedit): Boolean;

Begin

If aneditbox.text= ' Then

Begin

Aneditbox.color: = clred;

Aneditbox.text: = ' Please input integer value ';

Result: = True;

End

Else

Begin

Aneditbox.color: = Clwindow;

Result: = False;

End

End

The Novalue function checks to see if the edit box is empty, and if so, the edit box color turns red and the user is prompted to enter an integer, and then the function returns the true value; the result reserved word is used in Delphi to refer to function return values. Add the Novalue function to the previous routine:

Procedure Tform1.addclick (Sender:tobject);

Var

Firstnumber,secondnumber:integer;

Begin

If Novalue (Edit1) or Novalue (Edit2) Then

Exit

Count: = count + 1;

Counter.text: = IntToStr (Count);

Firstnumber: = Strtoint (Edit1.text);

Secondnumber: = Strtoint (Edit2.text);

Edit3.text: = IntToStr (Firstnumber+secondnumber);

End

If any one of them returns a true value, the edit box is empty, and the exit process is executed, causing the current program module to stop executing and making the edit box appear as a message. When the new value is entered, and then executes the program, the red hint is hidden to restore the normal computational state.

2.1.8.2 the title of Procedures and functions

Each procedure or function starts with a title, including the name of the procedure or function and the parameters it uses. The procedure starts with a reserved character procedure, and the function begins with a reserved word function. The arguments are in parentheses, with each parameter separated by a semicolon. For example:

Procedure Validatedate (Day:integer; month:integer; Year:integer);

You can also group parameters of the same type together, the above procedure header is written:

Procedure Validatedate (Day, Month, Year:integer);

The function also has one more item in the caption: the type of the return value. The following is a function caption that returns a double type:

function Calculateinterest (principal,interestrate:double):D ouble;

2.1.8.3 functions and type descriptions in procedures

A process or Function program module also contains a description section and a statement section. The description section can include a type description, a variable description, a constant description, and so on. In addition to the types already defined in the Object Pascal language, Delphi applications can also create new data types. The type description section begins with the reserved word type. Here are some types of instructions:

Type

Tcount = Integer;

Tprimarycolor = (red,yellow,blue);

Ttestindex = 1..100;

Ttextvalue = -99..99;

ttestlist = array [ttestindex] of Ttestvalue;

Tcharval = Ord (' A '). Ord (' Z ');

Today = (Monday,tuesday,wednesday,thursday,friday,saturday,

Sunday);

After the type identifier, the new type is defined with the "=" number. A type defines a variable's range of values, for example, a variable of type Tcount must be an integer value, and a tprimarycolor type of variable can only be red, yellow, or blue, and so on. The name of each type starts with the letter T, which is not necessary, but it is the practice of Delphi, which is useful when distinguishing between type names and identifiers. The type description can be local or global. If you put it behind the implementation, it means it is global for the library unit, and all event-handling and other procedures and functions can call it. If the type is described in the procedure, it is local, leaving the process and the type will be invalidated.

Generally speaking, in procedures and functions, any type description is preceded by a variable description, and any variable description precedes the constant. However, as long as the compliance instructions must be after the procedure and the function's title, and before the program code, that is valid.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.