【问题标题】:How to pass DataSet: TDataSet as procedure parameter如何传递 DataSet:TDataSet 作为过程参数
【发布时间】:2019-07-24 12:11:56
【问题描述】:

我正在设置一个新程序,该程序将在执行查询后显示一条消息。我正在使用“AfterOpen”事件,我必须传递“DataSet:TDataSet”参数。

procedure Tf_SeznamDluzniku.ShowInfoMessage(DataSet: TDataSet; info : string);
begin
  l_InfoMessage.Caption := info;
  img_success.Visible := True;
end;
query.AfterOpen := ShowInfoMessage(,'InfoMessage here')

谁能解释一下什么是 DataSet 变量以及我必须将什么作为第一个参数传递给程序?

【问题讨论】:

标签: delphi tdataset


【解决方案1】:

如果它附加到事件,它是触发 AfterOpen 事件的数据集。数据集本身将调用该过程,并将其自身传递给该参数。

但是您添加了 Info 参数,这使得该过程作为事件处理程序无效。您希望这些信息来自哪里?来自数据集?

因为它是一个事件处理程序,所以自己调用它是不好的做法。您可以这样做,只需传递nil(或特定数据集),因为它无论如何都不会被使用。但是您可能会遇到奇怪的情况,因为它看起来该方法只会在打开后被调用,但事实证明它也在其他场合被调用。 因此,最好创建一个单独的过程来执行您想要的操作,并从 AfterOpen 事件处理程序中调用它。您可以从数据集中传递信息,但您也可以从其他地方调用该过程,例如在打开数据集之前提供一些初始标题:

// The procedure doesn't need the dataset, only the info to set.
procedure Tf_SeznamDluzniku.ShowInfoMessage(Info : string);
begin
  l_InfoMessage.Caption := info;
end;

// The actual event handler for YourDataset.OnAfterOpen (you have to attach them)
// This can set the info, and/or maybe set the success indicator right away..
procedure Tf_SeznamDluzniku.YourDataSetAfterOpen(DataSet: TDataSet);
begin
  ShowInfoMessage(DataSet.FieldByName('info').AsString);
  img_success.Visible := True;
end;

// For demonstration, another event handler for showing the form, to put in some initial caption.
procedure Tf_SeznamDluzniku.FormShow(const Sender: TObject);
begin
  ShowInfoMessage('Loading...');
end;

【讨论】:

    猜你喜欢
    • 2017-01-27
    • 1970-01-01
    • 1970-01-01
    • 2020-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多