title: Delphi extract key from TObjectDictionary
body: <p>sharing the code on this question as reference:  <a href="https://stackoverflow.com/questions/5861857/delphi-tpair-exception">Delphi TPair Exception</a></p>
  
  <p>How can I retrieve the key and value from a TObjectDictionary concrete entry without using TPair and without extracting/remove/delete the pair from the list ?</p>
  
  <pre><code>{$APPTYPE CONSOLE}
  
  uses
    SysUtils,
    Generics.Defaults,
    Generics.Collections;
  
  type
    TProduct = class
    private
      FName: string;
      procedure SetName(const Value: string);
    published
    public
      property Name: string read FName write SetName;
    end;
  
  type
    TListOfProducts = TObjectDictionary&lt;TProduct, Integer&gt;;
  
  { TProduct }
  
  procedure TProduct.SetName(const Value: string);
  begin
    FName := Value;
  end;
  
  
  var
    MyDict: TListOfProducts;
    MyProduct1: TProduct;
    MyProduct2: TProduct;
    MyProduct3: TProduct;
    APair: TPair&lt;TProduct, Integer&gt;;
    aKey: string;
  
  begin
    try
      MyDict := TListOfProducts.Create([doOwnsKeys]);
      MyProduct1 := TProduct.Create;
      MyProduct1.Name := 'P1';
      MyProduct2 := TProduct.Create;
      MyProduct2.Name := 'P2';
      MyProduct3 := TProduct.Create;
      MyProduct3.Name := 'P3';
  
      MyDict.Add(MyProduct1, 1);
      MyDict.Add(MyProduct2, 2);
      MyDict.Add(MyProduct3, 3);
  
      //the code to look for a **concrete product** (ie: MyProduct1) goes here..
  
      Readln;
    except
      on E: Exception do
        Writeln(E.ClassName, ': ', E.Message);
    end;
  end.
  </code></pre>
  
  <p>Thanks.</p>
  
  <p>=========================</p>
  
  <h1>=  Code with the answer =</h1>
  
  <pre><code>{$APPTYPE CONSOLE}
  
  uses
    SysUtils,
    Generics.Defaults,
    Generics.Collections;
  
  type
    TProduct = class
    private
      FName: string;
      procedure SetName(const Value: string);
    published
    public
      property Name: string read FName write SetName;
    end;
  
  type
    TListOfProducts = TObjectDictionary&lt;TProduct, Integer&gt;;
  
  { TProduct }
  
  procedure TProduct.SetName(const Value: string);
  begin
    FName := Value;
  end;
  
  
  var
    MyDict: TListOfProducts;
    MyProduct1: TProduct;
    MyProduct2: TProduct;
    MyProduct3: TProduct;
  
    MySearchedProduct: TProduct;   // From Answer.
  
    APair: TPair&lt;TProduct, Integer&gt;;
    aProductName: string;
  
  begin
    try
      MyDict := TListOfProducts.Create([doOwnsKeys]);
      MyProduct1 := TProduct.Create;
      MyProduct1.Name := 'P1';
      MyProduct2 := TProduct.Create;
      MyProduct2.Name := 'P2';
      MyProduct3 := TProduct.Create;
      MyProduct3.Name := 'P3';
  
      MyDict.Add(MyProduct1, 1);
      MyDict.Add(MyProduct2, 2);
      MyDict.Add(MyProduct3, 3);
  
      Writeln('Enter the Product Name to search: ');
  
      //the code to look for a **concrete product** goes here..
      Readln(aProductName);
      for MySearchedProduct in Mydict.Keys do
        if (MySearchedProduct.Name = aProductName) then
          break;
      if MySearchedProduct.Name = aProductName then
        WriteLn('I have found the product: ' + MySearchedProduct.Name)
      else
        WriteLn('I have not found a product with that name.');
  
      Readln;
    except
      on E: Exception do
        Writeln(E.ClassName, ': ', E.Message);
    end;
  end.
  </code></pre>