I want to create key-value pairs in a TStringList in a C++Builder 6 VCL application.
I know how to read the Names and Values out, but I can't find a way to create the paired entries that works.
I tried, for example:
TStringList *StringList = new TStringList();
StringList->Add("Sydney=2000");
StringList->Names[0] returns "Sydney" but StringList->Values[0] returns "".
I know this works in CB11, but I can't find a way in BCB6.
You are creating the named pair correctly (in recent versions, you can use the
AddPair()method instead).The
Values[]property takes a key name, not an index (and always has, so your claim that the code you have shown works in CB11 is incorrect), eg:The only reason that using
Values[0]even compiles at all is because the property takes aSystem::String, and in C++ aStringcan be constructed directly from an integer value (in Delphi, you have to make that conversion explicitly using theSysUtils.IntToStr()function).The
ValueFromIndex[]property, on the other hand, takes an index, but it wasn't available until Delphi 7 (and thus did not appear in C++Builder until CB2006), eg:So, because
TStringListin C++Builder/Delphi 6 did not have theValueFromIndex[]property yet, if you want to access a value by index then you will have to use theNames[]property to get the key name needed for theValues[]property, eg:Alternatively, you can extract the value manually (so you don't have to waste time searching the list for a matching name), eg: