Differences between assigned (BTN) and assigned (@ BTN)-Reply to "sunnet"

Source: Internet
Author: User
Problem Source: http://www.cnblogs.com/del/archive/2008/05/21/1101470.html#1204455
Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) button1: tbutton; button2: tbutton; procedure button1click (Sender: tobject); Procedure button2click (Sender: tobject); end; var form1: tform1; implementation {$ R *. DFM} var btnglobal: tbutton; {define a global object (tbutton) variable} procedure tform1.button1click (Sender: tobject); var btnlocal: tbutton; {define a local object (tbutton) variable} begin {"object" itself is a pointer, which points to the location where the object data exists;} {"@ object" is a pointer to the object, which points to the "object" variable itself, if the "object" variable exists, it must exist} {the following four rows Code The address where the pointer can be obtained. If the result is 0, the object has not been assigned a value, that is, nil} showmessage (inttostr (INTEGER (btnglobal); {0 ;} showmessage (inttostr (INTEGER (@ btnglobal); {4607236} showmessage (inttostr (INTEGER (btnlocal ))); {0} showmessage (inttostr (INTEGER (@ btnlocal); {1242560} {assigned (object) returns false} showmessage (booltostr (assigned (btnglobal), true); {false} showmessage (booltostr (assigned (btnlocal), true )); {false} {because the object variable already exists, the "@ object" pointing to the object variable will not be nil} showmessage (booltostr (assigned (@ btnglobal), true )); {true} showmessage (booltostr (assigned (@ btnlocal), true); {true} {after the value is assigned ...} btnglobal: = tbutton (sender); btnlocal: = tbutton (sender); showmessage (booltostr (assigned (btnglobal), true )); {true} showmessage (booltostr (assigned (btnlocal), true); {true} showmessage (booltostr (assigned (@ btnglobal), true )); {true} showmessage (booltostr (assigned (@ btnlocal), true); {true} end; {Because delphi will give some types of local variables a useless default value, so the following situation may occur} procedure tform1.button2click (Sender: tobject); var BTN: tbutton; begin showmessage (inttostr (INTEGER (BTN); {15012064; if you do not comment out the following sentence, it will be 0} // showmessage (inttostr (INTEGER (@ BTN); {1242560} {my conclusion is: it is unreliable to use assigned to judge a local variable that has not been initialized. This should be considered a bug} {solution: IfProgram To determine this, initialize the local variable to be null first, for example:} BTN: = nil; showmessage (booltostr (assigned (BTN), true )); {false} BTN: = tbutton (sender); showmessage (booltostr (assigned (BTN), true); {true} end; end.
  
   
 

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.