Events In C#
Events are nothing but situations at which the message is sent to an object to signal the occurrence of the action. These actions can be caused by the user interaction or within the object itself.
Events are declared using delegates. Delegate object encapsulates a method so that it can be called anonymously. An event is a way for a class to allow clients to give it delegates to methods that should be called when the event occurs. When the event occurs, the delegate(s) given to it by its clients are invoked. Example 1:
using System;
public delegate void CustomerDetails();
class Bank
{
string _custName;
int _custID;
event CustomerDetails Customer;
public Bank()
{
_custID = 1001;
_custName = "Patrick";
}
void ShowCustomerDetails()
{
Console.WriteLine("Customer ID : " + _custID);
Console.WriteLine("Customer Name : " + _custName);
}
static void Main(string [] args)
{
Bank objBank = new Bank();
objBank.Customer += new CustomerDetails(objBank.ShowCustomerDetails);
objBank.Customer();
}
}
using System;
public delegate void CustomerDetails();
public delegate void AccountDetails();
class Bank
{
string _custName;
String _accountType;
int _custID, _accountNumber;
event CustomerDetails Customer;
event AccountDetails Account;
public Bank()
{
_custID = 1001;
_accountNumber = 10;
_accountType = "Fixed";
_custName = "Patrick";
}
void ShowCustomerDetails()
{
Console.WriteLine("Customer ID : " + _custID);
Console.WriteLine("Customer Name : " + _custName);
}
void ShowAccountDetails()
{
Console.WriteLine("Account Number: " + _accountNumber);
Console.WriteLine("Account Type: " + _accountType);
}
static void Main(string [] args)
{
Bank objBank = new Bank();
objBank.Customer += new CustomerDetails(objBank.ShowCustomerDetails);
objBank.Account += new AccountDetails(objBank.ShowAccountDetails);
objBank.Customer();
objBank.Account();
}
}
using System;
public delegate void Display(string msg);
public class Parent
{
event Display Print;
protected void InvokeMethod()
{
Print += new Display(PrintMessage);
Check();
}
void Check()
{
if (Print != null)
{
PrintMessage("Welcome to C#");
}
}
void PrintMessage(string msg)
{
Console.WriteLine(msg);
}
}
class Child : Parent
{
static void Main(string[] args)
{
Child objChild = new Child();
objChild.InvokeMethod();
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication4
{
public delegate void Show();
abstract class AbstractEvents
{
abstract protected event Show Display;
abstract protected void SetDetails(string valueOne, int valueTwo);
}
class Employee : AbstractEvents
{
protected override event Show Display;
int _empID;
string _empName;
protected override void SetDetails(string valueOne, int valueTwo)
{
_empID = valueTwo;
_empName = valueOne;
}
void GetDetails()
{
Console.WriteLine("Employee ID: " + _empID);
Console.WriteLine("Employee Name: " + _empName);
}
static void Main(string[] args)
{
Employee objEmp = new Employee();
objEmp.SetDetails("John", 101);
objEmp.Display += new Show(objEmp.GetDetails);
objEmp.Display();
}
}
}
using System;
public delegate void GetInfo();
class Employee
{
int _empID = 10;
string _empName = "James" ;
protected virtual event GetInfo ShowDetails;
void GetDetails()
{
Console.WriteLine("Employee ID: " + _empID);
Console.WriteLine("Employee Name: " + _empName);
}
public void Invoke()
{
ShowDetails += new GetInfo(GetDetails);
ShowDetails();
}
}
class Department : Employee
{
string _departmentName = "Accounts";
protected sealed override event GetInfo ShowDetails;
void Display()
{
Console.WriteLine("Department Name: " + _departmentName);
}
public void NewInvoke()
{
ShowDetails += new GetInfo(Display);
ShowDetails();
}
}
class Salary : Department
{ //protected override event GetInfo ShowDetails;
void Show() {}
static void Main(string [] args)
{
Salary objSalary = new Salary();
objSalary.ShowDetails += new GetInfo(objSalary.Show);
objSalary.Invoke();
objSalary.NewInvoke();
}
}
Events are declared using delegates. Delegate object encapsulates a method so that it can be called anonymously. An event is a way for a class to allow clients to give it delegates to methods that should be called when the event occurs. When the event occurs, the delegate(s) given to it by its clients are invoked. Example 1:
using System;
public delegate void CustomerDetails();
class Bank
{
string _custName;
int _custID;
event CustomerDetails Customer;
public Bank()
{
_custID = 1001;
_custName = "Patrick";
}
void ShowCustomerDetails()
{
Console.WriteLine("Customer ID : " + _custID);
Console.WriteLine("Customer Name : " + _custName);
}
static void Main(string [] args)
{
Bank objBank = new Bank();
objBank.Customer += new CustomerDetails(objBank.ShowCustomerDetails);
objBank.Customer();
}
}
Custom Events
using System;
public delegate void CustomerDetails();
public delegate void AccountDetails();
class Bank
{
string _custName;
String _accountType;
int _custID, _accountNumber;
event CustomerDetails Customer;
event AccountDetails Account;
public Bank()
{
_custID = 1001;
_accountNumber = 10;
_accountType = "Fixed";
_custName = "Patrick";
}
void ShowCustomerDetails()
{
Console.WriteLine("Customer ID : " + _custID);
Console.WriteLine("Customer Name : " + _custName);
}
void ShowAccountDetails()
{
Console.WriteLine("Account Number: " + _accountNumber);
Console.WriteLine("Account Type: " + _accountType);
}
static void Main(string [] args)
{
Bank objBank = new Bank();
objBank.Customer += new CustomerDetails(objBank.ShowCustomerDetails);
objBank.Account += new AccountDetails(objBank.ShowAccountDetails);
objBank.Customer();
objBank.Account();
}
}
Events and Inheritance
using System;
public delegate void Display(string msg);
public class Parent
{
event Display Print;
protected void InvokeMethod()
{
Print += new Display(PrintMessage);
Check();
}
void Check()
{
if (Print != null)
{
PrintMessage("Welcome to C#");
}
}
void PrintMessage(string msg)
{
Console.WriteLine(msg);
}
}
class Child : Parent
{
static void Main(string[] args)
{
Child objChild = new Child();
objChild.InvokeMethod();
}
}
Using abstract and sealed Events
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication4
{
public delegate void Show();
abstract class AbstractEvents
{
abstract protected event Show Display;
abstract protected void SetDetails(string valueOne, int valueTwo);
}
class Employee : AbstractEvents
{
protected override event Show Display;
int _empID;
string _empName;
protected override void SetDetails(string valueOne, int valueTwo)
{
_empID = valueTwo;
_empName = valueOne;
}
void GetDetails()
{
Console.WriteLine("Employee ID: " + _empID);
Console.WriteLine("Employee Name: " + _empName);
}
static void Main(string[] args)
{
Employee objEmp = new Employee();
objEmp.SetDetails("John", 101);
objEmp.Display += new Show(objEmp.GetDetails);
objEmp.Display();
}
}
}
Sealed events cannot be inherited
using System;
public delegate void GetInfo();
class Employee
{
int _empID = 10;
string _empName = "James" ;
protected virtual event GetInfo ShowDetails;
void GetDetails()
{
Console.WriteLine("Employee ID: " + _empID);
Console.WriteLine("Employee Name: " + _empName);
}
public void Invoke()
{
ShowDetails += new GetInfo(GetDetails);
ShowDetails();
}
}
class Department : Employee
{
string _departmentName = "Accounts";
protected sealed override event GetInfo ShowDetails;
void Display()
{
Console.WriteLine("Department Name: " + _departmentName);
}
public void NewInvoke()
{
ShowDetails += new GetInfo(Display);
ShowDetails();
}
}
class Salary : Department
{ //protected override event GetInfo ShowDetails;
void Show() {}
static void Main(string [] args)
{
Salary objSalary = new Salary();
objSalary.ShowDetails += new GetInfo(objSalary.Show);
objSalary.Invoke();
objSalary.NewInvoke();
}
}
No comments:
Post a Comment