How can I evoke an event by a simple change of a variable's value?

Could anybody explain me how to evoke an event method by a simple changing of a variable's value without explicit calling it.

For example:

******************************************************

void OnVariableChanged(...)
{
}

...
Statement1;
Statement2;
Statement3;

i = 3; // Now I want the method OnVariableChanged(...) be evoked
// without any explicit calling it

Statement4;
Statement5;
...


******************************************************




I try to understand the real purpose of the events but so far I have found only samples where the events are explicitly called. The following example was copied from the book "C# Professional Programming". The events seems to me unnecessary, because we can avoid them by the simple methods. As you can see, the events are explicitely called:


******************************************************


using System;
using System.Windows.Forms;

namespace Wrox.ProCSharp.AdvancedCSharp
{
class MainEntryPoint
{
static void Main()
{
UserInputMonitor inputMonitor = new UserInputMonitor();
MessageDisplayer inputProcessor = new MessageDisplayer(inputMonitor);
ManagersStaffMonitor mortimer = new ManagersStaffMonitor(inputMonitor);
inputMonitor.Run();
}
}

class UserInputMonitor
{
public delegate void UserRequest(object sender, UserRequestEventArgs e);

public event UserRequest OnUserRequest;

public void Run()
{
bool finished = false;
do
{
Console.WriteLine("Select preferred option:");
Console.WriteLine(" Request advertisement - hit A then return");
Console.WriteLine(" Request personal message from Mortimer - hit P then return");
Console.WriteLine(" Exit - hit X then return");
string response = Console.ReadLine();

char responseChar = (response == "") ? ' ' : char.ToUpper(response[0]);
switch(responseChar)
{
case 'X':
finished = true;
break;
case 'A':
OnUserRequest(this, new UserRequestEventArgs(RequestType.AdRequest));
break;
case 'P':
OnUserRequest(this, new UserRequestEventArgs(RequestType.PersonalMessageRequest));
break;
}
}
while (!finished);
}
}

enum RequestType {AdRequest, PersonalMessageRequest};

class UserRequestEventArgs : EventArgs
{
private RequestType request;

public UserRequestEventArgs(RequestType request)
: base()
{
this.request = request;
}

public RequestType Request
{
get
{
return request;
}
}
}

class MessageDisplayer
{
public MessageDisplayer(UserInputMonitor monitor)
{
monitor.OnUserRequest +=
new UserInputMonitor.UserRequest(UserRequestHandler);
}

protected void UserRequestHandler(object sender, UserRequestEventArgs e)
{
switch (e.Request)
{
case RequestType.AdRequest:
Console.WriteLine("Mortimer Phones is better than anyone else because all our software is written in C#!
");
break;
case RequestType.PersonalMessageRequest:
Console.WriteLine("Today Mortimer issued the following statement:" +
"
Nevermore!
");
break;
}
}
}

class ManagersStaffMonitor
{
public ManagersStaffMonitor(UserInputMonitor monitor)
{
monitor.OnUserRequest +=
new UserInputMonitor.UserRequest(UserRequestHandler);
}

protected void UserRequestHandler(object sender, UserRequestEventArgs e)
{
if (e.Request == RequestType.PersonalMessageRequest)
{
MessageBox.Show("Kaark!", "Mortimer says ...");
}
}
}
}

******************************************************




Comments

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion