Home  |  Delphi .net Home  |  System.Collections.HashTable  |  ContainsKey Method
ContainsKey  
Method  
Returns true if the current HashTable contains the given Key
HashTable Class
System.Collections NameSpace
CF1.  Function ContainsKey ( Key : Object; ) : Boolean;
CF : Methods with this mark are Compact Framework Compatible
Description
The ContainsKey method searches the current HashTable for an entry matching Key, returning true if found, otherwise false.
Notes
This is identical in operation to the Contains method.
Microsoft MSDN Links
System.Collections
System.Collections.HashTable
 
 
A simple example
program Project1;
{$APPTYPE CONSOLE}

uses
  System.Collections;

var
  MyTable    : System.Collections.HashTable;
  DictEntry  : DictionaryEntry;
  Enumerator : IEnumerator;

begin
  // Create our hash table
  MyTable := HashTable.Create;

  // Add entries to it
  MyTable['Name']     := 'Neil Moffatt';
  MyTable['Birthday'] := DateTime.Create(1957, 2, 18);
  MyTable['Age']      := TObject(47);

  // Display the table contents
  Console.WriteLine('MyTable at the start :');
  Console.WriteLine;
  Enumerator := MyTable.GetEnumerator;
  while Enumerator.MoveNext do
  begin
    // Note that entries are returned in key sorted order
    DictEntry := DictionaryEntry(Enumerator.Current);
    Console.WriteLine('{0} = {1}',
                      DictEntry.Key.ToString,
                      DictEntry.Value.ToString);
  end;

  Console.WriteLine;

  if MyTable.ContainsKey('Age')
  then Console.WriteLine('MyTable contains key ''Age''')
  else Console.WriteLine('MyTable does not contain key ''Age''');

  if MyTable.ContainsKey('AGE')
  then Console.WriteLine('MyTable contains key ''AGE''')
  else Console.WriteLine('MyTable does not contain key ''AGE''');

  Console.Readline;
end.
Show full unit code
  MyTable at the start :
  
  Birthday = 18/02/1957 00:00:00
  Age = 47
  Name = Neil Moffatt
  
  MyTable contains key 'Age'
  MyTable does not contain key 'AGE'
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author