温馨提示×

delphi中怎么获取value值

小亿
315
2023-08-18 16:44:40
栏目: 编程语言

在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方法判断键是否存在。

0