1. Computing

How to delete an item from a dynamic (string) array

By

If you have a dynamic array of strings and want to delete one string from it, you can use the DeleteArrayItem procedure. Here's a sample usage

var
myArray: TStringArray;
...//delete fifth element
DeleteArrayItem(myArray, 5) ;

~~~~~~~~~~~~~~~~~~~~~~~~~
type
   TStringArray = array of string;

procedure DeleteArrayItem(var X: TStringArray; const Index: Integer) ;
begin
   if Index > High(X) then Exit;
   if Index < Low(X) then Exit;
   if Index = High(X) then
   begin
     SetLength(X, Length(X) - 1) ;
     Exit;
   end;
   Finalize(X[Index]) ;
   System.Move(X[Index +1], X[Index],(Length(X) - Index -1) * SizeOf(string) + 1) ;
   SetLength(X, Length(X) - 1) ;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~

Delphi tips navigator:
» How to execute a method (procedure/function) by name
« How to draw transparent text on Windows Desktop

©2014 About.com. All rights reserved.