First version: just the console output:
private static int Main()
{
for( int i = 300; i > 10; i--)
{
if( i < 30)
{
Console.WriteLine("Value is below 30.");
}
}
}
But you actually wanted to create an event
public class MyClass
{
public event EventHandler ValueLow;
public void DoSomething()
{
for( int i = 300; i > 10; i--)
{
if( i < 30)
{
EventHandler eh = ValueLow;
if( eh != null)
{
eh(this, new EventArgs());
}
}
}
}
}
private static int Main()
{
MyClass myClass = new MyClass();
myClass.ValueLow += new EventHandler(MyClass_ValueLow);
private void MyClass_ValueLow(object sender, EventArgs e)
{
Console.WriteLine("Value is low.");
}
}
You can actually transport valueable data within events.
public class MyClass
{
public delegate void ValueLowEventHandler(int value, int threshold);
public event ValueLowEventHandler ValueLow;
public void DoSomething()
{
for( int i = 300; i > 10; i--)
{
if( i < 30)
{
EventHandler eh = ValueLow;
if( eh != null)
{
eh(i, 30);
}
}
}
}
}
private static int Main()
{
MyClass myClass = new MyClass();
myClass.ValueLow += new ValueLowEventHandler(MyClass_ValueLow);
private void MyClass_ValueLow(int value, int threshold)
{
Console.WriteLine("Value is " + value.ToString() + " which is below " + threshold.ToString() + ".");
}
}