I have a function where I store some key- value pairs and when I iterate them I get this error twice: [dcc32 Error] App.pas(137): E2149 Class does not have a default property. Here is part of my code:
function BuildString: string;
var
i: Integer;
requestContent: TDictionary<string, string>;
request: TStringBuilder;
begin
requestContent := TDictionary<string, string>.Create();
try
// add some key-value pairs
request := TStringBuilder.Create;
try
for i := 0 to requestContent.Count - 1 do
begin
// here I get the errors
request.Append(requestContent.Keys[i] + '=' +
TIdURI.URLEncode(requestContent.Values[i]) + '&');
end;
Result := request.ToString;
Result := Result.Substring(0, Result.Length - 1); //remove the last '&'
finally
request.Free;
end;
finally
requestContent.Free;
end;
end;
I need to collect the information from each item in the dictionary. How can I fix it?
The
KeysandValuesproperties of your dictionary class are of typeTDictionary<string, string>.TKeyCollectionandTDictionary<string, string>.TValueCollectionrespectively. These classes are derived fromTEnumerable<T>and cannot be iterated by index. You can however iterate overKeys, or indeedValues, not that doing the latter would be of much use to you.If you iterated over
Keysyour code might look like this:This however is inefficient. Since you know that you want both key and matching value, you can use the dictionary's iterator:
The pair iterator is more efficient than the first variant above. This is because the implementation details mean that the pair iterator is able to iterate the dictionary without: