如何在记录数组中按字段查找特定记录

Bob*_*der 2 delphi

我知道这可能是一个基本问题但是这里有.我有一个const数组记录定义:

  TDocumentKindInfo = record

    Employee: integer;
    First: string;
    Last: string;
    Title: string;
  end;

const
  CDocumentKindInfos: array[TDocumentKind] of TDocumentKindInfo = (

    (Emplyee: 1; First: 'Bob'; Last: 'Fredricks'; Title: 'Manager'),
    (Emplyee: 2; First: 'Bill'; Last: 'Evans'; Title: 'Cashier'),
    (Emplyee: 3; First: 'Jill'; Last: 'Dunne'; Title: 'Stocker'),
...
Run Code Online (Sandbox Code Playgroud)

如何找到作为经理的员工姓名.我环顾四周,找不到任何有帮助的东西.我是delphi的初学者.使用delphi 7.

Dav*_*nan 6

我通常会这样写:

function FindDocumentByTitle(const Title: string): TDocumentKind;
begin
  for Result := low(Result) to high(Result) do
    if SameText(Title, CDocumentKindInfos[Result].Title) then
      exit;
  raise EDocumentNotFound.CreateFmt('Document titled ''%s'' not found.', [Title]);
end;      
Run Code Online (Sandbox Code Playgroud)

如果找不到任何项目,该函数会引发错误.很快你就会想要把所有这些都包括在课堂上.