Use Sender parameters in BCB for code reuse

Source: Internet
Author: User

One of the features of object-oriented programming tools is to improve code reusability. BCB of Boran can certainly implement this function. We all know that in BCB, most program code directly or indirectly corresponds to an event. This program is called an event processing handle, which is actually a process. From the project of an application to the window, components, and programs, BCB emphasizes the reusability of each layer in its development process. It can take full advantage of the Code that has been written to reduce the workload, it will make your program more beautiful. The sharing between code segments is related to the control in which the event occurs. You need to handle the event based on the control type, and then use the Sender parameter.

The beginning of each function is tangible, such:

Void _ fastcall Tform1: Button1Click (TObject * Sender)

The Sender is a Tobject type parameter, which tells BCB which control receives the event and calls the corresponding processing process. We can write a single event processing handle and use the Sender parameter in combination with the if statement or case statement to process multiple components. In Delphi, we can use IS to test the Sender type, or use AS for type conversion. We only use dynamic_cast to perform the above two tasks. The following describes the usage of dynamic_cast.

Dynamic_cast can forcibly convert an object to another class. If the type conversion is successful, a pointer with a value of 0 is returned. If the conversion fails, an exception processing information is thrown: Bad_cast. However, you can rest assured that the system will not crash, so you can use it with confidence. Program:

Dynamic_cast <T> (ptr)

The T parameter must be a pointer, void, or defined class, And the ptr parameter must be a pointer or a reference ). If the T type is void, ptr is a member that can access the bottom class. Of course, such a class cannot be a base class.

1. Make judgment

We use dynamic_case to test the Sender so as to find the processing handle or component type that calls this event. For example, the processing handle of the Click event in the window editing box and tag points to the xxx function in the window. The editing box and tag have different reactions to the Click event:

Void _ fastcall TForm1: xxx (Tobject * Sender)

{

If (dynamic_cast <TEdit *> (Sender)

Showmessage ("This is a editbox ");

If (dynamic_cast <TLabel *> (Sender)

Showmessage ("This is a label ");

}

2. Force type conversion

Convert several sub-classes that inherit the same parent class to this parent class. For example, if the window contains a TEdit control and a TMemo control, they all inherit from the TCustomEdit class. If you want to provide the same processing for an event of the two, you can point both event handles to the custom function yyy:

Void _ fastcall TForm1: yyy (Tobject * Sender)

{

Dynamic_cast <TCustomEdit *> (Sender). text = "This is some demo text ";

}

Here, the TEdit class and TMemo class are forcibly converted to the TCustomEdit class, and then the attributes of the parent class are assigned a value.

The Sender parameter can be used to process multiple types of components through a single function segment, which truly reflects the reusability of BCB object-oriented.

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.