Events in C #

Source: Internet
Author: User
Tags define try catch
/* Events are similar to exceptions, and they are all raised by objects. The difference, however, is that the exception is handled by a try catch block, and the event needs to be handled by the main law (event handler) that subscribes to it.
* and this function must match the method signature of the event's request, which is specified by a delegate.
* When an event is raised by an object, it starts executing its handler.
*/
Let's look at an example
Using System;
namespace me
{
public delegate void Agehandler (int x);
public class Person
{
int age;
Public event Agehandler ageadd;//defines an event with a defined delegate
public person (int num)
{
Age=num;
}
Public person (): this (0)
{
}
public int Age
{
Get
{
return age;
}
Set
{
if (value>age)//When setting age is older than now, triggering event
{
This. Ageadd (value-age);//pass to its parameters for the age of growth
}
Age=value;
}
}
}
public class Class1
{
public static void Main (string[] args)
{
Person P=new person (20);
P.ageadd+=new Agehandler (P_ageadd);//Ageadd event for person object P Tim
program, i.e. method P_ageadd
p.age=25;//Trigger Event
}
Event handlers for public static void P_ageadd (int x)//ageadd
{
Console.WriteLine ("Grew" +x+ "old");
}
}
}

/* Now run the program, will be in the console output "up to 5 years old"
* But we should find that many of the event handler parameters are as follows:
* private void Button1_Click (object sender, System.EventArgs e)
* Because this handler may handle more than one event, sender represents the object that triggered it
* E is the parameter that is transmitted by the event
* We'll write a program here
*/
Using System;
namespace me
{
Write a class that acts as an argument for our event passing
public class AgeEventArgs:System.EventArgs
{
int oldage;
int newage;
Public Ageeventargs (int a,int b)
{
Oldage=a;
Newage=b;
}
public int Oldage
{
Get
{
return oldage;
}
}
public int NewAge
{
Get
{
return newage;
}
}
public int Addage
{
Get
{
return newage-oldage;
}
}


}


Define a delegate, the second argument we define the type
public delegate void Agehandler (Object Sender,me.ageeventargs e);

public class Person
{
public string name;
int age;
public event Agehandler Ageadd;
public person (int num,string N)
{
Age=num;
Name=n;
}
Public person (): This (0, "No Name")
{
}
public int Age
{
Get
{
return age;
}
Set
{
if (value>age)
{
This. Ageadd (this,new Me.ageeventargs (age,value));
}
Age=value;
}
}

}
public class Class1
{
static void Main (string[] args)
{
Person p=new persons (20, "man");
P.ageadd+=new Agehandler (P_ageadd);
p.age=25;
}
static void P_ageadd (Object Sender,me.ageeventargs e)
{
Console.WriteLine ((person) sender). name);
Console.WriteLine (E.oldage);
Console.WriteLine (E.newage);
Console.WriteLine (E.addage);

}
}

}
This is a simple illustration of the use of events in C #




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.