Title: Key name - value list
Question: Sometimes it's nice to be able to store information in a list, and get access each item by a key name, like valueList['Name']
Answer:
This is pretty simple to implement, I'll first give you code samples on how to use this class and then the code to the class itself.
First, to use this class we must add it to the uses list,
uses
abhStringValueList;
Then create an instance of the object
valueList := TABHStringValueList.Create;
Then we can fill it with data
valueList['Name'] := 'rni B. Halldrsson';
valueList['Email'] := 'abh@hugvit.is';
Then we can read the values
ShowMessage(valueList['Name'] + ', ' + valueList['Email']);
Or we can display key names and values by index
for i := 0 to valueList.Count - 1 do begin
ShowMessage(valueList.Names[i] + ' = ' + valueList.ValuesByIndex[i]);
end;
Then here is the code for the class itself, it inheriteds from TStrings and stores all it's data in Key=Value format.
// -------- CODE STARTS ---------
unit abhStringValueList;
interface
uses
classes;
type
TABHStringValueList = class(TStringList)
private
{ Return value from key name }
function Get_Value(Name: string): string;
{ Set value by key name }
procedure Set_Value(Name: string; const Value: string);
{ Return key name from index }
function Get_Name(index: integer): string;
{ Return value from index }
function Get_ValuesByIndex(index: integer): string;
public
property Values[name: string]: string read Get_Value write Set_Value; default;
property Names[index: integer]: string read Get_Name;
property ValuesByIndex[index: integer]: string read Get_ValuesByIndex;
end;
implementation
{ TABHStringValueList }
function TABHStringValueList.Get_Name(index: integer): string;
{ Returns the key name from given index }
var
eqPos: integer;
begin
Result := '';
// Check to see if item exists
if index // Find the = symbol
eqPos := Pos('=', Strings[index]);
if eqPos 0 then begin
// Return all chars before the = symbol
Result := Copy(Strings[index], 1, eqPos - 1);
end;
end;
end;
function TABHStringValueList.Get_Value(Name: string): string;
{ Returns the value from given key name }
var
i: integer;
begin
Result := '';
// Check to see if key exists
for i := 0 to Count - 1 do begin
if pos(Name + '=', Strings[i]) = 1 then begin
// Found key, parse out the value and send back
Result := Copy(Strings[i], Length(Name) + 2, Length(Strings[i]));
Break;
end;
end;
end;
function TABHStringValueList.Get_ValuesByIndex(index: integer): string;
{ Return the value from given index }
var
eqPos: integer;
begin
Result := '';
// Check to see if item exists
if index eqPos := Pos('=', Strings[index]);
if eqPos 0 then begin
// Return all chars after the = symbol
Result := Copy(Strings[index], eqPos + 1, Length(Strings[index]));
end;
end;
end;
procedure TABHStringValueList.Set_Value(Name: string; const Value: string);
var
i: integer;
begin
// Check to see if key exists
for i := 0 to Count - 1 do begin
if pos(Name, Strings[i]) = 1 then begin
// Found the key, so update the data
Strings[i] := Name + '=' + Value;
Exit;
end;
end;
// If we get this far, we didn't find the key
// so let's add the data
Add(Name + '=' + Value);
end;
end.
// -------- CODE ENDS ---------
For further improvements, we can implement a way to store multiple instances of keys, like
valueList.AddValue('Name', 'Arni B. Halldorsson');
valueList.AddValue('Name', 'Saeunn Hermannsdottir');
valueList.AddValue('Email', 'abh@simnet.is');
valueList.AddValue('Email', 'gullbra@simnet.is');
and then get the values like
ShowMessage(valueList['Name:1']);
ShowMessage(valueList['Name:2']);