I have a lot of constants that are somehow related, at some point I need to pair them, something like this:
const
key1 = '1';
key2 = '2';
key3 = '3';
value1 = 'a';
value2 = 'b';
value3 = 'c';
I want to avoid doing:
if MyValue = key1 then Result := value1;
I know how to do it with string lists using:
MyStringList.Add(key1 + '=' + value1);
Result := MyStringList.Values[key1];
But, is there any simpler way to do this?
Yes, assignment can be done this way instead, avoiding manual string concatenation:
MyStringList.Values[Key1] := Value1;
Do a wrapper around your "value"
TMyValue = class value: String; end;
Then use this:
myValue := TMyValue.Create; myValue.Value = Value1; MyStringList.AddObject(Key1, Value1);
Then, you can sort your list, do a IndexOf(Key1) and retrieve the object.
That way, your list is sorted and the search is very fast.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With