2017-02-27 1 views
-4

新しい言語を学習しようとしています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; 

デルファイで更新以下

は、私はこれは私のmyclass.pas

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. 

以下form11であるデルファイ

を達成したコードです。 pasはフォームコントロールを持っています: unit form11;

interface 

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

type 
    TForm1 = class(TForm) 
    Button1: TButton; 
    procedure Button1Click(Sender: TObject); 
    procedure MyClassEventHandler(Sender: TObject); 
    private 
    { Private declarations } 
    public 
    { Public declarations } 
    end; 

var 
    Form1: TForm1; 

implementation 

{$R *.dfm} 

procedure TForm1.MyClassEventHandler(Sender: TObject); 
begin 
    ShowMessage('Value is now below 6: ' + IntToStr(TMyClass(Sender).Value)); 
end; 

procedure TForm1.Button1Click(Sender: TObject); 
var 
    myclass: TMyClass; 
begin 
    myclass := TMyClass.Create(nil); 
    try 
    myclass.OnEvent := MyClassEventHandler; 
    myclass.Value := 9; // no event 
    myclass.Value := 3; // event 
    myclass.Value := 8; // no event; 

    // show it's current value 
    ShowMessage(IntToStr(myclass.Value)); 
    finally 
    myclass.Free(); 
    end; 

end. 

私はmyclass.pasに次のエラーを取得しています:

[エラー] myclass.pas(7):未定義の識別子: 'TComponentの'

[エラー] myclass.pas(10):宣言されていない識別子: 'TNotifyEvent'

[エラー] myclass.pas(16):静的メソッド

[エラー] myclass.pas(37)をオーバーライドすることはできません:互換性のない型

[エラー] myclass.pas(46): ';'期待されるが、すべての学習やグーグルの後に「END」は

に最終的なコードを見つけました:

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. 
+0

を? –

+0

こんにちは@DavidHeffernan、ikn Delphiの後に私のコードを見てください。私はいくつかのエラーに直面しています。私の質問は、Delphiでのイベントデリゲーションの使用です。私はC#でコードを行って、Delphiの助けが必要です! –

答えて

3

質問はイベントの委任に関連し、実際にはない、とC#のコードではありません関連性があります。ちょっとしたコンパイラエラーがあります。

エラーメッセージを読み、その内容を理解できるかどうかを確認してみましょう。

[エラー] myclass.pas(7):未定義の識別子: 'TComponentの'

あなたはTComponentを参照してくださいが、コンパイラは、その識別子を認識しません。何故なの?あなたがそれを定義するユニットを使用しなかったからです。

uses節に単位を追加して、コンパイラがこれらの記号を見つける必要があります。最初の2つのエラーを調べると、TComponentTNotifyEventClassesを使用する必要があります。その他のエラーは、TComponentがコンパイラで使用できないという結果になります。

あなたのユニットを次のように変更してください:あなたの質問は何ですか

unit myclass; 

interface 

uses 
    Classes; 

type 
.... 
関連する問題