Click here to Skip to main content
11,501,968 members (62,133 online)
Click here to Skip to main content

Tagged as

Understanding Events in C#

, 15 Oct 2012 CPOL 21.7K 17
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

johnyroyan

United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 1 Pin
Member 955352022-Apr-14 17:59
memberMember 955352022-Apr-14 17:59 
GeneralMy vote of 1 Pin
Member 955352022-Apr-14 17:57
memberMember 955352022-Apr-14 17:57 
GeneralMy vote of 2 Pin
chait30123-Dec-13 1:24
memberchait30123-Dec-13 1:24 
QuestionSuperb Pin
Behnam Seidali23-Sep-13 17:40
memberBehnam Seidali23-Sep-13 17:40 
GeneralBe careful with events.. Pin
jgauffin22-Oct-12 3:39
memberjgauffin22-Oct-12 3:39 
GeneralWaaaauhhhhhh...... Pin
Nivas Maran15-Oct-12 0:23
memberNivas Maran15-Oct-12 0:23 
Generalsuper Pin
varunsmani15-Oct-12 0:21
membervarunsmani15-Oct-12 0:21 
QuestionBetter variable names would make this much clearer! Pin
Daniel Abbatt14-Oct-12 20:44
memberDaniel Abbatt14-Oct-12 20:44 
GeneralMy vote of 1 Pin
Member 2254313-Oct-12 8:19
memberMember 2254313-Oct-12 8:19 
GeneralRe: My vote of 1 Pin
adriancs15-Oct-12 4:17
memberadriancs15-Oct-12 4:17 
GeneralRe: My vote of 1 Pin
Member 2254315-Oct-12 5:03
memberMember 2254315-Oct-12 5:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 15 Oct 2012
Article Copyright 2012 by johnyroyan
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid