Several problems needing attention in C + + Buidler 6

Source: Internet
Author: User
Tags goto variables

 First, the introduction

C + + Buidler and Dephi are examples of rad, making it possible to develop programs quickly, efficiently, and easily. However, in the process of using C + + Buidler, it is found that not only familiar with C + + Buidler VCL components can successfully complete the program writing, but also need to pay attention to some easily overlooked details. This article lists some problems encountered, and provides solutions for reference.

  Ii. Specific problems and solutions

1. When using SQL statements to manipulate a database, if a variable appears in the statement, the same variable cannot be reused

For example: To access the data table T1 and T2 in accordance with the conditions t1.t1_f1= "01" and T2.t2_f1= "01" records, according to conventional thinking, simply set a variable p in the SQL statement, respectively, to determine whether the T1.T1_F1 and t2.t2_f1 value is "01", The BCB statement is as follows: if (adoquery1->active)

{Adoquery1->close ();

Adoquery1->sql->clear (); }

adoquery1->sql->text= "SELECT * from T1, T2 where t1.t1_f1=:p and t2.t2_f1=:p";

adoquery1->parameters->paramvalues["P"]= "01";

Adoquery1->open ();

However, after this code is run, the result of the query is empty. (confirmed during the access Access2003) by tracing the variable p in the SQL statement, the second call to P is found, the BCB does not assign a value to P, that is, p is null, so the expected query result is not obtained. The correct parameter setting should be: adoquery1->sql->text= "SELECT * from T1, T2 where t1.t1_f1=:p 1 and t2.t2_f1=:p 2";

adoquery1->parameters->paramvalues["P1"]= "01";

adoquery1->parameters->paramvalues["P2"]= "01";

As you can see from the above, BCB uses only once for each assignment variable that appears in the SQL statement. Even if you need to assign the same value to a variable that appears two times in an SQL statement, apply two variables and assign the values separately.

2. Use of goto statements

In the goto statement of C + + Builder, if the reserved word goto has an initialization statement with a local variable between the target identity: Goto endmark;//Endmark as the target identity

int a=1;

......

Endmark:;

If you run the goto statement, the error will cause the use of goto statements is inconvenient, but if the Goto and the target logo with a pair of curly bracket, you can run smoothly: goto Endmark;

{int a=1;

......  }

Endmark:;

C + + Builder will {...} The initialization of the variable in the local variable is understood to be initialization, and the initialization of the local variable does not affect the statement outside the {}. Therefore, you can eliminate the restriction that C + + Builder uses for GOTO statements by appropriately using curly braces.

3. The impact of creating windows dynamically

The advantage of dynamically creating Windows is that you can use the Delete command to free up memory when the window is not needed. However, because Windows are created and released in memory, there are some problems. For example, the establishment of FORM1, Form2 and Form3,form1 in the new application is provided with Button1, Edit1 and Memo1,form2, Button21, Button22, FORM3 is provided with Button3 and EDIT3, Memo3.form1->button1 Click events in the dynamic Create window Form2, and set the FORM2->BUTTON21 Click event Display window FORM3, And Form1, Form2, Form3 each have a Edit1 and Memo1, and now try to FORM3 and Button3 by clicking on the Edit3 button, respectively, to Memo3 the Form2 and Edit2 components, Then, by clicking on the Button22 button on the Form2, the text in Edit2 and Memo2 is assigned to Form1.

Functions in Form1: void __fastcall tform1::button1click (tobject *sender)

{TForm2 * form2_n=new TForm2 (FORM1);

Form2_n->showmodal ();

Delete Form2_n; }

function void __fastcall Tform2_n::button21click in Form2_n (TObject *sender)

{Form3->showmodal ();

}

void __fastcall Tform2_n::button22click (tobject *sender)

{form1->edit1->text=form2->edit2->text;

form1->memo1->text=form2->memo2->text;

This->close (); }

Functions in Form3: void __fastcall tform3::button3click (tobject *sender)

{form2->edit2->text=form3->edit3->text;

form2->memo2->text=form3->memo3->text;

This->close (); }

The tests found that the text in Edit3 and Memo3 on FORM3 could not be assigned to Form2_n Edit2 and Memo2, but the text in Form2_n and Edit2 on Memo2 could be assigned to Form1 and Edit1 on Memo1.

If you modify the execution statement of the Button1 click event in Form1, the Create Form2_n is created to create the Form2, and the form2_n that appears elsewhere is changed to Form2:void __fastcall Tform1::button1click (TObject *sender)

{form2=new TForm2 (FORM1);

Form2->showmodal ();

Delete Form2; }

In this way, the words of EDIT3 and Memo3 in the FORM3 can be assigned to Form2 Edit2 and Memo2.

When you create Windows Form2_n and Form2 that belong to the TForm2 class, the difference is that when you create the Form2_n (when you click on Button1), you need to request memory creation from the system. The Form2_n creation process "TForm2 * form2_n=new TForm2 (FORM1)" contains the two actions for defining the new Form Class form2_n and initializing it, Form2_n requested memory space during the definition process While Form2 is a form defined in Design-time, the action to request memory space is completed when the program is initialized. The time that two forms apply for memory affects their acceptance of the values passed.

  Third, the conclusion

Some implied restrictions in C + + Builder are not intuitive, such as the creation of a dynamic window to the window to create variables between the variable assignment, and the creation of a dynamic window in Delphi (Form1:=tform1.create (self);) There is no such problem. Therefore, if encountered in the C + + Builder can not be solved by the general method of some problems, but also need to be more familiar with C + + Builder, meticulous analysis, more attempts to explore solutions, accumulated some experience, you can improve the coding efficiency.

Related 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.