Tlist, tstringlist, and tobjectlist of Delphi

Source: Internet
Author: User

Unlike the tlist class, methods like add, remove, indexof, and insert of the tobjectlist class all need to pass the tobject object as a parameter. Due to the strong type check during the compilation period, this makes tobjectlist more suitable for saving objects than tlist. In addition, the tobjectlist object has the ownsobjects attribute. If it is set to true (default value), different from the tlist class, the tobjectlist object will destroy any objects deleted from the list. Whether you call the delete, remove, clear method or release the tobjectlist object, all objects in the list will be destroyed. With the tobjectlist class, we no longer need to use loops to release objects. This avoids Memory leakage caused by forgetting to release objects in the linked list when releasing Linked List objects. In addition, it should be noted that the ownsobjects attribute does not affect the extract method. The extract method of tobjectlist is similar to tlist, but only removes the object reference from the list without destroying the object.

In the tstringlist, the strings are stored in one row and one row. Tstringlist. Text returns all strings. If the first, second, and third rows are \ 'aa \ ', \ 'bb \', and \ 'CC, then the text returns "\ 'aa \ '+ #13 #10 + \ 'bb \' + #13 #10 + \ 'CC \ '+ #13 #10" (No including double quotation marks ). All strings are connected by tstringlist using the carriage return and Line Break (#13 #10. If values are assigned to the text in turn, the text will be automatically split and stored in the tstringlist. This fully reflects a very practical value of tstringlist: it allows us to process strings row by row. If we want to operate row 4th, we only need to operate tstringlist [3]. I believe you will ask, why can tstringlist be used as an array? In fact, when we write tstringlist [3], we write tstringlist. Strings [3]. Strings is a default attribute of tstringlist. The default attribute of array is used in this way. If you want to use this function when writing a class, you can refer to the following method:

Property aproperty [I: integer] Read *** write ***;

Default ;.

Strings is a read/write attribute. That is to say, you can not only obtain the content of line N, but also change the content of line N. Therefore, we need to know the total number of s t r I n g lines in the tstringlist. The tstringlist attribute count can meet our needs.

As mentioned above, text is an attribute that returns all strings. When assigning a value to text, tstringlist can automatically divide text into one row and store it in tstringlist (of course, tstringlist is not completely stored in this way, for detailed procedures, we recommend that you check tstringlist and tstrings code ). In this way, the string returned by strings is

There are carriage return and line feed. However, if the string we assign a value to strings contains carriage return and line feed, what will happen? In this case, strings splits the string into several lines and inserts it into the original position. If tstringlist only has these functions, I don't have to talk about it. I mean, tstringlist can make us arbitrary.

To insert or delete a row. This requires the tstringlist method.

Function add (const S: string): integer;

Procedure append (const S: string );

The add method adds a line of string to the end of the tstringlist (here and below we assume that the input string does not have a carriage return or line feed, otherwise the string will be split ). Parameter s indicates the content of the string to be inserted. The return value of add indicates the position of the new string in the tstringlist-that is, the position of the last row, that is, the position of the new count minus one.

The only difference between the append method and add method is that there is no return value.

Procedure insert (Index: integer; const S: string );

The insert method inserts a line of strings into the tstringlist. In insert, We can freely select the position where the string is inserted. The parameter S indicates the content of the string to be inserted, and the index indicates the position to be inserted.

Procedure Delete (Index: integer );

The delete method deletes a row of strings. We can also choose to delete any row of strings as needed. The index parameter indicates the position of the string to be deleted.

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.