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

I would like to have a function that accepts a string dictionary, an array of options. Therefore, it can be called using:

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

I am currently achieving this

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

where Tsearch is a class that provides:

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:

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

instead:

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

Any help would be greatly appreciated.

+5
source share
2 answers

Although there is no problem in declaring a dictionary, adding values ​​can be a bit complicated. You can use a special construction to get the required array variant:

var
  searchDictionary: TDictionary<string, TArray<variant>>;
begin
  searchDictionary.Add('BIN_NAME', TArray<variant>.Create('X01%', '%D01'));
end;
+9
source

You can declare a type for an array of options to make as much code as you want:

type
  TDynamicArrayOfVariant = array of Variant;

var
  searchDictionary: TDictionary<string, TDynamicArrayOfVariant>;
begin
  searchDictionary.Add('BIN_NAME', ['X01%', '%D01']);
end;
0
source

All Articles