. NET Framework event Design Guidelines

Source: Internet
Author: User
Code

  
Namespace Csdemos. Model
{
Public Delegate Void Pubeventhandler ( Object Sender, pubeventargs E );

Public Class Publisher
{
Private String _ Name;

Public String Name
{
Get { Return _ Name ;}
Set {_ Name = Value ;}
}

Public Publisher ( String Name ){
This . _ Name = Name;
}

Public Event Pubeventhandler pubcomputer;
Public Event Pubeventhandler publife;

// Rewrite subclass
Protected Virtual Void Onpubcomputer (pubeventargs e ){ // Trigger event
If (Pubcomputer ! = Null )
{
// To avoid concurrent subscriber events that take too long to process.
Pubeventhandler pubcomputertmp = Pubcomputer;
Pubcomputertmp ( This , E );
}
}

Protected Virtual Void Onpublife (pubeventargs E)
{ // Trigger event
If (Publife ! = Null )
{
// To avoid concurrent subscriber events that take too long to process.
Pubeventhandler publifetmp = Publife;
Publifetmp ( This , E );
}
}

Public Void Dopubcomputer ( String Magazinename, datetime dtpubdate ){
Pubeventargs E = New Pubeventargs (magazinename, dtpubdate );
Onpubcomputer (E );
}


Public Void Dopublife ( String Magazinename, datetime dtpubdate)
{
Pubeventargs E = New Pubeventargs (magazinename, dtpubdate );
Onpublife (E );
}

}
}

 

 

Subscriber class:

 

 

Code

  Namespace  Csdemos. Model
{
Class Subscriber
{
Private String _ Name;
Public String Name
{
Get { Return _ Name ;}
Set {_ Name = Value ;}
}

Public Subscriber ( String Name)
{
This . _ Name = Name;
}

Public Void Receive ( Object Sender, pubeventargs E)
{

MessageBox. Show ( String . Format ( " {0} received at {1} {2} " , This . Name, E. pubdate. tostring (), E. Name ));

}
}
}

 

Eventargs:

Namespace csdemos. Model
{
Public class pubeventargs: eventargs
{
Private string _ name;

Public string name
{
Get {return _ name ;}
Set {_ name = value ;}
}
Private datetime _ pubdate;

Public datetime pubdate
{
Get {return _ pubdate ;}
Set {_ pubdate = value ;}
}

Public pubeventargs (string name, datetime DT)
{
This. _ name = Name;
This. _ pubdate = DT;
}

}
}

Client call:

Publisher pub = new publisher ("Xinhua press ");

Subscriber subzs = new subscriber ("James ");
Subscriber Suber = new subscriber ("Li Si ");

Pub. pubcomputer + = subzs. receive;
Pub. pubcomputer + = subls. receive;

Pub. publife + = subzs. receive;

 

Pub. dopubcomputer ("computer magazine", datetime. Now );
Pub. dopublife ("Life Magazine", datetime. Now );

 

 

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.