标签: tdictionary

如何在Delphi 2009中按键按字母顺序列出TDictionary?

如何使用TEnumerator按键排序顺序浏览我的TDictionary?

我有这样的事情:

  var
    Dic: TDictionary<string, string>;
    Enum: TPair<string, string>;

  begin
    Dic := TDictionary<string, string>.create;
    Dic.Add('Tired', 'I have been working on this too long');
    Dic.Add('Early', 'It is too early in the morning to be working on this');
    Dic.Add('HelpMe', 'I need some help'); 
    Dic.Add('Dumb', 'Yes I know this example is dumb');

   { I want to do the following but do it in sorted order by Enum.Key }
    for Enum in Dic do
      some processing with Enum.Key and Enum.Value;

    Dic.Free;
  end;
Run Code Online (Sandbox Code Playgroud)

所以我想按顺序处理我的字典:Dumb,Early,HelpMe,Tired.

不幸的是,Delphi帮助在描述一般的枚举器和TEnumerator如何具体工作方面是非常小的,并没有给出我能找到的例子.关于在Delphi中使用带有泛型的枚举器,网上也很少写. …

delphi generics delphi-2009 tdictionary

19
推荐指数
3
解决办法
2万
查看次数

Delphi TDictionary迭代

我有一个函数,我存储一些键值对,当我迭代它们时,我得到两次错误:[dcc32错误] App.pas(137):E2149类没有默认属性.这是我的代码的一部分:

function BuildString: string;
var
  i: Integer;
  requestContent: TDictionary<string, string>;
  request: TStringBuilder;
begin
  requestContent := TDictionary<string, string>.Create();

  try
    // add some key-value pairs
    request :=  TStringBuilder.Create;
    try
      for i := 0 to requestContent.Count - 1 do
      begin
        // here I get the errors
        request.Append(requestContent.Keys[i] + '=' +
          TIdURI.URLEncode(requestContent.Values[i]) + '&');
      end;

      Result := request.ToString;
      Result := Result.Substring(0, Result.Length - 1); //remove the last '&'
    finally
      request.Free;
    end; 
  finally
    requestContent.Free;
  end;
end;
Run Code Online (Sandbox Code Playgroud)

我需要收集字典中每个项目的信息.我该如何解决?

delphi iteration tdictionary

18
推荐指数
1
解决办法
2万
查看次数

有没有简单的方法将TDictionary内容复制到另一个?

有一种方法或简单方法如何将一个TDictionary内容复制到另一个中?假设我有以下声明

type
  TItemKey = record
    ItemID: Integer;
    ItemType: Integer;
  end;
  TItemData = record
    Name: string;
    Surname: string;
  end;
  TItems = TDictionary<TItemKey, TItemData>;

var
  // the Source and Target have the same types
  Source, Target: TItems;
begin
  // I can't find the way how to copy source to target
end;
Run Code Online (Sandbox Code Playgroud)

我想将源1:1复制到目标.有这样的方法吗?

谢谢!

delphi generics deep-copy tdictionary delphi-xe2

14
推荐指数
1
解决办法
2815
查看次数

delphi使用记录作为TDictionary中的键

你能在TDictionary中使用记录作为Key值吗?我想基于字符串,整数和整数的组合来查找对象.

TUserParKey=record
  App:string;
  ID:integer;
  Nr:integer;
end;
Run Code Online (Sandbox Code Playgroud)

...

var
  tmpKey:TUserParKey;
  tmpObject:TObject;
begin
  tmpObject:= TTObject.Create(1); 
  tmpKey.App:='1';
  tmpKey.ID :=1;
  tmpKey.Nr :=1;

  DTUserPars.Add(tmpKey,tmpObject)
Run Code Online (Sandbox Code Playgroud)

...

var
  tmpKey:TUserParKey;
begin
  tmpKey.App:='1';
  tmpKey.ID :=1;
  tmpKey.Nr :=1;

  if not DTUserPars.TryGetValue(tmpKey,Result) then begin
    result := TTObject.Create(2); 
  end;
Run Code Online (Sandbox Code Playgroud)

这返回对象2.

delphi generics collections tdictionary

14
推荐指数
1
解决办法
3624
查看次数

如何为从TDictionary派生的类创建自定义枚举器?

我已经定义了一个派生自TDictionary的集合,需要定义一个应用额外过滤器的自定义枚举器.

我被卡住,因为我无法访问TDictionary FItems数组(它是私有的)所以我无法定义MoveNext方法

您将如何继续重新定义从TDictionary派生的类的过滤枚举器?

这是一个简单的代码来说明我想要做的事情:

TMyItem = class(TObject)
public
  IsHidden:Boolean; // The enumerator should not return hidden items
end;
TMyCollection<T:TMyItem> = class(TDictionary<integer,T>)
public
   function GetEnumerator:TMyEnumerator<T>; // A value filtered enumerator
   type
     TMyEnumerator = class(TEnumerator<T>)
     private
       FDictionary: TMyCollection<integer,T>;
       FIndex: Integer;
       function GetCurrent: T;
     protected
       function DoGetCurrent: T; override;
       function DoMoveNext: Boolean; override;
     public
       constructor Create(ADictionary: TMyCollection<integer,T>);
       property Current: T read GetCurrent;
       function MoveNext: Boolean;
     end;
end;

function TMyCollection<T>.TMyEnumerator.MoveNext: Boolean;
begin
// In below code, FIndex is not accessible, so I can't …
Run Code Online (Sandbox Code Playgroud)

delphi enumerator tdictionary

9
推荐指数
1
解决办法
1296
查看次数

按整数键的顺序对TDictionary进行排序

如何在Delphi 2009中按升序键的整数键对TDictionary进行排序?

delphi sorting tdictionary

8
推荐指数
1
解决办法
7057
查看次数

如何从TDictionary获取密钥?

我有一个TDictionary<TKeyClass, TValueClass>.

我想完成类似的事情

for i := 0 to MyDictionary.Count -1 do
  ShowMessage(MyDictionary.Keys[i].AStringProperty)
Run Code Online (Sandbox Code Playgroud)

我不能再访问密钥如果我完全了解它我就可以使用它们.

是唯一的替代品TDictionary<TValueClass, TKeyValue>吗?所以我可以循环使用Keys?

我找到的工作方法是创建一个,TList<TKeyClass>但这是我不喜欢的东西.

delphi tdictionary

7
推荐指数
1
解决办法
4703
查看次数

为什么反序列化的TDictionary不能正常工作?

我尝试使用标准delphi序列化程序序列化/反序列化标准delphi容器.

procedure TForm7.TestButtonClick(Sender: TObject);
var
    dict: TDictionary<Integer, Integer>;
    jsonValue: TJSONValue;
begin
    //serialization
    dict := TDictionary<Integer, Integer>.Create;
    dict.Add(1, 1);
    jsonValue := TJsonConverter.ObjectToJSON(dict);
    dict.Free;

    //deserialization
    dict := TJsonConverter.JSONToObject(jsonValue) as TDictionary<Integer, Integer>;
    try
        Assert(dict.ContainsKey(1), 'deserialization error - key not found');
    except
        Assert(false, 'deserialization error - dict object broken');
    end;
end;
Run Code Online (Sandbox Code Playgroud)

有一种方法我将对象转换为JSON,反之亦然;

class function TJsonConverter.JSONToObject(AJSONValue: TJSONValue): TObject;
var
    lUnMarshal: TJSONUnMarshal;
begin
    lUnMarshal := TJSONUnMarshal.Create();
    try
        Result := lUnMarshal.Unmarshal(AJSONValue);
    finally
        lUnMarshal.Free;
    end;
end;

class function TJsonConverter.ObjectToJSON(AData: TObject): TJSONValue;
var
    lMarshal: TJSONMarshal;
begin
    lMarshal := TJSONMarshal.Create(); …
Run Code Online (Sandbox Code Playgroud)

delphi serialization json tdictionary json-deserialization

7
推荐指数
1
解决办法
537
查看次数

Delphi中的类字段(静态字段)

有一个类TPerson.众所周知,FSecondName对每个对象都是唯一的.

type
  TPerson = class(TObject)
  private
    FAge:        Integer;
    FFirstName:  String;
    FSecondName: String;
  public
    property Age:        Integer read FAge;
    property FirstName:  String  read FFirstName;
    property SecondName: String  read FSecondName;
    constructor Create;
  end;
Run Code Online (Sandbox Code Playgroud)

如何添加类字段(如C#中的静态字段)Persons:TDictionary(String,TPerson),其中键是SecondName,值是类TPerson的对象.

谢谢!

delphi static tdictionary

6
推荐指数
1
解决办法
3157
查看次数

GetHashCode的好习惯?

对于Delphi项目(使用RAD Studio XE7构建),我想创建一个画笔字典.每个字典项包含一个TMyBrush对象作为键,描述要检索的画笔,以及GDI +画笔作为值.

TMyBrush类包含3个字段

  • 用于确定画笔类型的枚举类型(实体,渐变,......)
  • 描述画笔内容的TBrushInfo类(颜色,换行模式......)
  • 表示钳位字段的TRect

在我的字典中,我想根据他的特征检索画笔,而不是他的实例.例如,我希望通过创建本地TMyBrush实例,将其配置为黑色实体,并使用TryGetValue()函数获取匹配的GDI +值,从我的字典中获取黑色实心画笔.为此,我创建了一个TMyBrushComparer.

编写Equals()函数对我来说不是问题.但是我不知道编写GetHashCode()函数的最佳做法是什么.我倾向于写一个这样的函数:

function TMyBrushComparer.GetHashCode(const pValue: TMyBrush): Integer;
begin
    Result := BobJenkinsHash(pValue, SizeOf(TMyBrush), 0);
end;
Run Code Online (Sandbox Code Playgroud)

但是我觉得这不是一个很好的做法,这是正确的吗?那么,为我的TMyBrushComparer编写一个好的GetHashCode()函数的最佳实践是什么?

问候

delphi hash tdictionary gethashcode

6
推荐指数
1
解决办法
760
查看次数