Chapter II-delphi Object-oriented programming method (i) (1)

Source: Internet
Author: User
Tags range valid

Delphi's programming language is based on Pascal. Pascal has the characteristics of good readability and easy writing, which makes it very suitable for the development language. Also, applications created using the compiler generate only a single executable file (. EXE), it is this combination, makes Pascal become Delphi this advanced development environment programming language.

In this chapter, we will discuss the main features of Object Pascal and explain how to use it for programming code in event-handling and other applications. This chapter will explain the most commonly used Object Pascal syntax in Delphi applications, not all the details of Pascal language. If you are completely unfamiliar with Pascal programming, see some basic Pascal tutorials. If you have programming experience and are skilled in using other popular programming languages, you will find some of the same concepts in the Object Pascal of this chapter. If you are already familiar with Borland Pascal, you can quickly browse or skip this chapter.

2.1 Writing Object Pascal program code

In the preceding chapters, we have written a few lines of simple code through routines. In this chapter, we will explain the basic methods of Object Pascal programming from the point of view of Pascal programming and with examples.

When writing your own Object Pascal program, pay attention to the readability of the program. Pascal language is a British structure language, in the program to choose the appropriate indentation, capitalization style, and when necessary, the code branches will make the program code can be easily read by themselves and others. The average programmer has this experience: if you do not add appropriate annotations to the program, after a period of time, it is difficult to clarify the process of the process. It is a good programming habit to add annotations in time to the program. Delphi annotations need to be raised between {} and the editor will handle them as blanks. Delphi retains the style of the Borland Pascal Editor, the keyword is in bold text, the part of the annotation will darken, which makes the programming style is good, easy to read and easy to write.

2.1.1 Write an assignment statement

The most common work in event handling is assigning a new value to a property or variable. When you design the user interface, you can use Object Inspector (object Inspector) to change its properties, but sometimes you need to change the value of the property while the program is executing, and some properties can only be changed at execution time, these properties are in the Delphi online Help " The Proprety "topic is marked as an execution-time attribute. To make this change, you must use an assignment statement.

The assignment statement below represents an onclick event. When the button is pressed, set the color property of the edit box part Edit1 to clred:

Procedure Tform1.button1click (Sender:tobject);

Begin

Edit1.color: = clred;

End

When the button is pressed, the assignment statement is executed, and the edit box becomes red.

In the statement, the name of the part is in front of the property, with the middle "." Represents the owning relationship of a property. This specifies exactly which property of the part to assign the clred value to. The assignment number is ": =", regardless of whether you assign a value to the property or to a variable, you assign the value on the right to the property or variable on the left.

When you assign a property value, variable, constant, or text data to a property or variable, the type of the assignment and the type of the property or variable that accepts the value should be the same or compatible. The type of a property or variable defines the set of possible values for this property or variable, and also defines the operations that program code can perform. In the preceding routine, the color property of the edit box part and the type of clred are tcolor. You can find the type of a property in the online Help, and another way to select the property value segment in Object Inspector and press F1, the type will be listed at the end of the property description, such as the color property that lists the following statement:

Property color:tcolor;

Some properties are read only, and they can only be read and cannot be changed. Please refer to the online Help, these read-only properties are annotated in Delphi.

Description and use of 2.1.2 identifiers

Identifiers are the names of some quantities in the Delphi application that include variables (VAR), constants (const), type (type), procedure (Procedure), methods (method), and other, Object Pascal must first describe them when applying identifiers. Object Pascal is a strongly typed language whose compiler checks to make sure that the value assigned to a variable or property is the correct type so that you can correct the error. Because Object Pascal is a compiled language, Delphi executes much faster than the language used to interpret it. By explaining them before using identifiers, you can reduce program errors and increase the efficiency of your code.

2.1.2.1 variable

A variable is an identifier in the program code that represents a memory address, and the memory content of this address can be changed when the program code executes. You must describe the variable before you use it, name it, and describe its type. Add the reserved word var before all the variable descriptions. The variable description is the name of the variable on the left and the type of the variable on the right, separated by the middle (:).

Var

Value, Sum:integer;

line:string;

Add an edit box named Edit1 to the form, add a button part with the name (property name) to add, and establish the following event handling procedure:

Procedure Tform1.addclick (Sender:tobject);

Var

X, Y:integer;

Begin

X: = 100;

Y: = 20;

Edit1.text: = IntToStr (X + Y);

End

In this example, when the Add button is pressed, the value 120 is displayed in the edit box. In Object Pascal, you must ensure that a variable or property is assigned a value of the same or compatible type. You can try to change the value assigned to X to 100.0, or remove the INTTOSTR function, and a type mismatch error occurs at compile time, which also illustrates the characteristics of the object Pascal strongly typed language.

2.1.2.2 Predefined types

Object Pascal has several predefined data types, and you can describe any of these types of variables:

Shaping: Integer range is 32768 to 32767, accounting for 2 bytes of memory, Shortint from 128 to 127, accounting for 1 bytes of memory, Longint from-2147443648 to 2147483647 for 4 bytes of memory; byte from 0 to 255, Occupies 1 bytes, Word from 0 to 65535, and occupies 2 bytes of memory. They are numbers without a decimal part.

Solid: Single can contain 7 to 8-bit valid decimal parts, occupy 4 bytes of memory; The double class can contain 15 to 16-bit valid decimal parts, occupy 8 bytes of memory, and the extended type contains 19 to 20-bit valid decimal parts, consuming 10 bytes of memory ; comp can contain 19 to 20 bits of valid decimal parts, consuming 8 bytes of memory. The above type of real numbers can only be used if the 8087/80287 option [n+] is open. Real can contain 11 to 12-bit valid decimal parts, consuming 6 bytes of memory. It is only used if it is compatible with previous Borland Pascal, or you should use double or extended.

Boolean: Boolean that contains only true or false two values and consumes 1 bytes of memory.

Character type: Char, one ASCII character; string literal a string can be up to 255 ASCII characters long.

Pointer type: pointer, can point to any particular type.

String: Pchar, a pointer to a string ending in 0.

In addition to predefined types, Delphi also has its own type of definition. The tcolor of the above routines is this type. In addition, users can define their own data types, which are described in detail in the following article.

There are five types of integer and solid categories, in the same category, all types are compatible with other categories, and you can assign a value of one type to a variable or property of a different type in the same category, and you need only the range of that value within the range of possible values for the variable or property being assigned. For example, for a shortint variable, you can accept any integer in the range 128 through 127, such as 7 of the shortint type; You cannot assign 300 to it because 300 is already outside the Shortint range. Open the Range Check feature (select options| Project, and selecting range Checking in the compiler Options page, checks for a range error, and if the range Checking is not open, the program code will be executed, but the assigned value will not be the value you expect.

In some cases, you can assign values to different types of variables or properties. In general, a smaller range of values can be assigned to a larger range of values. For example, you can assign an integer value of 10 to a double property that accepts a real value and make the value 10.0, but if you assign a double value to a shape variable, a type error occurs. If you are not sure about type compatibility, refer to the "Type compatibility and Assignment Compatibility" topic in Delphi's online Help.

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.