Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Events
Hi everyone. I am newly programmer of C# (junior) and I have run into a problem that I don't really know how to tackle. Although I have a notion on how the resolution has to be like:
 
/*
I want to create a program that through the use of events, it will notify me when a variable (int) is under the value of 30, the event raises and shows "Console.WriteLine()" showing that the variable has dropped to a low value.
In order to decrease the value of the variable (which I refer to as "i") I use a while loop.
*/
{
// Inside main
int i = 300;
while(i  > 10)
{
          i--;
}
// Event code to implement.
 
I have a slight idea about how events work but I don't really yet understand how to create them from scratch. If any charitable soul is willing to help me i'd be grateful.
Posted 18-Jul-12 0:57am
Comments
dimpledevani at 18-Jul-12 6:59am
   
Create a normal event, in your loop when you are decrementing the value of 'i' , then you can check if its 30 and if yes , you can call the event like you call methods from there
Erik Rude at 18-Jul-12 7:09am
   
You could make your integer i a member of a class. That class could have an event that is raised when the value of i is low.
unscathed18 at 18-Jul-12 7:46am
   
Yes, that's right. I'll try to get the hang out of it, but first I have to understand how it works.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
{
    // Define custom event
    public event EventHandler ValueLow;
 
    public void DoSomething()
    {
        for( int i = 300; i > 10; i--)
        {
            if( i < 30)
            {
                EventHandler eh = ValueLow;
                if( eh != null)
                {
                    // Fire custom event
                    eh(this, new EventArgs());
                }
            }
        }
    }
}
 
private static int Main()
{
    MyClass myClass = new MyClass();
 
    // Subscribe to custom event
    myClass.ValueLow += new EventHandler(MyClass_ValueLow);
 
    // Handle custom event
    private void MyClass_ValueLow(object sender, EventArgs e)
    {
        Console.WriteLine("Value is low.");
    }
}
You can actually transport valueable data within events.
public class MyClass
{
    // Define custom event
    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)
                {
                    // Fire custom event
                    eh(i, 30);
                }
            }
        }
    }
}
 
private static int Main()
{
    MyClass myClass = new MyClass();
 
    // Subscribe to custom event
    myClass.ValueLow += new ValueLowEventHandler(MyClass_ValueLow);
 
    // Handle custom event
    private void MyClass_ValueLow(int value, int threshold)
    {
        Console.WriteLine("Value is " + value.ToString() + " which is below " + threshold.ToString() + ".");
    }
}
  Permalink  
Comments
unscathed18 at 18-Jul-12 7:45am
   
Perfect! Thanks, that was what I was looking for. The example is clear and good to make a practice out of it.
lukeer at 19-Jul-12 2:07am
   
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 329
1 OriginalGriff 275
2 Richard MacCutchan 220
3 Jared Sanow 145
4 Vinay Mistry 143
0 Sergey Alexandrovich Kryukov 5,939
1 OriginalGriff 4,885
2 CPallini 2,473
3 Abhinav S 1,507
4 Richard MacCutchan 1,437


Advertise | Privacy | Mobile
Web01 | 2.8.140814.1 | Last Updated 18 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100