Previous Topic Next topic Print topic


Delegates and Events - in COBOL and C#

Delegates and Events in Managed COBOL

delegate-id MsgArrivedEventHandler.
procedure division using by value msg as string. 
end delegate.

class-id a.
01 MsgArrivedEvent type MsgArrivedEventHandler event static.
    

method-id main static.
*> Combining delegates
    set MsgArrivedEvent to MsgArrivedEvent + method My_MsgArrivedEventCallback
       
    invoke run MsgArrivedEvent("Test message")
    set MsgArrivedEvent to MsgArrivedEvent - method My_MsgArrivedEventCallback

*> Attaching to an event
    attach method My_MsgArrivedEventCallback to MsgArrivedEvent
    invoke run MsgArrivedEvent("Test message 2")
end method.

method-id My_MsgArrivedEventCallback static.
procedure division using by value str as string.
    display str
end method.
end class.

Delegates and Events in C#

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); 
}
Previous Topic Next topic Print topic