Change uncertainty to OK ~ I want to monitor my objects. If it is a value, it is called another method to run automatically.

Source: Internet
Author: User

The name is not easy to understand, but I like this vernacular and do not like the translation of books!
Today, I want to solve an uncertain problem. The problem is that a program has an attribute. If it is true, I want to run other methods automatically, this can be achieved by subscribing to events, right? This is indeed the case after my tests.
The incident is a topic that has always been a headache for us. Why are our programmers so afraid of "events" that they don't need? I will analyze several reasons
1. I am not very familiar with my own concepts.
2. Its role may not be very clear. I think
3. I usually use less, so I am more unfamiliar with it.
Today, I will join you in learning another C # event.
If we talk about an event, we won't talk about delegation. What is the relationship between the two? In my opinion, delegation is a class, and events are an instance of this class, this makes it easy for everyone to understand.
An event consists of the event data source, the event type, and the event subscriber. "event subscriber" means that an event can be subscribed to by multiple subscriptions.
When the code is written, the code can best describe the problem:
Event source class:
/// <Summary>
/// Event Source
/// </Summary>
Internal class KeyEventArgs: EventArgs
{
Private char keyChar;
Public KeyEventArgs (char keyChar)
: Base ()
{
This. keyChar = keyChar;
}

Public char KeyChar
{
Get
{
Return keyChar;
}
}
}

All ends with EventArgs. EventArgs itself is the base class of all event source classes. It does not provide any event source information. If there is personalized event information, you need to derive it.
Next, let's take a look at the event class, where our events occur, and when and under which they all come from.
/// <Summary>
/// Event class
/// </Summary>
Internal class KeyInputMonitor
{
// Create a delegate. The return type is avoid and there are two parameters.
Public delegate void KeyDownEventHandler (object sender, KeyEventArgs e );
// Associate the created delegate with a specific event. The specific event here is OnKeyDown.
Public event KeyDownEventHandler OnKeyDown;

Public void Run ()
{
Bool finished = false;
Do
{
Console. WriteLine ("Input a char ");
String response = Console. ReadLine ();

Char responseChar = (response = "")? '': Char. ToUpper (response [0]);
Switch (responseChar)
{
Case 'X ':
Finished = true;
Break;
Default:
// Obtain the key information Parameters
KeyEventArgs keyEventArgs = new KeyEventArgs (responseChar );
// Trigger the event www.2cto.com
OnKeyDown (this, keyEventArgs );
Break;
}
} While (! Finished );
}
}
The function is to enter a character. When it is X, exit. If it is not X, an event is triggered. The event source data is "input character"
At this point, this event does not have any function, it is equivalent to selling things, things are already on the stage, but no one has bought them. Well, now it's time for a customer to come.
/// <Summary>
/// Display Chinese receiving classes
/// </Summary>
Internal class EventReceiverForChina
{
Public EventReceiverForChina (KeyInputMonitor monitor)
{
// Generate a delegate instance and add it to the event list generated by KeyInputMonitor
Monitor. OnKeyDown + = new KeyInputMonitor. KeyDownEventHandler (this. Echo );
}
Private void Echo (object sender, KeyEventArgs e)
{
Console. WriteLine ("the note you entered is: {0}", e. KeyChar );
}
}
When we subscribe to events here, we can use + =. In fact, it is to create a new event instance of the delegate type.
When calling at the front-end, you can do this:
// Instantiate an event sender and declare an EventReceiverForChina subscriber
KeyInputMonitor monitor = new KeyInputMonitor ();

EventReceiverForChina eventReceiverForChina = new EventReceiverForChina (monitor );
Monitor. Run ();
The running result is that when you go to Run (), some methods of the eventReceiverForChina type are also executed. How can we achieve our topic today, in fact, this is the event subscription mechanism, which is actually very useful in software development.
It should be noted that generally, the event return type is void. Of course, this is also normal, because the event is to do some events, and it does not know the consequences. Haha.
I wish you a good dream!

 

From Lose. zhang

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.