【问题标题】:Changing Delphi array range更改 Delphi 数组范围
【发布时间】:2018-06-19 15:28:31
【问题描述】:

我想知道是否有一种方法可以更改我的学校项目的数组范围。这是代码示例(我没有测试过,但我确信它会起作用):

procedure TfrmWelkom.LeesRekeningeIn(arrInLees : array [1..100,1..2] of string);
var
  iKol,iRy : Integer;
begin
  with dmAlleInlig do
  begin
    tblrekeninge.First;
    while not tblrekeninge.eof do
    begin
      for iKol := 1 to tblrekeninge.Eof do
        for iRy := 1 to 2 do
        begin
          arrInLees[iKol] := tblrekeninge['GebruikersNaam'];
          arrInLees[iRy] := tblrekeninge['Wagwoord'];
          tblrekeninge.Next;
        end;
    end;
  end;
end;

如您所见,我的数组范围是1..1001..2。我希望根据数据库中的信息量来更改 100。

希望你能理解。

如果您有更好的方法从 Access 数据库中读取信息,请分享....

我是 StackOverflow 的新手,如有错误请见谅。

【问题讨论】:

  • 使用动态数组
  • 这是相当标准的基本内容。 Google Delphi 开放数组和 Delphi 动态数组。
  • 正如@JerryDodge 所说:请不要发布指向您的代码图片的链接。从编辑器复制代码并将其粘贴到您的问题中(您可以编辑自己的问题)。要将其格式化为代码,只需选择它并在浏览器的问题编辑器中按{} 工具栏按钮。读者可以通过这种方式复制和检查代码。外部链接可能会过时(而且经常会过时)。问题中的代码不会。
  • @Dsm:什么是“Delphi 开放数组”?如果你的意思是开放数组 parameters 那就这么说吧。这样的“开放数组”不存在。动态数组当然可以。
  • procedure TfrmWelkom.LeesRekeningeIn(arrInLees : array [1..100,1..2] of string); 绝对不会工作。你不能定义这样的参数。您必须先定义一个类型。

标签: arrays database delphi


【解决方案1】:

你的代码有几个错误:

  • 您的数组参数使用了无法编译的非法声明:

    [DCC 错误] E2029 'OF' 预期但发现 '['

  • iKol := 1 to tblrekeninge.Eof do 不起作用,您可能打算改用iKol := 1 to 100 do

  • arrInLees[iKol] := ... 应改为 arrInLees[iKol][iRy] := ...。但实际上,您的整个内部 iRy 循环应该被完全删除,它没有正确填充数组。

试试这样的:

type
  TRekeningeArray = array [1..100, 1..2] of string;

function TfrmWelkom.LeesRekeningeIn(var arrInLees : TRekeningeArray): Integer;
var
  iKol : Integer;
begin
  with dmAlleInlig do
  begin
    tblrekeninge.First;
    iKol := 1;
    while (not tblrekeninge.Eof) and (iKol <= 100) do
    begin
      arrInLees[iKol][1] := tblrekeninge['GebruikersNaam'];
      arrInLees[iKol][2] := tblrekeninge['Wagwoord'];
      Inc(iKol);
      tblrekeninge.Next;
    end;
  end;
  Result := iKol - 1; // the actual number of records in the array
end;

然后您可以调整以使用动态数组而不是固定数组:

type
  TRekeninge = record
    GebruikersNaam : string;
    Wagwoord : string;
  end;
  TRekeningeArray = array of TRekeninge;

function TfrmWelkom.LeesRekeningeIn(var arrInLees : TRekeningeArray): Integer;
var
  iKol, iLengte : Integer;
begin
  with dmAlleInlig do
  begin
    tblrekeninge.First;
    iLengte := tblrekeninge.RecordCount;
    SelLength(arrInLees, iLengte);
    iKol := 0;
    while (not tblrekeninge.Eof) and (iKol < iLengte) do
    begin
      arrInLees[iKol].GebruikersNaam := tblrekeninge['GebruikersNaam'];
      arrInLees[iKol].Wagwoord := tblrekeninge['Wagwoord'];
      Inc(iKol);
      tblrekeninge.Next;
    end;
  end;
  Result := iKol;
end;

【讨论】:

  • 他或她没有使用开放数组,不在发布的代码中。您链接到我的文章,但参数与我在那里写的不匹配。
  • @RudyVelthuis 实际上,它确实与您文章中的某些内容相匹配-“混淆”部分中的注释:“注意:您不应该忘记在 Delphi 中,参数只能是使用类型规范声明,而不是使用类型声明。因此,以下形式参数(可能是类型声明)是不可能的:..."。它与您给出的第一个示例相匹配。我已经更新了我的答案。
  • 它与我文章中的“开放数组参数”不匹配。毕竟,你称它为开放数组。谢谢指正。
猜你喜欢
  • 2021-11-24
  • 2019-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-02
  • 2010-11-06
  • 1970-01-01
  • 2019-12-18
相关资源
最近更新 更多