Home  |  Delphi .net Home  |  System.Array  |  LastIndexOf Method
  
LastIndexOf  
Method  
Tries to find the last occurence of an object in a single dimensional array
Array Class
System NameSpace
NotCF1.  Function LastIndexOf ( TheArray : System.Array; Value : Object ) : Integer; Static;
NotCF2.  Function LastIndexOf ( TheArray : System.Array; Value : Object; BackFromIndex : Integer ) : Integer; Static;
NotCF3.  Function LastIndexOf ( TheArray : System.Array; Value : Object; BackFromIndex : Integer; Count : Integer ) : Integer; Static;
CF : Methods with this mark are Compact Framework Compatible
Description
TheArray single dimensional array is searched for last occurence of the given Value object. If found, the element index of the object is returned, otherwise -1 is returned.
 
The array is scanned from the end to start unles the optional BackFromIndex and Count parameters are provided to give a start and range of elements as appropriate.
Notes
Do not forget that the scan moves backwards!
Microsoft MSDN Links
System
System.Array
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

 Buy Proxies at
 Buyproxies.io
 
 
 
A simple example
program Project1;
{$APPTYPE CONSOLE}

var
  myArray : System.Array;
  i       : Integer;

begin
  // Create a 4 element single dimension array of strings
  myArray := System.Array.CreateInstance(TypeOf(String), 4);

  // Fill the array with values
  myArray.SetValue('ABC', 0);
  myArray.SetValue('DEF', 1);
  myArray.SetValue('ABC', 2);
  myArray.SetValue('GHI', 3);

  // Try to find the position of 'ABC' in the array
  i := System.Array.LastIndexOf(myArray, 'ABC');
  Console.WriteLine('Last position of ''ABC'' = {0}', i.ToString);

  // Try to find the position of 'abc' in the array
  i := System.Array.LastIndexOf(myArray, 'abc');
  Console.WriteLine('Last position of ''abc'' = {0}', i.ToString);

  Console.ReadLine;
end.
   Last position of 'ABC' = 2
   Last position of 'abc' = -1
Using the optional BackFromIndex and Count parameters
program Project1;
{$APPTYPE CONSOLE}

var
  myArray : System.Array;
  i       : Integer;

begin
  // Create a 4 element single dimension array of strings
  myArray := System.Array.CreateInstance(TypeOf(String), 6);

  // Fill the array with values
  myArray.SetValue('ABC', 0);
  myArray.SetValue('DEF', 1);
  myArray.SetValue('ABC', 2);
  myArray.SetValue('GHI', 3);
  myArray.SetValue('DEF', 4);
  myArray.SetValue('GHI', 5);

  // Try to find the position of 'ABC' in elements 2 down to 0
  i := System.Array.LastIndexOf(myArray, 'ABC', 2, 3);
  Console.WriteLine('''ABC'' from 2 down to 0 = {0}', i.ToString);

  // Try to find the position of 'ABC' in elements 5 down to 3
  i := System.Array.LastIndexOf(myArray, 'ABC', 5, 3);
  Console.WriteLine('''ABC'' from 5 down to 3 = {0}', i.ToString);

  Console.ReadLine;
end.
   'ABC' from 2 down to 0 = 2
   'ABC' from 5 down to 3 = -1
 
 
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Contact the author