Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Delphi中的事件委托_C#_Delphi - Fatal编程技术网

C# Delphi中的事件委托

C# Delphi中的事件委托,c#,delphi,C#,Delphi,由于我正试图学习一门新语言Delphi,我需要一些帮助 可能的技术,但不知道如何实施: 以下是Delphi中事件委派的用法: type TMyProcEvent = procedure(const AIdent: string; const AValue: Integer) of object; TMyFuncEvent = function(const ANumber: Integer): Integer of object; 在类中,可以添加DoEvent(为适当的事件重命名)。因

由于我正试图学习一门新语言Delphi,我需要一些帮助

可能的技术,但不知道如何实施:

以下是Delphi中事件委派的用法:

type
  TMyProcEvent = procedure(const AIdent: string; const AValue: Integer) of object;
  TMyFuncEvent = function(const ANumber: Integer): Integer of object;
在类中,可以添加DoEvent(为适当的事件重命名)。因此,您可以在内部调用DoEvent。DoEvent处理未分配事件的可能性

type
  TMyClass = class
  private
    FMyProcEvent : TMyProcEvent;
    FMyFuncEvent : TMyFuncEvent;
  protected
procedure DoMyProcEvent(const AIdent: string; const AValue: Integer);
function DoMyFuncEvent(const ANumber: Integer): Integer;

  public
    property MyProcEvent: TMyProcEvent read FMyProcEvent write FMyProcEvent;
    property MyFuncEvent: TMyFuncEvent read FMyFuncEvent write FMyFuncEvent;
  end;

procedure TMyClass.DoMyProcEvent(const AIdent: string; const AValue:   Integer);
begin
  if Assigned(FMyProcEvent) then
    FMyProcEvent(AIdent, AValue);
  // Possibly add more general or default code.
end;


function TMyClass.DoMyFuncEvent(const ANumber: Integer): Integer;
begin
  if Assigned(FMyFuncEvent) then
    Result := FMyFuncEvent(ANumber)
  else
    Result := cNotAssignedValue;
end;
在Delphi中更新

下面是我在Delphi中实现的代码

这是我的班级

unit myclass;

interface

type
  TEvent1 = procedure() of Object;
  TMyClass = class(TComponent)
    private
 FValue: Integer;
 FEvent: TNotifyEvent; // could be TEvent1 aswell
 procedure SetValue(const AValue: Integer);
 function GetValue(): Integer;
  protected
 procedure DoFireEvent();
  public
 constructor Create(AOwner: TComponent); override;
 property Value: Integer read GetValue write SetValue;
  published
 property OnEvent: TNotifyEvent read FEvent write FEvent;
  end;

implementation

procedure TMyClass.SetValue(const AValue: Integer);
begin
  FValue := AValue;
  if AValue < 6 then DoFireEvent();
end;

function TMyClass.GetValue(): Integer;
begin
  Result := FValue;
end;

procedure TMyClass.DoFireEvent();
begin
  if Assigned(FEvent) then FEvent(Self);
end;

constructor TMyClass.Create(AOwner: TComponent); 
begin
  FEvent := nil;
  FValue := 10;
end

end.
我在myclass.pas中遇到以下错误:

[Error]myclass.pas(7):未声明的标识符:“TComponent”

[Error]myclass.pas(10):未声明的标识符:“TNotifyEvent”

[Error]myclass.pas(16):无法重写静态方法

[Error]myclass.pas(37):不兼容类型

[Error]myclass.pas(46):“;”预期但找到“结束”

学习和搜索之后的最终代码:

unit myclass;

interface

uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

    type
  MyInterfce = procedure (var AValue: Integer; var isbreak: Boolean) of object;

  TMyClass = class
  private
    OnEvent: MyInterfce;
  public
     constructor Create();

     procedure DoFireEvent();
  published

    property MyEvent  :  MyInterfce read OnEvent write OnEvent;
  end;

implementation


procedure TMyClass.DoFireEvent();
var i : integer;
isbreaked: boolean;
begin
  isbreaked:= false;
  for i :=0 to 5 do
  begin
    if assigned (MyEvent) then MyEvent(i,isbreaked);
    Application.ProcessMessages;
    if  isbreaked = true then break;
    Sleep(1000);
  end;
  //ShowMessage(IntToStr(integer(i)));
    //for i :=0 to 20 do
  //FEvent := IntToStr(i);
end;

constructor TMyClass.Create();
begin
;
end;

end.


unit form11;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,myclass;

type
 TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure MyClassEventHandler(var value:Integer; var isbreak: Boolean);
procedure Button2Click(Sender: TObject);
  private
{ Private declarations }
  public
{ Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.MyClassEventHandler(var value:Integer;var isbreak:     Boolean);
begin
    Edit1.Text:=IntToStr(value);

end;

procedure TForm1.Button1Click(Sender: TObject);
var
  myclass: TMyClass;
begin
  myclass := TMyClass.Create();
  try
myclass.MyEvent := MyClassEventHandler;
myclass.DoFireEvent();
  finally
    myclass.Free();
  end;

end;

procedure TForm1.Button2Click(Sender: TObject);
var
  myclass: TMyClass;
begin
  myclass.FreeInstance();
  end;

end.

这个问题实际上与活动授权无关,C#code也不相关。您刚刚遇到一些平庸的编译器错误

让我们阅读错误消息,看看是否能理解它们所说的内容

[错误]myclass.pas(7):未声明的标识符:“TComponent”

您引用了
t组件
,但编译器无法识别该标识符。为什么不呢?因为您没有使用定义它的单位

您需要将单位添加到
uses
子句中,以便编译器可以找到这些符号。查看前两个错误,
t组件
t通知事件
要求您使用
。其他错误只是
t组件
不可用于编译器的结果

对您的单元进行以下更改:

unit myclass;

interface

uses
  Classes;

type
....

您的问题是什么?您好@DavidHeffernan,请在更新ikn Delphi后查看我的代码。我面临着一些错误。我的问题是在Delphi中使用事件委托。我已经用C#编写了代码,需要Delphi的帮助!
unit myclass;

interface

uses
  Classes;

type
....