Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
/*
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
Comments
dimpledevani 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 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 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

First version: just the console output:
C#
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
C#
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.
C#
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() + ".");
    }
}
 
Share this answer
 
Comments
unscathed18 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 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900