The code is reused in the BCB.

zhaozj2021-02-16  45

One of the features of object-oriented programming tools is to increase the code reuse (Reuse), and Balan's BCB can of course achieve this. We all know that in BCB, most of the program code corresponds directly or indirectly, this program is called an event handle, which is actually a process. From the application's engineering to windows, components, and programs, BCB emphasizes the reuse of each level during its development process, which can make full use of the written code to reduce workload, which will make your program beautiful. Both the sharing between the code segment is related to the control of the event, and the corresponding processing needs to be made according to the type of control. At this time, the sender parameter is used.

The beginning of each function is like:

Void _fastcall tform1 :: button1click (TOBJECT * Sender)

The Sender is a TOBJECT type parameter, which tells BCB which control receives this event and calls the corresponding process. We can write a single event handle handle to handle multiple components via Sender parameters and IF statements or CASE statements. In Delphi, you can use IS to test the Sender type, or use AS to convert, BCB We only use Dynamic_Cast to perform the above two work, below the Dynamic_cast's usage.

Dynamic_cast can enforce some object to another class. If the type conversion is successful, it returns a value of 0. If it fails, it will leave an exception handling information: BAD_CAST, but you can't cause the system to crash, so you can use it with confidence. Program:

Dynamic_cast (PTR)

T parameters must be a pointer, void, or class that has been defined, and the PTR parameter must be a pointer or a reference (Reference). If the Type of T is Void, PTR is a member that can access any member in the lower class, of course, such a class will not be an underlying class.

1. Judgment

We use Dynamic_Case to test Sender to find the type of handle or component that calls this event. For example, the handle of the window editing box and the label's handle's handle is pointing to the XXX function, edit box, and tags have different reactions to the Click event:

Void _fastcall tform1 :: xxx (TOBJECT * Sender)

{

IF (DynamiC_Cast (Sender)

ShowMessage ("this is a editbox");

IF (Dynamic_cast (Sender)

ShowMessage ("this is a label");

}

2. Mandatory type conversion

Mandatory for several subclasses of the same parent class into the parent class. If there is a TEDit class control and a TMEMO control in the window, they actually inherited in the TCUStomedit class. If you want to provide the same process for one of the two, you can point the two event handles to custom functions YYY. :

Void _fastcall tform1 :: yyy (TOBJECT * Sender)

{

Dynamic_cast (sender) .Text = "this is some demo text";

}

Here, the TEDIT class and TMEMO classes are first converted to the TCUStomedit class, and the properties of their parent class are assigned.

Using the Sender parameter can process multiple class components through a single function segment, truly embodies the object-oriented reuse of BCB.

转载请注明原文地址:https://www.9cbs.com/read-26029.html

New Post(0)