Chapter II-delphi Object-oriented Programming (IV.) (3)

Source: Internet
Author: User
Tags constructor exception handling integer reserved

2.2.6 Assignment of object variables

If two variable types are the same or compatible, you can assign one of the object variables to another object variable. For example, objects TForm1 and TForm2 are types inherited from Tform, and Form1 and Form2 have been described, so you can assign Form1 to Form2:

Form2: =form1;

You can assign an object variable to another object variable as long as the object variable being assigned is the ancestor type of the object variable that is assigned the value. For example, the following is a Tdataform type description, with two variables described in the Variable description section: Aform and DataForm.

Type

Tdataform = Class (Tform)

Button1:tbutton;

Edit1:tedit;

Datagrid1:tdatagrid;

Database1:tdatabase;

Tableset1:ttableset;

Visiblesession1:tvisiblesession;

Private

{Private domain Description}

Public

{Public Domain description}

End

Var

Aform:tform;

Dataform:tdataform;

Because Tdataform is a descendant of the Tform type, DataForm is a descendant of Aform, so the following assignment statement is legal:

Aform: =dataform;

This point is extremely important in Delphi. Let's take a look at the procedure that the application invokes the event-handling process, and the following is the OnClick event-handling process for a button part:

Procedure Tform1.button1click (Sender:tobject);

Begin

End

You can see the TObject class at the top of Delphi's Visual Component Library, which means that all Delphi objects are descendants of TObject. Because sender is a tobject type, any object can be assigned to it. Although you do not see the assigned program code, the fact that the part or control part of the event occurred has been assigned to sender, which means that the sender value is the part or control part that responds to the event.

You can use the reserved word is to test sender to find the type of part or control part that calls this event-handling procedure. A DRAGDROP.DPR project in Delphi that shows Drag-and-drop. Loading it, you can look up the code in the Dropfont.pas Library unit and examine the type of an object variable in the Memo1dragover method. In this case, the argument is source rather than sender.

Procrdure Tform1.memo1dragover (sendersource:tobject; X,y:integer;

State:tdragstate;var Accept:boolean);

Begin

Accept: =source is Tlabel;

End

The source parameter is also the TObject type, and source is assigned the object that is being dragged. The purpose of using the Memo1dragover method is to ensure that only tags can be towed. Accept is a Boolean parameter, and if accept is true, then the user-selected part can be towed, whereas when accept value is false, the user is not allowed to drag and choose the control part. The IS reserved word checks the type of source for Tlabel, so the accept is true only when the user drags a label and is output to the function as a parameter.

The source parameter is also used in the Memo1dragdrop event handling process shown in the following drag-and-drop. This method is to change the font of the memo part to the same font as the label of the note control part:

Procedure Tform1.memo1dragdrop (Sendersource:tobject;

X,y:integer);

Begin

Memo1.font: = (Source as Tlabel). Font;

End

When you write assignment statements during this event processing, the developer does not know which tag the user will put in, only by referencing the name of the tag (Source as Tlabel), and assigning the tag type to Memo1.tfont. Source contains the name of the user's drag-and-drop control widget, which allows the assignment to occur only if source is a label.

2.2.7 set up non-visual objects

Most of the objects you use in Delphi are the parts that you can see during design and operation, such as edit boxes, buttons, etc., some parts, such as the common dialog box (Common dialog box), are not visible at design time, but can be seen at run time, and some parts, such as timers ( Timer), data source parts, and so on, there are no visual displays during the run of the program, but you can use them in your application.

2.2.7.1 description of a Non-visual object

Here's a simple example of how to build your own Non-visual objects:

You can create your own Temployee Non-visual object in the following ways:

Type

Temployee = Class (TObject);

Name: = string[25];

Title: = string[25];

hourlypayrate:double;

function calculatepayamount:double;

End

In this case, Temployee inherits from TObject and contains three domains and one method. Place the type description you created in the Description section of the library unit and put it together with the form description. In the variable description section of this library unit, a new type of variable is described:

Var

Employee:temployee;

2.2.7.2 object instance with Create method

Temployee is just an object type. An object is not stored in memory unless it is replaced or created by an instance through a call to a constructor. A constructor is a method that configures memory for a new object and points to the new object. This new object is also called an instance of this object type.

To create an instance of an object, you call the Create method, and then the constructor assigns the instance to a variable. If you want to describe an instance of a Temployee type, your program code must call create before you access any of the fields of the object.

Employee: = temployee.create;

The Create method is not described in the Temployee type and inherits from the TObject type. Because Temployee is a subclass of TObject, it can invoke the Create method and creates a Temployee instance. Then assign it to the employee variable. Once you have created an object like this, you can access the Employee object just as you would with other Delphi objects.

2.2.7.3 Undo Object

When you finish working with the object, you should undo it in time to free up the memory occupied by this object. You can undo your object by calling a logoff method, which frees the memory allocated to that object.

Delphi Cancellation method has two: Destroy and free. Delphi recommends using free because it is more secure than destroy, and calling free generates more efficient code.

You can release the exhausted employee object with the following statement:

Employee.free;

As with the Create method, the free method is also inherited from the TObject Temployee. Putting your logoff in the finally part of the Try...finally program module, and putting the object's program code in the Try section is a good habit of programming. This ensures that the memory you allocate for this object is freed, even if an exception event occurs while your program code is using the object. Information about exception handling and try...finally of program modules, as well as examples of creating non-visual objects, are described in the following article.

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.