TWind = class
private
fName, fCap:string;
function GetCount: integer;
public
constructor Create;
destructor Destroy; override;
property Name: string read fName write fName;
property Cap: string read fCap write fCap;
property Count: integer read GetCount;
end; TWindList = class
private
fCount:integer;
function GetItem(Index: Integer): TWind;//这过程怎么写?
procedure SetItem(Index: Integer; Value: TWind);//还有这个?
public
constructor Create;
destructor Destroy; override;
function AddWind(Item: TWind): TWind; //这步最重要,要增加到Item中,然后使用时Item[0].Cap可以调用
property Item[Index: Integer]: TWind read GetItem write SetItem; default;
property Count: integer read fCount;
end;
private
fName, fCap:string;
function GetCount: integer;
public
constructor Create;
destructor Destroy; override;
property Name: string read fName write fName;
property Cap: string read fCap write fCap;
property Count: integer read GetCount;
end; TWindList = class
private
fCount:integer;
function GetItem(Index: Integer): TWind;//这过程怎么写?
procedure SetItem(Index: Integer; Value: TWind);//还有这个?
public
constructor Create;
destructor Destroy; override;
function AddWind(Item: TWind): TWind; //这步最重要,要增加到Item中,然后使用时Item[0].Cap可以调用
property Item[Index: Integer]: TWind read GetItem write SetItem; default;
property Count: integer read fCount;
end;
TCollectionItem和TCollection作为基类,专门实现这种功能的。
上个代码改为这个
PCmd = ^TCmd
^TCmd = record
fName, fCap:string;
fParams:array of TVarRec;
end;
TWind = record
Name, Cap:string;
Params:array of TVarRec;
end; TWindList = class
private
FList: TList;
function GetItem(Index: Integer): TWind; //how to write this procedure?
procedure SetItem(Index: Integer; Value: TWind);
function GetCount: integer; //This procedure?
public
constructor Create;
destructor Destroy; override;
function AddWind(Item: TWind): TWind; //Important, I want to add to Item, then can get Item[0].Cap
property Item[Index: Integer]: TWind read GetItem write SetItem; default;
property Count: integer read GetCount;
end;var
Form1: TForm1;implementation{$R *.dfm}{ TWindList }function TWindList.AddWind(Item: TWind): TWind;
begin
FList.Add(@Item);
end;constructor TWindList.Create;
begin
FList := TList.Create;
end;destructor TWindList.Destroy;
begin
FList.Free; inherited;
end;function TWindList.GetCount: integer;
begin
Result := FList.Count;
end;function TWindList.GetItem(Index: Integer): TWind;
var
P: PWind;
begin
if (Index >= 0) and (Index < Count) then
begin
P := FList[Index];
Result := P^;
end
else
ShowMessage('Index out of range!');
end;procedure TWindList.SetItem(Index: Integer; Value: TWind);
begin
if (Index >= 0) and (Index < Count) then
FList[Index] := @Value
else
ShowMessage('Index out of range!');
end;----------------------
procedure TForm1.Button3Click(Sender: TObject);
var mm:TWindList;
aa:TWind;
begin
mm:=TWindList.Create;
aa.Name :='aaa';
aa.Cap:='cap';
aa.Params[0].VpChar :=pchar('ok');
mm.AddCmd(aa);
showmessage(mm.Item[0].Cap);
mm.Free;
end;
Project Project2.exe raised exception class EAccessViolation with message 'Access violation at address 004042e8 in module 'Project2.exe'. Write of ...还有为什么
mm.Item[0].cap:='King';
出现这个错误: Left side cannot be assigned to
使用Record也行,但是不能像你那样,我将以你将TWnd改称Class,因为从你使用Record的方法看,我觉得你对如何使用Record不大明白,你是在用使用Class的方法来使用Record。在实现TWndList.AddCmd方法中,一定要创建TWnd实例,调用方法TWnd.Create。然后在实现TWndList.Destroy方法中,一定要销毁TWnd实例,调用方法TWnd.Free。