德尔福1 16位(是的它已经老了,但效果很好)
一些示例代码:
procedure TForm1.Button1Click(Sender: TObject);
var
SL: TStringList;
begin
SL := TStringList.Create;
SL.Sorted := True;
SL.Duplicates := dupIgnore;
SL.AddObject('A', TObject(100));
SL.AddObject('A', TObject(999));
ShowMessage(IntToStr(LongInt(SL.Objects[0]))); {A}
SL.Free;
end;
Run Code Online (Sandbox Code Playgroud)
我正在使用Object字段来存储longints(一个hack,是的,但它可以完成工作).无论如何,在上面的A行,我希望ShowMessage显示100,而不是显示999(即使设置了dupIgnore).我在这里错过了什么吗?或者它应该以这种方式工作(我希望stringlist忽略999)?
刚刚在Delphi 2009中测试 - 它显示100(根据Delphi 2009文档关于Duplicates和dupIgnore它应该显示100).
可能是Delphi 1的bug.
更新
@Sertac Akyuz:是的,这似乎是真的.Google表明,旧的Delphi版本具有以下TStringList.Add和TStringList.AddObject方法的实现:
function TStringList.Add(const S: string): integer;
begin
if not Sorted then
Result := FCount
else
if Find(S, Result) then
case Duplicates of
dupIgnore: Exit;
dupError: Error(SDuplicateString, 0);
end;
InsertItem(Result, S);
end;
function TStrings.AddObject(const S: string; AObject: TObject): Integer;
begin
Result := Add(S);
PutObject(Result, AObject);
end;
Run Code Online (Sandbox Code Playgroud)
目前(Delphi 2009)的实施是:
function TStringList.Add(const S: string): Integer;
begin
Result := AddObject(S, nil);
end;
function TStringList.AddObject(const S: string; AObject: TObject): Integer;
begin
if not Sorted then
Result := FCount
else
if Find(S, Result) then
case Duplicates of
dupIgnore: Exit;
dupError: Error(@SDuplicateString, 0);
end;
InsertItem(Result, S, AObject);
end;
Run Code Online (Sandbox Code Playgroud)
看到不同.旧的实现可能被视为错误(内存泄漏等)或未记录的允许行为.在任何情况下,当前的实现都没有问题.
| 归档时间: |
|
| 查看次数: |
1787 次 |
| 最近记录: |