Analyze the features of the tstringlist Access Object-or reply to the test1234 question

Source: Internet
Author: User
Problem Source: http://www.cnblogs.com/del/archive/2008/04/26/973346.html#1171927
The tlist class is commonly used to store series objects in Delphi. With tobjectlist (in the contnrs unit), the storage object has a better choice, because the objects removed from the tobjectlist list will be released at the same time.

Few people use tstringlist to store objects, but they do not know that using tstringlist to store objects also has the advantages of tlist and tobjectlist.

I want to repeat the concept before continuing the discussion: "Pointer" and "first address" of the object ":
We can find the object through the object pointer, that is, the Pointer Points to the object, the object is just a series of data, "Pointer" is generally directed to the "first address" of this set of data ".
BelowCodeYou can get the "Pointer" and "first address" of the button1 object ":

 
Procedure tform1.button1click (Sender: tobject); begin showmessagefmt ('pointer: % d', [INTEGER (@ button1)]); {14910416} showmessagefmt ('first address: % d ', [INTEGER (button1)]); {15011440} end;

  

Let's look at the method declaration for adding objects to tlist, tobjectlist, and tstringlist:

Tlist: function add (item: pointer): integer; tobjectlist: function add (aobject: tobject): integer; tstringlist: function addobject (const S: string; aobject: tobject ): integer;

  

It can be seen that the tlist is only a pointer. The type added by tobjectlist and tstringlist is an object.
When adding an object, is the data of the entire object added? Of course not. You only need to remember the first address of the object. (The pointer-like method should also be used. I did not study it carefully.) Test the Code:

 
Procedure tform1.button1click (Sender: tobject); var list: tstringlist; begin showmessagefmt ('pointer: % d', [INTEGER (@ button1)]); {14910416} showmessagefmt ('first address: % d', [INTEGER (button1)]); {15011440} List: = tstringlist. create; List. addobject ('btn ', button1); showmessagefmt ('retrieve: % d', [INTEGER (list. objects [0]); {15011440, you can see the same as the above first address} List. free; end;

  

You can use the addobject and insertobject methods of tstringlist to add objects;
Use the objects [] attribute to retrieve objects; Use list [] to retrieve strings. Example:

Procedure tform1.button1click (Sender: tobject); var list: tstringlist; OBJ: tobject; STR: string; begin list: = tstringlist. create; List. addobject ('btn ', button1); {Add} OBJ: = List. objects [0]; {retrieve object} STR: = list [0]; {retrieve string} {use object, there is a premise: we know it belongs to tbutton} showmessage (tbutton (OBJ ). caption); {button1} showmessage (STR); {BTN} List. free; end;

  

Can I add an object pointer? Yes, but it must be converted to a non-type pointer. For example:

 
Procedure tform1.button1click (Sender: tobject); var list: tstringlist; OBJ: tobject; begin list: = tstringlist. create; List. addobject ('btn ', pointer (@ button1); list. addobject ('btn ', pointer (button1); {This can also be} OBJ: = List. objects [0]; showmessage (tbutton (OBJ ). caption); {display: button1} List. free; end;

  

Since pointers can also be added, we can also add other pointers that do not belong to the tobject structure;
If a pointer cannot be added, the structure cannot be added because the structure does not belong to tobject. Example:

Type pmyrec = ^ tmyrec; tmyrec = record S: string; I: integer; end; Procedure tform1.button1click (Sender: tobject); var list: tstringlist; R1, R2: tmyrec; begin list: = tstringlist. create; r1.s: = 'abc'; r1. I: = 123; List. addobject ('rec ', pointer (@ R1); // list. addobject ('rec ', @ R1); {the structure is special. You can do this without converting to a non-type pointer.} R2: = pmyrec (list. objects [0]) ^; showmessagefmt ('% s, % d', [r2.s, r2. I]); {abc, 123} List. free; end;

  

As mentioned above, tstringlist also has some advantages. First, we must acknowledge its disadvantage because it is a list composed of two groups of data and has a disadvantage in efficiency when the data volume is very large; let's talk about its advantages:
The object types retrieved from tlist and tobjectlist are unknown (of course, the author knows). Therefore, only a single type of objects can be stored;
Because tstringlist has two fields, we can use the string field to store the object type, so that tstringlist can store more types of objects at the same time. Example:

Unit unit1; interfaceuses windows, messages, extensions, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) button1: tbutton; Procedure button1click (Sender: tobject); end; var form1: tform1; implementation {$ R *. DFM} type pmyrec = ^ tmyrec; tmyrec = record S: string; I: integer; end; Procedure tform1.button1click (Sender: tobject); var list: tstringlist; R1, R2: tmyrec; STR: string; I: integer; begin list: = tstringlist. create; r1.s: = 'abc'; r1. I: = 123; STR: = 'I am string'; List. addobject ('1', @ R1); {use 1 to indicate the structure tmyrec} List. addobject ('2', Sender); {use 2 to represent tbutton} List. addobject ('3', self); {use 3 to represent tform1} List. addobject ('4', pointer (STR); {use 4 to represent string} For I: = 0 to list. count-1 do begin case strtointdef (list [I], 0) of 1: Begin R2: = pmyrec (list. objects [I]) ^; showmessagefmt ('% s, % d', [r2.s, r2. I]); {abc, 123} end; 2: showmessage (tbutton (list. objects [I]). caption); {button1} 3: showmessage (tform1 (list. objects [I]). text); {form1} 4: showmessage (pchar (list. objects [I]); {I am a string} end; List. free; 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.