Delegates / Events
In vb.net & c# language Delegates / Events are differ. Code of Delegates / Events of vb.net & c# language.
____________________________________________________________________
VB.NET
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
C#
delegate void MsgArrivedEventHandler(string message);
event MsgArrivedEventHandler MsgArrivedEvent;
// Delegates must be used with events in C#
MsgArrivedEvent += new MsgArrivedEventHandler(My_MsgArrivedEventCallback);
MsgArrivedEvent("Test message"); // Throws exception if obj is null
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);
}
-------------------------------------------------------------------------------------------------
Note: This code applicable for both desktop and web application.
No comments:
Post a Comment