[英]Access TDictionary item
I am testing the TDictionary using the embarcadero sample ( http://docwiki.embarcadero.com/CodeExamples/XE5/en/Generics_Collections_TDictionary_%28Delphi%29 ) 我正在使用embarcadero示例测试TDictionary( http://docwiki.embarcadero.com/CodeExamples/XE5/en/Generics_Collections_TDictionary_%28Delphi%29 )
No problem for creating and adding key and value. 创建和添加键和值没有问题。 However, when I try to access the table using key value 'London': 但是,当我尝试使用键值“伦敦”访问表时:
(1) Dictionary.Items['London'].Country -> gives the correct value "Dictionary.Items['London'].Country' (1)Dictionary.Items ['London']。Country->给出正确的值“ Dictionary.Items ['London']。Country'
(2) input 'London' in Edit1.Text, then Dictionary.Items[Edit1.Text].Country -> gives error "the item not found'? (2)在Edit1.Text中输入“伦敦”,然后在Dictionary.Items [Edit1.Text] .Country->中给出错误“找不到该项目”?
Could someone explain this? 有人可以解释一下吗?
Thanks in advance. 提前致谢。
//////////////////////////////////// /// sample code //////////////////////////////////////示例代码
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;
The explanation is that, contrary to what you claim, Edit1.Text
does not equal 'London'
. 解释是,与您声称的相反, Edit1.Text
不等于'London'
。 Perhaps the letter case does not match. 也许字母大小写不匹配。 Or there is leading or trailing whitespace. 或者存在前导或尾随空格。
Add an assertion to verify that I am correct: 添加一个断言以验证我是正确的:
Assert(Edit1.Text='London');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.