Skip to content

fernand-o/delphi-better-array

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Overview

TBetterArray<T> is just a workaround to provide instance methods for arrays.

Why?

The generic class TArray<T> was introduced in Deplhi XE2 to give a better approach of manipulating dynamic arrays. Although it was a huge step for delphi, TArray<T> is painful to deal with, any single interation requires lots of code.

Usage

The usage is basically the same of TArray<T>, however there are some examples described bellow.

For the complete list of available methods, please check the source and the examples at the test unit.

Simple types

var
  People: TBetterArray<string>;
begin
  People := ['Fernando', 'John', 'Paul'];
  
  People.Count;             // 3
  People.Join('-');         // 'Fernando-John-Paul'
  People.First;             // 'Fernando'  
  People.Contains('Trump'); // False
  People.Reverse;           // ['Paul', 'John', 'Fernando']  
  People.Add('Bill');       // ['Paul', 'John', 'Fernando', 'Bill']
end;
  

Complex types

TCoffee = class
  Name: string;  
  Origin: string;
  Strength: Integer;  
  constructor Create(Name, Origin: string; Strength: Integer);
end;

...

var 
  Coffees: TBetterArray<TCoffee>;
begin
  Coffees := [
    TCoffee.Create('Arabic', 'Egypt', 5),  
    TCoffee.Create('Turkish', 'Turkey', 8),
    TCoffee.Create('Capuccino', 'Italy', 2)
  ];  
end;  

Sorting

var
  StrengthComparison: TComparison<TCoffee>;
begin
  StrengthComparison := 
    function(const Left, Right: TCoffee): Integer
    begin
      Result := Left.Strength - Right.Strength;
    end;  
    
  Coffees.Sort(StrengthComparison);
  Coffees.First.Name; // 'Capuccino'
  Coffees.Last.Name;  // 'Turkish'  

Retrieving values

  Coffees[1];      // TCoffee instance
  Coffees[5];      // Error
  Coffees.Get(1);  // TCoffee instance
  Coffees.Get(5);  // nil

About

An easier way of handling arrays in Delphi

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages