Home  |  Delphi .net Home  |  System.Array  |  IndexOf Method
  
IndexOf  
Method  
Tries to find the first occurence of an object in a single dimensional arra
Array Class
System NameSpace
NotCF1.  Function IndexOf ( TheArray : System.Array; Value : Object ) : Integer; Static;
NotCF2.  Function IndexOf ( TheArray : System.Array; Value : Object; FromIndex : Integer ) : Integer; Static;
NotCF3.  Function IndexOf ( TheArray : System.Array; Value : Object; FromIndex : Integer; Count : Integer ) : Integer; Static;
CF : Methods with this mark are Compact Framework Compatible
Description
TheArray single dimensional array is searched for the first 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 start to the end unles the optional FromIndex and Count parameters are provided to give a start and range of elements as appropriate.
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.IndexOf(myArray, 'ABC');
  Console.WriteLine('First position of ''ABC'' = {0}', i.ToString);

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

  Console.ReadLine;
end.
   First position of 'ABC' = 0
   First position of 'abc' = -1
Using the optional FromIndex 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 'GHI' in elements 0 to 2
  i := System.Array.IndexOf(myArray, 'GHI', 0, 3);
  Console.WriteLine('''GHI'' from 0 to 2 = {0}', i.ToString);

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

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