首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >访问TDictionary项目

访问TDictionary项目
EN

Stack Overflow用户
提问于 2014-02-25 03:34:50
回答 1查看 1.1K关注 0票数 0

我正在使用embarcadero示例( http://docwiki.embarcadero.com/CodeExamples/XE5/en/Generics_Collections_TDictionary_%28Delphi%29 )测试TDictionary。

创建和添加键和值没有问题。但是,当我尝试使用键值‘London’访问该表时:

(1) Dictionary.Items‘’London‘.Country ->给出了正确的值“Dictionary.Items’‘London’.Country‘

(2)在Edit1.Text中输入'London‘,然后Dictionary.ItemsEdit1.Text.Country ->给出错误"the item not found“?

有没有人能解释一下?

提前谢谢。

/

代码语言:javascript
复制
var Dictionary: TDictionary<String, TCity>;
    City, Value: TCity;
    Key: String;


begin
  Dictionary := TDictionary<String, TCity>.Create;
  City := TCity.Create;
  { Add some key-value pairs to the dictionary. }
  City.Country := 'Romania';
  City.Latitude := 47.16;
  City.Longitude := 27.58;
  Dictionary.Add('Iasi', City);

  City := TCity.Create;
  City.Country := 'United Kingdom';
  City.Latitude := 51.5;
  City.Longitude := -0.17;
  Dictionary.Add('London', City);

  City := TCity.Create;
  City.Country := 'Argentina';
  { Notice the wrong coordinates }
  City.Latitude := 0;
  City.Longitude := 0;
  Dictionary.Add('Buenos Aires', City);

  showmessage(Dictionary.Items['London'].Country); // This One is OK

  // now using Edit1.Text where I put 'London'
  Showmessage(Dictionary.Items[Edit1.Text].Country); // This return to error message (Item not found)


  Dictionary.Clear;
  Dictionary.Free;
  City.Free;

end;
EN

回答 1

Stack Overflow用户

发布于 2014-02-25 03:38:17

解释是,与您声称的相反,Edit1.Text不等于'London'。可能字母大小写不匹配。或者有前导空格或尾随空格。

添加断言以验证我是否正确:

代码语言:javascript
复制
Assert(Edit1.Text='London');
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21997242

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档