如何使 TObjectDictionary.Values 作为属性可访问?

发布于 2024-10-08 11:29:13 字数 552 浏览 5 评论 0原文

我有一个像这样的对象:

  TMyObj = class
  private
    FObjList: TObjectDictionary <integer, TMyObject>;
  public
    constructor Create;
    destructor Destroy;
    // How to access Values correctly? Something similar to this not working code
    property Values: TValueCollection read FObjList.Values write FObjList.Values;
  end;

  var MyObj: TMyObj;

要访问 FObjList 的值,我想写:

  for tmpObject in MyObj.Values do
...

我需要如何声明属性“Values”,以便 MyObj.Values 的行为与访问 MyObj.FObjList.Values 完全相同?

I have an object like this:

  TMyObj = class
  private
    FObjList: TObjectDictionary <integer, TMyObject>;
  public
    constructor Create;
    destructor Destroy;
    // How to access Values correctly? Something similar to this not working code
    property Values: TValueCollection read FObjList.Values write FObjList.Values;
  end;

  var MyObj: TMyObj;

To access the values of FObjList, I'd like to write:

  for tmpObject in MyObj.Values do
...

How do I need to declare the property "Values" so that MyObj.Values behaves exactly as if I would access MyObj.FObjList.Values?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橙幽之幻 2024-10-15 11:29:14
  /// Interface

  TMyDictionary = TObjectDictionary <integer, TMyObject>;
  TMyValueCollection = TDictionary<integer,TMyObject>.TValueCollection;

  TMyObj = class
  private
    FObjList: TMyDictionary;
    function GetValues: TMyValueCollection;
  public
    constructor Create;
    destructor Destroy; override;
    property Values: TMyValueCollection read GetValues;
  end;


/// Implementation


constructor TMyObj.Create;
begin
  inherited;
  FObjList := TMyDictionary.Create;
end;

destructor TMyObj.Destroy;
begin
  FObjList.Free;
  inherited;
end;

function TMyObj.GetValues: TMyValueCollection;
begin
  Result := FObjList.Values;
end;
  /// Interface

  TMyDictionary = TObjectDictionary <integer, TMyObject>;
  TMyValueCollection = TDictionary<integer,TMyObject>.TValueCollection;

  TMyObj = class
  private
    FObjList: TMyDictionary;
    function GetValues: TMyValueCollection;
  public
    constructor Create;
    destructor Destroy; override;
    property Values: TMyValueCollection read GetValues;
  end;


/// Implementation


constructor TMyObj.Create;
begin
  inherited;
  FObjList := TMyDictionary.Create;
end;

destructor TMyObj.Destroy;
begin
  FObjList.Free;
  inherited;
end;

function TMyObj.GetValues: TMyValueCollection;
begin
  Result := FObjList.Values;
end;
天荒地未老 2024-10-15 11:29:14

TValueCollection 是 TDictionary 的嵌套类,必须进行限定。并且您最好为 Values 指定一个 getter 方法。

type   
  TMyObjectDictionary = TObjectDictionary <integer, TMyObject>;

  TMyObj = class   
  private
    FObjList: TMyObjectDictionary;
    function GetValues: TMyObjectDictionary.TValueCollection;  
  public
    property Values: TMyObjectDictionary.TValueCollection read GetValues;   
  end;

function TMyObj.GetValues: TMyObjectDictionary.TValueCollection;
begin
  Result := FObjList.Values;
end;

编辑: 起来!太晚了……但略有不同。

TValueCollection is a nested class of TDictionary and has to be qualified. And you better specify a getter method for Values.

type   
  TMyObjectDictionary = TObjectDictionary <integer, TMyObject>;

  TMyObj = class   
  private
    FObjList: TMyObjectDictionary;
    function GetValues: TMyObjectDictionary.TValueCollection;  
  public
    property Values: TMyObjectDictionary.TValueCollection read GetValues;   
  end;

function TMyObj.GetValues: TMyObjectDictionary.TValueCollection;
begin
  Result := FObjList.Values;
end;

Edit: Ups! Too late... But slightly different.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文