Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I declare a dictionary with dynamic array as value type?

I would like to have a function that takes a dictionary of string, array of variant. So it can be called with:

  searchDictionary := TDictionary<string, array of variant>;
  searchDictionary.Add('KEY_NAME', ['X01%', '%D01']);
  aValue := TDtoClass.Search(searchDictionary)

I am currently achieving this by

  searchDictionary := TDictionary<string, TList<variant>>.Create;
  searchDictionary.Add('BIN_NAME', TSearch.Values(['X01%', '%D01']));

where Tsearch is a class that exposes:

class function TSearch.Values(const arguments: array of variant): TList<variant>;
var
list : TList<variant>;
item: variant;
begin
    list := TList<variant>.Create;
    for item in arguments do
    begin
      list.Add(item);
    end;
    Result := list;           
end;

What I would like to do is:

searchDictionary.Add('BIN_NAME', ['X01%', '%D01']);

instead of:

searchDictionary.Add('BIN_NAME', TSearch.Values(['X01%', '%D01']));

Any help will be greatly appreciated.

like image 235
reckface Avatar asked Jun 29 '12 10:06

reckface


1 Answers

While there is no problem in declaring the dictionary, adding values might get somewhat tricky. You can use a special construct to get the required variant array:

var
  searchDictionary: TDictionary<string, TArray<variant>>;
begin
  searchDictionary.Add('BIN_NAME', TArray<variant>.Create('X01%', '%D01'));
end;
like image 121
Uwe Raabe Avatar answered Oct 13 '22 01:10

Uwe Raabe