DelphiBasics
Slice
Function
Creates a slice of an array as an Open Array parameter System unit
 function Slice(SourceArray array; Count Integer):array;
Description
The Slice function creates a sub-array from the first Count elements of an array, SourceArray.
 
It can ONLY be used as an argument for an 'Open array' parameter of a procedure or function.
 
An 'Open array' parameter is one that has an unknown number of array elements at compile time.
 
This allows for a routine to work with variable sized arrays.
Related commands
ArrayA data type holding indexable collections of data
CopyCreate a copy of part of a string or an array
 Download this web site as a Windows program.




 
Example code : Passing an array slice to a procedure
var
  i : Integer;
  Source : array[0..4] of Integer;

begin
  // Create the source array with 0..4 values for elements 0..4
  for i := 0 to 4 do
    Source[i] := i;

  // Use the Slice command to pass just the first 3 elements of Source as
  // an open array to the ShowSlice procedure below.
  ShowSlice(Slice(Source, 3));
end;

// Show an array of unknown size - it is passed as an 'Open' array
procedure TForm1.ShowSlice(SubArray : array of Integer);
var
  i : Integer;

begin
  // Show every element of this array
  for i := 0 to Length(SubArray)-1 do
    ShowMessage('SubArray['+IntToStr(i)+'] : '+ IntToStr(SubArray[i]));
end;
Show full unit code
  SubArray[0] : 0
  SubArray[1] : 1
  SubArray[2] : 2
 
Delphi Programming © Neil Moffatt . All rights reserved.  |  Home Page