如下定义类型:
type
TValidFloor= 1..10;
TElevatorState= set of (esUpRun, esDownRun, esStop);
TTask= record
Floor: TValidFloor;
Direction: TElevatorState;
end;
TTaskList= array[1..10] of TTask;
然后定义类:
type
TBuilder = class
private
FRequestList: TTaskList;
public
procedure CreateTask(Task: TTask);
end;procedure TBuilder.CreateTask(Task: TTask);
begin
RequestList[2].Floor:= Task.Floor;
end;编译出错:Left side cannot be assigned to
该怎么办呀?
type
TValidFloor= 1..10;
TElevatorState= set of (esUpRun, esDownRun, esStop);
TTask= record
Floor: TValidFloor;
Direction: TElevatorState;
end;
TTaskList= array[1..10] of TTask;
然后定义类:
type
TBuilder = class
private
FRequestList: TTaskList;
public
procedure CreateTask(Task: TTask);
end;procedure TBuilder.CreateTask(Task: TTask);
begin
RequestList[2].Floor:= Task.Floor;
end;编译出错:Left side cannot be assigned to
该怎么办呀?
RequestList[2].Floor:= Task.Floor;
应该改为:
FRequestList[2].Floor:= Task.Floor;
property RequestList: TTaskList write FRequestList read FRequestList;
property RequestList: TTaskList write FRequestList read FRequestList;也就是RequesList指定是读写FRequesList,没说如何读写FRequesList的子项。
所以,你要改子项,必须用我上面说的方法,或者如下也可以。
var
R:TTaskList;
begin
R.Floor := Task.Floor;
RequestList[2] := R;
end;
如 King_Dragon(我是谁) 所说
你的属性不能这样定义。
可以做个改动,如下:type
TBuilder = class
private
FRequestList: TTaskList;
function GetRequestList(const Index: Integer): TTask;
procedure SetRequestList(const Index: Integer; Value: TTask);
public
procedure CreateTask(Task: TTask);
property RequestList[const Index: Integer]: TTask read
GetRequestList write SetRequestList;
end;procedure TBuilder.CreateTask(Task: TTask);
begin
FRequestList[2].Floor:= Task.Floor;
end;function GetRequestList(const Index: Integer): TTask;
begin
Result := FRequestList[Index];
end;procedure SetRequestList(const Index: Integer; Value: TTask);
begin
FRequestList[Index] := Value;
end;
如下这样使用
var
R:TTaskList;
begin
R.Floor := Task.Floor;
RequestList[2] := R;
end;