Implementation of code reuse with sender parameters in BCB

Source: Internet
Author: User
Tags exception handling

One of the characteristics of object-oriented programming tools is to improve code reusability (reuse), Baolan BCB Of course can achieve this function. As we all know, in BCB, most program code directly or indirectly corresponds to an event, this program is called the event handling handle, it is actually a process. From application Engineering to Windows, components, and programs, BCB emphasizes the reusability of each level of the development process, making full use of the code that has been written to reduce the amount of work that can be done and make your program more graceful. The sharing between code snippets is related to the control in which the event occurred, and it needs to be handled according to the control type, when the sender parameter is used.

The beginning of each function is visible as:

void _fastcall Tform1::button1click (tobject *sender)

The sender is a tobject type parameter that tells BCB which control receives the event and invokes the appropriate process. We can write a single event-handling handle, with sender parameters and if statements or case statements, to handle multiple components. In Delphi can use is to test sender type, or use as for type conversion, BCB we only use dynamic_cast to do the above two work, the following dynamic_cast usage explain.

Dynamic_cast can force an object to be converted to another class, where the so-called coercion still has its limitations, that is, if the class does not turn, the system will not convert. Returns a pointer that has a value of 0 if the type conversion cannot succeed. If the parameter T is a reference type, and the conversion of the class fails, the system throws an exception handling message: Bad_cast. But you are assured that this will not cause the system to panic, so you can safely use. Its program:

dynamic_cast <T> (PTR)

The T parameter must be a pointer, a void*, or a class that has already been defined, and the PTR parameter has to be a pointer (pointer) or a reference (reference). If the type of T is void*, then PTR is one that can access any member in the bottom class, and of course such a class will not be a base class.

1. To judge

We use Dynamic_case to test the sender to find the type of handler or component that invokes this event. For example, we will be in the window of the edit box and the label of the click event handle all point to the window of the XXX function (in fact, you just first the click of a control event named XXX, and in which to write the shared code, the other control click events Point to XXX on the line), The edit box and label in this example will react differently to the Click event, as follows:

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");
}

Of course, if multiple similar components, just want to share an event, it is much simpler than this. For example, if you have a lot of edit boxes that you want to empty before you type in an item, you just have to write a OnEnter event:

void __fastcall TForm1::Edit1Enter(TObject *Sender)
{
  TEdit *Edittemp=(TEdit*)(Sender);//把不同的编辑框统一起来
  Edittemp->Text="";
}

Other edit components of the OnEnter event point to Edit1enter, so that's OK, try, is not the mouse in the edit box to empty a bit of J in fact, here is only a different edit box (sender clear is that an edit box) unified, use a common event to deal with. You must pay attention to this when you share the same event with the same component.

2. To force type conversions

Casts several subclasses that inherit the same parent class to the parent class. If you have a Tedit class control and a TMemo control in the window that actually inherit from the Tcustomedit class, you can point the event handle of both to a custom function yyy if you want to provide the same treatment for one of the two events. We are still here in the OnEnter event (you can definitely do it in other events):

void __fastcall TForm1::yyy(TObject *Sender)
{
   dynamic_cast<TCustomEdit &>(*Sender).Text="This is some demo text";
}
或以下的格式:
void __fastcall TForm1::yyy(TObject *Sender)
{
   dynamic_cast<TCustomEdit* >(Sender)->Text="This is some demo text";
}

Notice the difference between the two, which is actually "." The difference between "->" and a careful taste, you will be clear.

All two of the above programs are forced to convert both the Tedit class and the TMemo class to the Tcustomedit class, and then assign values to the properties of their parent class.

The sender parameter can be used to deal with many kinds of components through a single function segment, which truly embodies the BCB of object-oriented reuse.

(The above program XP system, BCB6 compiled through)

The original article written when there are some mistakes, here to say sorry to everyone, but also to thank some enthusiastic Netizen's correction j

But at the same time I solemnly declare that this article except CSDN and Ccrun have not agreed to any other person or website use. But "BCB Master Advanced (10) with sender parameters to achieve code reuse NXYC_TWZ (original)", I was wrong, and every word is not bad, I hope you can openly to me and care about my friends to apologize, otherwise I will reserve to csdn to complain about your rights l

------------------

Author: ch_builder

Mailbox: ch_builder@163.com

qq:116001522

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.