Hallo.
Danke für eure Antworten.
Ich habe jetzt folgende Variante für TStringList und THashedStringList geschrieben. Dadurch wird der Code auch gleich viel schlanker.
Delphi-Quellcode:
uses
IniFiles;
type
TEasyObjectManager = class(TObject)
published
constructor Create(CaseSensitive: boolean);
destructor Destroy; override;
public
function GetValue(Name: string): TObject;
procedure SetValue(Name: string; Value: TObject);
function ValueExists(Name: string): boolean;
procedure DeleteValue(Name: string);
function Count: integer;
function GetValueById(Index: integer): TObject;
function GetNameById(Index: integer): string;
private
List: THashedStringList; // Alternativ: TStringList
end;
(*** TEasyObjectManager ***)
constructor TEasyObjectManager.Create(CaseSensitive: boolean);
begin
inherited Create;
List := THashedStringList.Create;
// Ich bin der Meinung, man sollte das vorher definieren und nicht mittendrin ändern
List.CaseSensitive := CaseSensitive;
end;
destructor TEasyObjectManager.Destroy;
begin
if Assigned(List) then List.Free;
inherited;
end;
function TEasyObjectManager.GetValue(Name: string): TObject;
var
i: integer;
begin
i := List.IndexOf(Name);
if i <> -1 then
begin
result := List.Objects[i];
end
else
begin
result := nil;
end;
end;
procedure TEasyObjectManager.SetValue(Name: string; Value: TObject);
var
i: integer;
begin
i := List.IndexOf(Name);
if i <> -1 then
begin
List.Objects[i] := Value;
end
else
begin
UniqueString(Name); // Notwendig?
List.AddObject(Name, Value);
end;
end;
function TEasyObjectManager.ValueExists(Name: string): boolean;
begin
result := List.IndexOf(Name) <> -1;
end;
procedure TEasyObjectManager.DeleteValue(Name: string);
var
i: integer;
begin
i := List.IndexOf(Name);
if i <> -1 then
begin
List.Delete(i);
end;
end;
function TEasyObjectManager.Count: integer;
begin
result := List.Count;
end;
function TEasyObjectManager.GetValueById(Index: integer): TObject;
begin
result := List.Objects[Index];
end;
function TEasyObjectManager.GetNameById(Index: integer): string;
begin
result := List.Strings[Index];
end;
Gruß
blackdrake