arrays - Tarray构造函数可以中止以不添加新记录吗?

标签 arrays delphi generics delphi-10.1-berlin

我试图控制,限制将多少新记录添加到数组中。这是一个简化的示例:

我正在尝试使用变量LimitToUSA,该变量将仅允许来自Country = US的开发人员在数组中创建:

type
  TDevelopers = record
    FName: string;
    LName: string;
    Country: string;
    constructor New(const aFName, aLName, aCountry: string);
  end;

var
  Developers:TArray<TDevelopers>;
  LimitToUSA: boolean; // <-- Controlling variable

constructor TDevelopers.New(const aFName, aLName, aCountry: string);
begin
  if LimitToUSA And (aCountry <> 'US') then
   Exit; // <-- Cancel adding new record here

  FName := aFName;
  LName := aLName;
  Country := aCountry;
end;

procedure TForm2.Button1Click(Sender: TObject);
begin
  LimitToUSA := True;

  Developers := TArray<TDevelopers>.Create(
    TDevelopers.New('John','Smith', 'US'),
    TDevelopers.New('Leo','Hazen', 'CA'),
    TDevelopers.New('Bob','Tilson','UK'),
    TDevelopers.New('Jennifer','Wolken','US'),
    TDevelopers.New('John','Willer','US'));
end;


问题在于它为非美国开发人员添加了空记录:

enter image description here

当您有50个开发人员时,此解决方案很难看,并且难以维护:

  if LimitToUSA then
  begin
    Developers := TArray<TDevelopers>.Create(
      TDevelopers.New('John','Smith', 'US'),
      TDevelopers.New('Jennifer','Wolken','US'),
      TDevelopers.New('John','Willer','US'));
  end
  else
  begin
    Developers := TArray<TDevelopers>.Create(
      TDevelopers.New('John','Smith', 'US'),
      TDevelopers.New('Leo','Hazen', 'CA'),
      TDevelopers.New('Bob','Tilson','UK'),
      TDevelopers.New('Jennifer','Wolken','US'),
      TDevelopers.New('John','Willer','US'));
  end;


是否可以通过构造函数根据条件取消添加新记录?

最佳答案

不,你不能那样做。您可以使用TList<TDevelopers>并仅添加有效的类实例:

type
  TDevelopers = record
    LName: string;
    FName: string;
    Country: string;
    constructor New(const aFName, aLName, aCountry: string);
  end;

const
  NumDevelopers = 5; // or whatever the real number is.
  ConstDevelopers: array[0..NumDevelopers - 1] of TDevelopers =
  (
    (LName: 'Smith'; FName: 'John'; Country: 'US'),
    (LName: 'Wolken'; FName: 'Jennifer'; Country: 'US'),
    (LName: 'Hazen'; FName: 'Leo'; Country: 'CA'),
    (LName: 'Tilson'; FName: 'Bob'; Country: 'UK'),
    (LName: 'Willer'; FName: 'John'; Country: 'US')
  );

...

var
  Developers: TList<TDevelopers>;
  I: Integer;
begin
  Developers := TList<TDevelopers>.Create;
  for I := 0 to High(ConstDevelopers) do
  begin
    if not LimitToUSA or (ConstDevelopers[I].Country = 'US') then
      Developers.Add(ConstDevelopers[I]);
  end;


现在,如果您确实需要一个数组,则可以使用ToArray。

请注意,我现在无法测试此代码(目前,我只能以安全模式启动此Mac,然后无法使用Windows VM启动Parallels)。但是它应该给您提示您可以做什么。

另类

另外,您可以保留代码,而只需删除所有空记录(检查Country = '')。在最新的Delphi版本(包括Berlin)中,您可以使用Delete从动态数组中删除,如Delete用作字符串。但是请倒退,否则您的索引会遇到麻烦:

for I := High(Developers) downto 0 do
  if Developers[I].Country = '' then
    Delete(Developers, I, 1);


选择2

为避免重复自己,您可以执行以下操作:

  Developers := TArray<TDevelopers>.Create(
    TDevelopers.New('John','Smith', 'US'),
    TDevelopers.New('Jennifer','Wolken','US'),
    TDevelopers.New('John','Willer','US'));
  if not LimitToUSA then
  begin
    ForeignDevelopers := TArray<TDevelopers>.Create(
      TDevelopers.New('Leo','Hazen', 'CA'),
      TDevelopers.New('Bob','Tilson','UK'));
    Developers := Developers + ForeignDevelopers;
  end;

关于arrays - Tarray构造函数可以中止以不添加新记录吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47639161/

相关文章:

delphi - Windows Vista 上的应用程序看不到 Shift+Ctrl+0 组合键

c# - 声明泛型类时 new() 的目的是什么?

java - 如何获取字段的泛型参数类

delphi - Chau Chee Yang 的 dbExpress 和 XE2 Enterprise dbExpress for Firebird 的兼容性如何?

Delphi 7 和 ICS 发送 DELETE 请求

php - 用巨大的数组填充表

javascript - 如何检查用户名是否相同? Json数组

typescript - 接口(interface)泛型的类型推断

php - 获取最常用的带有特殊字符的单词

c# - 如何从 C# 中的简单数组中获取元素?