Event & EXTENSION METHODS


protected void Page_Load(object sender, EventArgs e) { Button objC = new Button(); } protected void Number_Handler(object sender, CommandEventArgs e) { switch (e.CommandName) { case "NAT": LoadNumbers(0, 1); break; case "EVE": LoadNumbers(0, 2); break; case "ODD": LoadNumbers(1, 2); break; } } private void LoadNumbers(int stratNumber, int stepNumber) { ListBox1.Items.Clear(); for (int i = stratNumber; i <= 100; i = i + stepNumber) { ListBox1.Items.Add(i.ToString()); } } ================================================ EVENTS ====== what is an event? An action recognized by an object. Event_handler contains the logic to handle the action. protected void Button1_Click(object sender, EventArgs e) { Employee objEmp = new Employee(); objEmp.SalaryEvent += new Employee.SalaryDelegate(myhandler); objEmp.EmpName = "raju"; objEmp.Salary = 145000; Response.Write(objEmp.Salary); } void myhandler(string EmpName, int Salary) { Response.Write(EmpName + " Salary " + Salary.ToString() + " is invalid"); } ========== public class Employee { public delegate void SalaryDelegate(string EmpName, int Salary); private int _salary; public string EmpName; public event SalaryDelegate SalaryEvent; public int Salary { get { return _salary; } set { if (value <= 50000) { _salary = value; } else { SalaryEvent(EmpName, value); } } } } ================= namespace WebApplication17 { public class Employee { //public delegate void SalaryDelegate(string EmpName, int Salary); private int _salary; public string EmpName; public event EventHandler SalaryEvent; public int Salary { get { return _salary; } set { if (value <= 50000) { _salary = value; } else { SalaryEvent(this, new EventArgs()); //SalaryEvent(null, null); } } } } } =================== protected void Button1_Click(object sender, EventArgs e) { Employee objEmp = new Employee(); objEmp.SalaryEvent += new EventHandler(objEmp_SalaryEvent); objEmp.EmpName = "raju"; objEmp.Salary = 145000; Response.Write(objEmp.Salary); } void objEmp_SalaryEvent(object sender, EventArgs e) { Employee tmpEmp = sender as Employee; Response.Write(tmpEmp.EmpName + " salary is invalid" ); } =================== public class Employee { //public delegate void SalaryDelegate(string EmpName, int Salary); private int _salary; public string EmpName; public event EventHandler SalaryEvent; public int Salary { get { return _salary; } set { if (value <= 50000) { _salary = value; } else { SalaryEvent(null, new EmpEventData(this.EmpName, value)); } } } } public class EmpEventData : EventArgs { public string Name; public int salary; public EmpEventData(string pName, int psalary) { this.Name = pName; this.salary = psalary; } } =================== protected void Button1_Click(object sender, EventArgs e) { Employee objEmp = new Employee(); objEmp.SalaryEvent += new EventHandler(objEmp_SalaryEvent); objEmp.EmpName = "raju"; objEmp.Salary = 145000; Response.Write(objEmp.Salary); } void objEmp_SalaryEvent(object sender, EmpEventData e) { Response.Write(e.Name + " salar " + e.salary.ToString() + " is invalid"); } } =================== EXTENSION METHODS ================= 1) If no source code available 2) the class is sealed class but still you want to extend the functionalty of the clas then you can go for extentsion methods namespace StringExtender { public static class StringExtender { public static string ToProper(this string strValue) { StringBuilder sb = new StringBuilder(); string[] myWords = strValue.Split(' '); foreach (string st in myWords) { sb.Append(st.Substring(0, 1).ToUpper()); sb.Append(st.Substring(1).ToLower()); sb.Append(" "); } return sb.ToString(); } } } using StringExtender; namespace WebApplication17 { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { string st = "ravi kuMar KVS, gss Info TEch, Hyderabad"; Response.Write(st.ToProper()); } } }

No comments:

Post a Comment