Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Understanding Events in C#

0.00/5 (No votes)
15 Oct 2012 1  
This article is to explain events in C# to begginers.

Introduction 

In this article let us understand events from a different perspective. We are familiar with passing data to an object from outside through constructors and public variables. How about passing statements to an object from outside? These statements are not defined inside the class. We can pass statements to a function defined inside the class by clever use of events.

Using the code

public void TargetFunction(string name)
{
    MessageBox.Show("hi ");
    MessageBox.Show(name); 
} 

Consider the above code. It has two statements to show the name. What about passing the second statement from outside the class?

First let's have this function inside the class EventDemo.

public class EventDemo
{   
    public void TargetFunction(string name)
    {
        MessageBox.Show("hi ");
    } 
}

Then let's declare a delegate CodeWrappingDelegate inside. 

public class EventDemo
{
    public delegate void CodeWrappingDelegate(string name); 
    public void TargetFunction(string name)
    {
        MessageBox.Show("hi ");
    } 
}

Now let's use an event CodeWrapping to wrap the delegate CodeWrappingDelegate

public class EventDemo
{
    public delegate void CodeWrappingDelegate(string name); 
    public event CodeWrappingDelegate CodeWrapping; 
    public void TargetFunction(string name)
    {
        MessageBox.Show("hi ");
    } 
}

Now let us call the event CodeWrapping inside the class EventDemo where we have to pass the statement from outside.

public class EventDemo
{
    public delegate void CodeWrappingDelegate(string name);
    public event CodeWrappingDelegate CodeWrapping;

    public void TargetFunction(string name)
    {
        MessageBox.Show("hi ");
        if (CodeWrapping!= null)
            CodeWrapping(name);
    }
}

Now let's use this class and create an object. And create a function ContainCode() and  place the statement which we have to pass from outside.

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
   
   
    private void ContainCode(string name)
    {
        MessageBox.Show(name);
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        EventDemo d = new EventDemo(); 
        
    }
}

Now just assign the function ContainCode() to the event.

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
   
   
    private void ContainCode(string name)
    {
        MessageBox.Show(name);
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        EventDemo d = new EventDemo();
        d.CodeWrapping+= ContainCode;
    }
}

Now we have successfully passed the statement MessageBox.Show(name); to the class EventDemo from outside.

Now finally call the function TargetFunction() in the object d.

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
   
    private void ContainCode(string name)
    {
        MessageBox.Show(name);
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        EventDemo  d= new EventDemo();
        d.CodeWrapping+= ContainCode;
        d.TargetFunction("nivas");
    }
}

public class EventDemo
{
    public delegate void CodeWrappingDelegate(string name);
    public event CodeWrappingDelegate CodeWrapping;

    public void TargetFunction(string name)
    {
        MessageBox.Show("hi ");
        if (CodeWrapping!= null)
            CodeWrapping(name);
    }
} 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here