![]() |
![]() |
|||||||||||
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
|||||||
What's Wrong With This Code? Volume #7Phantom UpdatesIn 1998, I started working on a medium sized database program using C++Builder 3 and MS SQL Server 6.5. When C++Builder 4 came out in 1999, we decided to postpone upgrading because we were about to ship the first version of the product. After releasing version 1.0 of our system, we started thinking about upgrading to C++Builder 4. Our program is a front end for the database. The primary form in our program contains a couple of panels. The panels are separated by a splitter control. Each panel displays data from the database. The form also contains a toolbar with buttons on it for editing and updating the data. Figure 1 shows a stripped down version of the main form. ![]() Figure 1. The primary form in my database applicationWhen the user presses the update button on the toolbar, an OnClick handler fires. Over the years, I have learned that it is not wise to place tons of code in an OnClick handler, because doing so can make it difficult to maintain a project. With this in mind, I added a private method to my class called Update. The code looks like this: //----------------------------------------------------------------- // MAINFORM.H Header file // class TForm1 : public TForm { __published: // ... TSpeedButton *btnUpdate; void __fastcall btnUpdateClick(TObject *Sender); private: void __fastcall Update(); public: __fastcall TForm1(TComponent* Owner); }; //----------------------------------------------------------------- //----------------------------------------------------------------- // MAINFORM.CPP Source file // __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //----------------------------------------------------------------- void __fastcall TForm1::btnUpdateClick(TObject *Sender) { Update(); } //----------------------------------------------------------------- void __fastcall TForm1::Update() { // update database // ... ShowMessage("Database Updated!"); } //----------------------------------------------------------------- I have stripped out all unecessary code to keep things simple. When the user presses the update button, the OnClick handler simply calls the Update member function. Update interacts with a business object to write changes back to the SQL database. This code worked fine in C++Builder 3, but we noticed a bit a strangeness when we moved to C++Builder 4. The update button still works in C++Builder 4, which is good. However, when the user moves the splitter bar, the program acts as if the user had clicked the update button. Moving the splitter bar updates the database! How can that be? The splitter bar did not trigger the update when we were compiling with C++Builder 3. Is the splitter bar in C++Builder 4 completely whacked? Did the DFM file get messed up when we moved from C++Builder 3 to C++Builder 4? See if you can determine why moving the splitter bar triggers an update when the project is compiled with C++Builder 4. You can download the project using the links at the bottom. Answer
| ||||||||||||
All rights reserved. |