Delphi Basics Home  |  Delphi Basics .NET Home  |  System.Collections.SortedList  |  ContainsKey Method
ContainsKey  
Method  
Returns true if the current SortedList contains the given Key
SortedList Class
System.Collections NameSpace
CF1.  Function ContainsKey ( Key : Object ) : Boolean;
CF : Methods with this mark are Compact Framework Compatible
Description
The Contains method searches the current SortedList 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.SortedList
System.Collections.SortedList.ContainsKey
 
A simple example
program Project1;
{$APPTYPE CONSOLE}

uses
  System.Collections;

var
  MyList     : System.Collections.SortedList;
  DictEntry  : DictionaryEntry;
  Enumerator : IEnumerator;

begin
  // Create our sorted list
  MyList := SortedList.Create;

  // Add entries to the Source list
  MyList['Name']     := 'Thomas Johnson';
  MyList['Age']      := TObject(47);
  MyList['Birthday'] := DateTime.Create(1957, 2, 18);

  // Display the Sorted list
  Enumerator := MyList.GetEnumerator;
  while Enumerator.MoveNext do
  begin
    DictEntry := DictionaryEntry(Enumerator.Current);
    Console.WriteLine('{0} = {1}',
                      DictEntry.Key.ToString,
                      DictEntry.Value.ToString);
  end;

  Console.WriteLine;

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

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

  Console.Readline;
end.
   Age = 47
   Birthday = 18/02/1957 00:00:00
   Name = Thomas Johnson
  
   MyList contains key 'Age'
   MyList does not contain key 'AGE'
 
© CodeGear 2006 - 2007. All rights reserved.