このヘルプでは、COBOL、C#、VB.NET におけるデリゲートとイベントの使い方について説明します。
$set sourceformat(free)
$set ilusing "System"
delegate-id MsgArrivedEventHandler.
procedure division using by value messag as string.
end delegate.
class-id a.
01 MsgArrivedEvent type "MsgArrivedEventHandler" event static.
*> Delegates must be used with events in COBOL
method-id main static.
set MsgArrivedEvent to type Delegate::Combine
(
MsgArrivedEvent,
new MsgArrivedEventHandler(self::My_MsgArrivedEventCallback)
) as type MsgArrivedEventHandler
*> Throws exception if obj is null
invoke MsgArrivedEvent::Invoke("Test message")
set MsgArrivedEvent to type Delegate::Remove
(
MsgArrivedEvent,
new MsgArrivedEventHandler(self::My_MsgArrivedEventCallback)
) as type MsgArrivedEventHandler
invoke self::add_MsgArrivedEvent
(new MsgArrivedEventHandler(self::My_MsgArrivedEventCallback))
invoke MsgArrivedEvent::Invoke("Test message 2")
end method main.
method-id My_MsgArrivedEventCallback static.
procedure division using by value str as string.
display str
end method.
end class a.
delegate void MsgArrivedEventHandler(string message);
event MsgArrivedEventHandler MsgArrivedEvent;
// Delegates must be used with events in C#
MsgArrivedEvent +=
new MsgArrivedEventHandler(My_MsgArrivedEventCallback);
// Throws exception if obj is null
MsgArrivedEvent("Test message");
MsgArrivedEvent -=
new MsgArrivedEventHandler(My_MsgArrivedEventCallback);
using System.Windows.Forms;
Button MyButton = new Button();
MyButton.Click += new System.EventHandler(MyButton_Click);
private void MyButton_Click(object sender, System.EventArgs e)
{
MessageBox.Show(this, "Button was clicked", "Info",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
Delegate Sub MsgArrivedEventHandler(ByVal message As String)
Event MsgArrivedEvent As MsgArrivedEventHandler
' or to define an event which declares a delegate implicitly
Event MsgArrivedEvent(ByVal message As String)
AddHandler MsgArrivedEvent, AddressOf My_MsgArrivedCallback
' Won't throw an exception if obj is Nothing
RaiseEvent MsgArrivedEvent("Test message")
RemoveHandler MsgArrivedEvent, AddressOf My_MsgArrivedCallback
Imports System.Windows.Forms
Dim WithEvents MyButton As Button ' WithEvents can't be used on local variable
MyButton = New Button
Private Sub MyButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyButton.Click
MessageBox.Show(Me, "Button was clicked", "Info", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
これらの例の一部は、ハーディング大学コンピュータ サイエンス学部の Frank McCown 博士が作成したもので、クリエイティブ コモンズ ライセンスに基づいて使用が許可されています。