Recommended statements in Delphi

Source: Internet
Author: User

{No.1 determining logical type}
VaR B: Boolean;
Begin
B: = Boolean (2); // This is only for debugging. // B: = true;
If B = true then showmessage ('B = true'); // not recommended // insecure
///////
If B then showmessage ('B'); // recommended // brief
End;

VaR B: Boolean;
Begin
If edit1.text = 'is 'then // not recommended // cumbersome
B: = true
Else B: = false;
///////
B: = edit1.text = 'yes'; // suggestion // short
End;

{No. 2 temporary SQL query}
Begin
Querytemp. close;
Querytemp. SQL. Text: = 'select sum (amount) as total from sale table ';
Querytemp. Open; // not recommended // The data is not closed, resulting in a waste of resources
Showmessage (query1.fieldbyname ('Total'). asstring );
/////
Querytemp. SQL. Text: = 'select sum (amount) as total from sale table ';
Querytemp. open;
Showmessage (query1.fieldbyname ('Total'). asstring );
Querytemp. Close; // It is recommended that you disable it after use.
End;

{Number of records retrieved by No. 3}
VaR
Vrecordcount: integer;
Begin
Query1. SQL. Text: = 'select * From table1'; // not recommended // a serious waste of resources, resulting in a lot of unnecessary information
Query1.open;
Vrecordcount: = query1.recordcount;
Query1.close;
/////
Query1. SQL. Text: = 'select count (*) as number of records from table1'; // recommended // fast and effective, only one record is processed
Query1.open;
Vrecordcount: = query1.fieldbyname ('number of records'). asinteger;
Query1.close;

Showmessage (inttostr (vrecordcount ));
End;

{Assignment of field no. 4}
Begin
Table1.edit;
Table1.fieldbyname ('name'). asstring: = edit1.text; // not recommended
Table1.fieldbyname ('date'). asdatetime: = date;
/////
Table1 ['name']: = edit1.text; // recommended // short, good scalability
// Table1.fieldvalues ['name']: = edit1.text; // recommended Borland method. And paramvalues []
Table1 ['date']: = date;
End;

{No. 5 Use self pointer}
Begin
Edit1.parent: = form1; // not recommended // form1 is just a variable // What if no resource is allocated?
///////
Edit1.parent: = self; // recommended
End;

{No. 6 traversing a dataset}
VaR
I: integer;
Begin
Query1.first;
For I: = 0 to query1.recordcount-1 do begin // not recommended // easily affected
Query1.next;
{};
End;
/////
Query1.first;
While not query1.eof do begin // recommended
{}
Query1.next;
End;
End;

{No. 7 generic code using the sender parameter}
Procedure tform1.edit1change (Sender: tobject );
Begin
If edit1.text = ''then // not recommended
Edit1.color: = clred;
///////
If tedit (sender). Text = ''then // It is recommended that you // copy it to editxchange.
Tedit (sender). Color: = clred;
End;

{No. 8 use the default conversion function}
VaR
I: integer;
Begin
I: = strtoint (edit1.text); // not recommended
///////
I: = strtointdef (edit1.text, 0); // recommended // refer to strtofloatdef, strtodatedef... but these are only available in DELPHI6.
End;

{No. 9 traversal array}
VaR
I: integer;
A: array [0 .. 9] of integer;
Begin
For I: = 0 to 9 do // not recommended
A [I]: = I;
///////
For I: = low (A) to high (a) Do // recommended // good scalability
A [I]: = I;
End;

{No. 10 using maxint constants}
Begin
Caption: = copy (edit1.text, 3, length (edit1.text)-3 + 1); // not recommended
///////
Caption: = copy (edit1.text, 3, maxint); // recommended // you can calculate the value once less.
End;

{No. 11 result function pointer}
Function funcname: Boolean;
Begin
Funcname: = true; // not recommended // and placed on the right of the value assignment number cannot be a common variable
///////
Result: = true; // recommended // good scalability
End;

Function funcsum (A: array of integer): integer;
VaR I: integer;
Begin
Result: = 0;
For I: = low (A) to high (a) do
Result: = Result + A [I]; // cannot use funcsum: = funcsum + A [I];
End;

{Code that must be executed in No. 12, use the try... finally... end statement}
VaR
Vstringlist: tstringlist;
Begin
Vstringlist: = tstringlist. Create;
Vstringlist. loadfromfile ('C: \ temp.txt ');
Showmessage (vstringlist. Text );
Vstringlist. Free; // not recommended // resources cannot be released if an exception occurs
///////
Vstringlist: = tstringlist. Create;
Try
Vstringlist. loadfromfile ('C: \ temp.txt ');
Showmessage (vstringlist. Text );
Finally // suggestion // execute the command even if exit appears
Vstringlist. Free;
End;
End;

// Other cases 1
Begin
Screen. cursor: = crhourglass;
Try
{Time-consuming operations}
Finally
Screen. cursor: = crdefault;
End;
End;
// Other cases 2
Begin
Query1.disablecontrols;
Try
{Operation dataset}
Finally
Query1.enablecontrols;
End;
End;

 

Http://www.cnblogs.com/dingjie/articles/193199.html

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.