繁体   English   中英

如何传递数据集:TDataSet作为过程参数

[英]How to pass DataSet: TDataSet as procedure parameter

我正在设置一个新的过程,该过程将在执行查询后显示一条消息。 我正在使用“ 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变量,什么是我必须作为第一个参数传递给过程的?

如果它附加到事件,则是触发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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM