我有一个TList动态填充的数据库,它包含一个 ID 和名称。
我需要知道的是如何在不使用循环TList的情况下通过提供 ID来搜索某个名称。for
without using
forloop
Unfortunately, that is exactly what you must use. A TList containing pointers only knows how to search for pointers, nothing else. To do what you are wanting, you must loop through the list dereferencing the pointers manually in order to compare their field values.
For example:
type
TDatabaseRecord = class
public
ID: Integer;
Name: String;
end;
function FindNameByID(ID: Integer): String;
var
I: Integer;
begin
Result := '';
for I := 0 to MyList.Count-1 do
begin
if TDatabaseRecord(MyList[I]).ID = ID then
begin
Result := TDatabaseRecord(MyList[I]).Name;
Exit;
end;
end;
end;