I need to store a temporary list of records and was thinking that a TList
would be a good way to do this? However I am unsure how to do this with a TList
and was wondering if this is the best was and also if anyone has any examples of how to do this?
相关问题
- Is there a Delphi 5 component that can handle .png
- Is there a way to install Delphi 2010 on Windows 2
- Is TWebBrowser dependant on IE version?
- iOS objective-c object: When to use release and wh
- DBGrid - How to set an individual background color
相关文章
- Best way to implement MVVM bindings (View <-> V
- Windows EventLog: How fast are operations with it?
- How to force Delphi compiler to display all hints
- Coloring cell background on firemonkey stringgrid
- Systematically applying a function to all fields o
- HelpInsight documentation in Delphi 2007
- How do I modify a record in erlang?
- Can RTTI interrogate types from project code at de
First, if you want to combine a classic TList with Records, you will need to:
Combining Lists with Records requires so much "pointers-and-heap-management" work that such a technique would be only within the capabilities of an expert.
Alternatives to what you have asked for that still use something called "TList", include using a generics.collections style TList, with Record types, which would have all the benefits of TList, but would require you to basically do a lot of entire-record-copies to get data into it.
The most idiomatic Delphi ways to do what you ask are to either:
use a TList or TObjectList with a Class Types instead of a record. Usually you end up subclassing either TList or TObjectList in this case.
Use a dynamic Array of Record Types, but be aware that it's harder to sort an Array type, and that expanding an array type at runtime isn't as speedy as it is with a TList.
Use generics.Collections TList with your classes. This lets you avoid subclassing TList or TObjectList each time you want to use a list with a different class.
A code sample showing Dynamic arrays:
Now for some background information on why TList is not easy to use with Record types:
TList is better suited for use with Class types, because a variable of type 'TMyClass', where 'type TMyClass = class .... end;' can be easily "referred to" as a pointer value, which is what TList holds.
Variables of type Record are value-Types in Delphi, whereas class values are implicitly by-reference values. You can think of by-reference values as stealth-pointers. You don't have to dereference them to get at their contents, but when you add it to a TList, you're actually just adding a pointer to the TList, not making a copy or allocating any new memory.
The answer by Remy shows you literally you how to do exactly what you want, and I am writing my answer only because I want to warn you about the details of what you are asking, and suggest that you consider alternatives too.
It all depends on the type of data you want to store.
You might consider using
TCollection
andTCollectionItem
.Here is (edited) code from a working unit, in which I used
TCollection
to read a list of report definitions from a folder. Each report consisted of a sort of template and an SQL statement which had to be stored together with a file name.Since it is edited, and uses some of my own units (TedlFolderRtns reads files into an internal list, to name but one), the example is simple enough to be useful. With a few replace all, you can adapt to whatever your need.
Look up TCollection in the help, you can do a lot with it. And it keeps your code handling nicely grouped together in a class-like structure.
Use as :
The easiest way is to create your own descendant of
TList
. Here's a quick sample console app to demonstrate:This eliminates a couple of things:
As Remy and Warren both said, it's a little more work because you have to allocate the memory when you add new records.
You can take a look at our TDynArray wrapper. It's defined in an Open Source unit, working from Delphi 6 up to XE.
With
TDynArray
, you can access any dynamic array (likeTIntegerDynArray = array of integer
orTRecordDynArray = array of TMyRecord
) usingTList
-like properties and methods, e.g.Count, Add, Insert, Delete, Clear, IndexOf, Find, Sort
and some new methods likeLoadFromStream, SaveToStream, LoadFrom
andSaveTo
which allow fast binary serialization of any dynamic array, even containing strings or records - aCreateOrderedIndex
method is also available to create individual index according to the dynamic array content. You can also serialize the array content into JSON, if you wish.Slice, Reverse
orCopy
methods are also available.It will handle a dynamic array of records, and even records within records, with strings or other dynamic arrays inside.
When using an external
Count
variable, you can speed up a lot the adding of elements in the referred dynamic array.There is also a
TDynArrayHashed
class, which allow internal hashing of a dynamic array content. It's very fast and able to hash any kind of data (there are standard hashers for strings, but you can supply your own - even the hash function can be customized).Note that
TDynArray
andTDynArrayHashed
are just wrappers around an existing dynamic array variable. You can therefore initialize aTDynArray
wrapper on need, to access more efficiently any native Delphi dynamic array.You can use TList for that, eg:
We've just run into a similar issue here with a generic list of records. Hope the following psuedo code helps.