用常规程序处理类事件(Delphi) [英] Handle class event with regular procedure (Delphi)

查看:323
本文介绍了用常规程序处理类事件(Delphi)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以编程方式在一个简单的过程中创建一个数据库连接对象(不是一个类中的一个方法)。

  mydb:= TUniConnection.Create(nil); 
mydb.Database:= knowledge_db_name;
mydb.LoginPrompt:= False;
mydb.Username:= aaa;
mydb.Password:= bbb;

现在我需要处理与其他过程的错误和断开连接。当我尝试做:

  mydb.OnError:= OnConnectionError; 
mydb.OnConnectionLost:= OnConnectionLost;

编译器告诉我

  [DCC错误] test.pas(373):E2009不兼容的类型:'方法指针和常规过程'

我该如何解决这个问题?
以下是事件过程的定义:

  procedure OnConnectionError(Sender:TObject; E:EDAError; var Fail :Boolean); 
procedure OnConnectionLost(Sender:TObject; Component:TComponent; ConnLostCause:TConnLostCause; var RetryMode:TRetryMode);


解决方案

如果您没有合适的课程事件处理程序在你可以定义一个虚拟类,并使事件处理程序类过程 s。那么你不必创建一个类的实例,但可以分配 mydb.OnError:= TMyDummyEventHandlerClass.OnConnectionError;



这是一个例子 - 我使用不同的事件,因为我没有 TUniConnection ,但希望确保一切编译。 :$)

  $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b //事件处理程序:
类过程OnBeforeOpen(发件人:TObject);
类程序OnAfterOpen(Sender:TObject);
结束

类程序TXMLEventHandlers.OnBeforeOpen(Sender:TObject);
begin
MessageBox(0,PChar(ClassName +'.OnBeforeOpen'),nil,0)
end;

类程序TXMLEventHandlers.OnAfterOpen(发件人:TObject);
begin
MessageBox(0,PChar(ClassName +'.OnAfterOpen'),nil,0)
end;

程序测试;
var
xml:TXMLDocument;
begin
xml:= TXMLDocument.Create(nil);
try
//注意:必须创建TXMLEventHandlers的实例:
xml.AfterOpen:= TXMLEventHandlers.OnAfterOpen;
xml.BeforeOpen:= TXMLEventHandlers.OnBeforeOpen;

xml.Active:= True; //调用两个事件处理程序
finally
xml.Free;
结束
结束


I am programmatically creating a database connection object inside a simple procedure (not a method in a class).

mydb:= TUniConnection.Create(nil);
mydb.Database:= knowledge_db_name;
mydb.LoginPrompt:= False;
mydb.Username:= aaa;
mydb.Password:= bbb;

now I need to handle errors and disconnections with other procedures. When I try to do:

mydb.OnError:= OnConnectionError;
mydb.OnConnectionLost:= OnConnectionLost;

The compiler tells me

[DCC Error] test.pas(373): E2009 Incompatible types: 'method pointer and regular procedure'

How can I work around this? Here are the definitions of the event procedures:

procedure OnConnectionError(Sender: TObject; E: EDAError; var Fail: Boolean);
procedure OnConnectionLost(Sender: TObject; Component: TComponent; ConnLostCause: TConnLostCause; var RetryMode: TRetryMode);

解决方案

If you don't have a suitable class to put the event handlers in you can define a dummy class and make the event handlers class procedures. Then you don't have to create an instance of the class but can assign mydb.OnError:= TMyDummyEventHandlerClass.OnConnectionError;.

Here is an example - I use different events because I don't have TUniConnection but want to be sure everything compiles. :-)

type
  // Dummy class to hold event handlers:
  TXMLEventHandlers = class
    // Event handlers:
    class procedure OnBeforeOpen(Sender: TObject);
    class procedure OnAfterOpen(Sender: TObject);
  end;

class procedure TXMLEventHandlers.OnBeforeOpen(Sender: TObject);
begin
  MessageBox(0, PChar(ClassName + '.OnBeforeOpen'), nil, 0)
end;

class procedure TXMLEventHandlers.OnAfterOpen(Sender: TObject);
begin
  MessageBox(0, PChar(ClassName + '.OnAfterOpen'), nil, 0)
end;

procedure Test;
var
  xml: TXMLDocument;
begin
  xml := TXMLDocument.Create(nil);
  try
    // Note: No instance of `TXMLEventHandlers` must be created:
    xml.AfterOpen := TXMLEventHandlers.OnAfterOpen;
    xml.BeforeOpen := TXMLEventHandlers.OnBeforeOpen;

    xml.Active := True; // Calls the two event handlers
  finally
    xml.Free;
  end;
end;

这篇关于用常规程序处理类事件(Delphi)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆