Delphi 从 TObjectDictionary 中提取键

标签 delphi generics vcl delphi-xe

分享这个问题的代码作为引用:Delphi TPair Exception

如何在不使用 TPair 且不从列表中提取/移除/删除该对的情况下从 TObjectDictionary 具体条目中检索键和值?

{$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<TProduct, Integer>;

{ TProduct }

procedure TProduct.SetName(const Value: string);
begin
  FName := Value;
end;


var
  MyDict: TListOfProducts;
  MyProduct1: TProduct;
  MyProduct2: TProduct;
  MyProduct3: TProduct;
  APair: TPair<TProduct, Integer>;
  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.

谢谢。

==========================

= 带有答案的代码 =

{$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<TProduct, Integer>;

{ 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<TProduct, Integer>;
  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.

最佳答案

您可以使用 MyDict 的 KeysValues 属性。

在这样的循环中:

var
  MyProduct: TProduct;
  Value: Integer;
begin

  for Value in MyDict.Values do
    writeln(Value);

  for MyProduct in MyDict.Keys do
    writeln(MyProduct.Name);

或者使用ToArray通过索引:

writeln(MyDict.Keys.ToArray[1].Name);
writeln(MyDict.Values.ToArray[1]);

关于Delphi 从 TObjectDictionary 中提取键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5866185/

相关文章:

php - 使用 Delphi 2010 和 php4Delphi 编写 PHP 扩展

delphi - Delphi中的全局变量

c# - 从平面文件加载数据的通用实体

c# - 如何创建具有动态对象类型的通用列表

database - Delphi中保存数据最好的方法是什么

generics - 我可以在 Rust 中提供一组具有单个类型参数的相关类型吗?

c++ - 如何 shell 到另一个应用程序并让它以 Borland VCL 形式出现 (c++)

delphi - 在运行时按需更改组件类

delphi - 使用VCL对接: How to make vertically docked Forms prevail over horizontally docked Forms?

delphi - 如何修复 "Cannot open clipboard: Access Denied"错误?