PDA

View Full Version : سوال: مشكل در ساخت Propety



ahmad_eagle2002
یک شنبه 31 مرداد 1389, 12:32 عصر
سلام دوستان

من در كلاس خودم يك فيلد دارم به صورت زير

FName:array[0..500] of Char;

حالا براي ساخت يك Property از نوع آرايه هر كاري ميكنم به مشكل بر مي خورم

SAASTN
یک شنبه 31 مرداد 1389, 17:41 عصر
برای این منظور باید یه نوع جدید برای آرایه تعریف کنید. البته بهتره property های از نوع لیست رو بصورتی که List در مثال زیر نوشته شده پیاده سازی کنید:
TMyClassName = array [0..499] of Char;

TMyClass = class
private
FName: TMyClassName;
FList: array of Integer;
procedure SetName(const Value: TMyClassName);
function GetList(Index: Integer): Integer;
procedure SetList(Index: Integer; const Value: Integer);
function GetListCount: Integer;
public
property List[Index: Integer]: Integer read GetList write SetList;
property ListCount: Integer read GetListCount;
property Name: TMyClassName read FName write SetName;
end;

...

{ TMyClass }

function TMyClass.GetList(Index: Integer): Integer;
begin
if Index < GetListCount then
Result := FList[Index]
else
raise Exception.Create('Out Of Bound Index!');
end;

function TMyClass.GetListCount: Integer;
begin
Result := Length(FList);
end;

procedure TMyClass.SetList(Index: Integer; const Value: Integer);
begin
if Index < GetListCount then
FList[Index] := Value
else
raise Exception.Create('Out Of Bound Index!');
end;

procedure TMyClass.SetName(const Value: TMyClassName);
begin
FName := Value;
end;