Wednesday, 15 April 2015

delphi - How do I find the index of a record in a TList? -


I am trying to increase the number of times I use each product code in my database. Being a problem, I do not know all the code (or more can be added).

I can use a TList with'm assuming that I have value added to it (Utpadkod and counting) something like this (I like to have a list & lt treatment Trying;;> C # to etc.)

  process myProc type TProductCodeCount = record ProductCode: string; Count: integer; End; Var Product Codes: TLIST & LT; Tputcodecountain & gt;; NewProductCodeCount: TProductCodeCount; CurrProductCodeCount: TProductCodeCount; CurrentProductCode: string; ProductCodeIndex: Integer; Start ProductCode: = TList & lt; TProductCodeCount>. Create; // Set your data // Turn on the loop through my data set CurrentProductCode: = // value from the database; ProductCodeIndex: = productCodes.indexOf (Current ProductCode); If productCodeIndex = -1 then the new ProductCodeCountor. ProductCode starts: = currentProductCode; NewProductCodeCount.count: = 1; ProductCodes.Add (newProductCodeCount) and start currProductCodeCount: = productCodes.Extract (productCodeIndex); CurrProductCodeCount.count: = currProductCodeCount.count + 1 ProductCode Delete (productCodeIndex); ProductCodes.Add (currProductCodeCount); End of end; // Start the loop through my TList / do something with each product and edit and end the end;  

Here are two big things wrong.

  1. me how to do that than work index is sure to work (if it is possible a TList of a record type
  2. Update list items unsightly

How can I compare? Or there is a better way to accomplish this

if you have TList & lt will pass the custom comparer can use ;? T & gt; Works

  Type TProductCodeCountComparer = class (TComparer & LT; TProductCodeCount & gt;) in the public function (const Left, Right: TProductCodeCount) :. Integer; override; end; function TProductCodeCountComparerkCompare (const Left, right: TProductCodeCount): integer; begin Result: = comparison (Baankutpadkod, right. Prodktkod); end; var Tulnar: Aisiompiyrr; Teepeedatakodkount & gt; Tulnar = Tiprodktkodcoint Comprer. Create; Utpadkod = Tielaisti & lt; Tiprodktkodkount & gt ;. (c omparer);  

Tulnakrta in as luggage can also create

  productCodes: = TList & lt; TProductCodeCount>. Create (Tikanpprr & lt; TProductCodeCount & gt ;. Constrkt (function (const left, right: TProductCodeCount): begins int result = compare (Baankutpadkod, right. Utpadkod); end));  

You can use the edit record in place TList & LT; T & gt; .List is the property that gives you direct access to TList & lt built-in, T & gt; The array enables modifying the record directly in the list:

  var productCodes: TList & lt; TProductCodeCount> .... inc (productcodes.list [productCodeIndex]. Count);  

TList & LT; T & gt; .List The property was introduced in XE3, class extension following record modifications for the first Delphi versions:

  TXList & lt; T & gt; = Square (TList & lt; T & gt;) Protected Type PT = ^ T; Function GetPItem (Index: Integer): PT; Public Property PItems [Index: Integer]: Read GetPItem PT; End; Function TXList & lt; T & gt; .GetPItem (index: integer): PT; Start the result: = @fights [index]; End; Var Productcode: TXList & lt; TProductCodeCount> .... inc (productCodes.PItems [productCodeIndex]. Count);  

No comments:

Post a Comment