27.08.2010, 19:13
|
|
Активный
|
|
Регистрация: 12.09.2008
Сообщения: 275
Репутация: 43
|
|
Код:
unit CustomStringList;
interface
uses
SysUtils;
type
TCustomStringList = class
private
FLines: Array of String;
function GetText: String;
function GetCount: Integer;
function GetLines(const Index: Integer): String;
procedure SetLines(const Index: Integer; const Value: String);
public
constructor Create; virtual;
destructor Destroy; override;
property Text: String read GetText;
property Count: Integer read GetCount;
property Lines[const Index: Integer]: String read GetLines write SetLines;
procedure Add(const Value: String);
procedure Clear;
end;
implementation
constructor TCustomStringList.Create;
begin
inherited;
SetLength(FLines, 0);
end;
destructor TCustomStringList.Destroy;
begin
SetLength(FLines, 0);
inherited;
end;
function TCustomStringList.GetLines(const Index: Integer): String;
begin
if (Index >= 0) and (Index <= Length(FLines)-1) then
Result := FLines[Index]
else
begin
raise Exception.Create('Недопустимый индекс строки');
Exit;
end;
end;
procedure TCustomStringList.SetLines(const Index: Integer; const Value: String);
begin
if (Index >= 0) and (Index <= Length(FLines)-1) then
FLines[Index] := Value
else
begin
raise Exception.Create('Недопустимый индекс строки');
Exit;
end;
end;
function TCustomStringList.GetText;
var
I, B, E: Integer;
begin
Result:= '';
B:= Low(FLines);
E:= High(FLines);
for I:=B to E do
if I = E then
Result:= Result + FLines[i]
else
Result:= Result + FLines[i] + #13#10;
end;
function TCustomStringList.GetCount;
begin
Result:= Length(FLines);
end;
procedure TCustomStringList.Add(const Value: String);
begin
SetLength(FLines, Length(FLines)+1);
FLines[High(FLines)]:= Value;
end;
procedure TCustomStringList.Clear;
begin
SetLength(FLines, 0);
end;
end.
Используем
Код:
var
CSL: TCustomStringList;
I: Integer;
begin
CSL:= TCustomStringList.Create;
for I:= 1 to 10 do
CSL.Add('Lines '+IntToStr(I));
Memo1.Lines.Add(CSL.Text);
Memo1.Lines.Add(inttostr(CSL.Count));
CSL.Free;
end;
|