代码之家  ›  专栏  ›  技术社区  ›  Nat

将引用传递给TObjectDictionary<TKey,TValue>.TValue枚举器

  •  3
  • Nat  · 技术社区  · 15 年前

    我正在尝试使用Delphi2010的TObjectDictionary。

    我想通过一个 Values 属性,编译器似乎不想让我。。。例子:

      TAttributeStates = class(TInterfacedObject, IAttributeStates)
      private
        FStates: TObjectDictionary<TPatchAttribute, TAttributeState>;
    
      public
    
        constructor Create;
        destructor Destroy; override;
    
        function GetEnumerator: TObjectDictionary<TPatchAttribute, TAttributeState>.TValueEnumerator;
    
      end;
    
      implementation
    
        function TAttributeStates.GetEnumerator: TObjectDictionary<TPatchAttribute, TAttributeState>.TValueEnumerator;
        begin
          result := FStates.Values.GetEnumerator;
        end;
    

    无法编译,错误为:

    [DCC Error] ChannelStates.pas(249): E2010 Incompatible types: 'TDictionary<Generics.Collections.TObjectDictionary<TKey,TValue>.TKey,Generics.Collections.TObjectDictionary<TKey,TValue>.TValue>.TValueEnumerator' and 'TDictionary<ChannelPatch.TPatchAttribute,ChannelStates.TAttributeState>.TValueEnumerator'
    

    似乎编译器没有正确解析子类型。。。

    有人有什么想法吗?

    不@

    1 回复  |  直到 15 年前
        1
  •  2
  •   Nat    15 年前

    找到了。

    function GetEnumerator: TEnumerator<TAttributeState>;
    
    
    function TAttributeStates.GetEnumerator: TEnumerator<TAttributeState>;
    begin
      result := FStates.Values.GetEnumerator;
    end;
    

    很好用。