delphi中怎么获取value值

1085
2023/8/17 16:44:40
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在Delphi中,可以通过以下方式获取value值:

  1. 根据键获取value值:
var
dict: TDictionary<string, Integer>;
value: Integer;
begin
dict := TDictionary<string, Integer>.Create;
try
dict.Add('key1', 10);
dict.Add('key2', 20);
if dict.TryGetValue('key1', value) then
begin
ShowMessage('Value of key1: ' + IntToStr(value));
end;
finally
dict.Free;
end;
end;
  1. 使用索引获取value值:
var
dict: TDictionary<string, Integer>;
value: Integer;
begin
dict := TDictionary<string, Integer>.Create;
try
dict.Add('key1', 10);
dict.Add('key2', 20);
value := dict['key1'];
ShowMessage('Value of key1: ' + IntToStr(value));
finally
dict.Free;
end;
end;

请注意,在使用索引获取value值时,如果指定的键不存在,会抛出异常。因此,在使用索引获取value值之前,最好先使用ContainsKey方法判断键是否存在。

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: Delphi中ExtractFilePath、ParamStr以及更多文件/目录操作