如何解决如何找出正在写入的记录的索引
我有变量
MyList : array [1..5] of TMyRecord;
type
TMyRecord = record
private
FSize : Integer;
procedure SetSize(const Value: Integer);
public
property Size : Integer read FSize write SetSize;
end;
在此变量中,我多次写入不同的索引。 如何确定在SetSize过程中写入记录的索引?
解决方法
在您的示例中,写入记录MyList
的代码可以选择写入哪个记录。例如。您可能有一些这样的代码:
MyList[2].Size := 31000;
您对SetSize()
的实现显然是:
procedure TMyRecord.SetSize(const Value: Integer);
begin
FSize := value;
end;
在SetSize()
过程中,不可能知道索引,而且我也不是很清楚为什么需要知道索引。实际上,记录甚至不知道(也不需要知道)它是MyList
只有在SetSize()
可以访问MyList
的情况下,它才能通过遍历MyList
寻找与其{{1 }}指针,例如:
Self
我不推荐这种方法,但是它是可能。
更好的解决方案是将所需的索引传递给procedure TMyRecord.SetSize(const Value: Integer);
var
index: Integer;
begin
for index := Low(MyList) to High(MyList) do
begin
if @MyList[index] = Self then
begin
// use index as needed...
break;
end;
end;
end;
。
您可以将其作为输入参数传递给SetSize()
属性,例如:
Size
再次,不是很干净,但是可能。
如果可能的话,我建议将索引存储为记录的成员,例如:
type
TMyRecord = record
private
FSize : Integer;
function GetSize(const Index: Integer): Integer;
procedure SetSize(const Index,Value: Integer);
public
property Size[Index: Integer] : Integer read GetSize write SetSize;
end;
var
MyList : array [1..5] of TMyRecord;
...
function TMyRecord.GetSize(const Index: Integer): Integer;
begin
// use Index as needed...
Result := FSize;
end;
procedure TMyRecord.SetSize(const Index,Value: Integer);
begin
// use Index as needed...
FSize := Value;
end;
...
MyList[SomeIndex].Size[SomeIndex] := 31000;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。