Loading...

How to C# Static vs Instance Methods As Event Handlers

View: 539    Dowload: 0   Comment: 0   Post by: hanhga   Category: C / C++ / MFC   Fields: Other

Both static and instance methods can be used as event handlers in C#.

Static and instance methods/handlers are different from each other in one reason: When we use a static method as a handler, an event notification is received by a class

When an instance method is used as a handler, a specific object receives an event notification OR when there are multiple objects of a single class, each object of a class receives an event notification separately.

Each object of a class that is interested to receive an event notification must register/add their handler/method to an event individually.

In real world examples, most handlers are instance methods.

Example – 1, An instance method as an event handler:

using System;
namespace csharpAdvance
{
    // A void type delegate declration.
    delegate void ClickHandel();
 
    class CalculatorEvent
    {
        // Public event ClickTasks of delegate type is declared.
        public event ClickHandel ClickTasks;
 
        // Public void type method is declared.
        public void OnOperations()
        {
            if (ClickTasks != null)
                ClickTasks();
            else
                Console.WriteLine("Please register a handler to an event !");
        }
    }
    class Add
    {
        int Id;
 
        // 1 parameterized constructor.
        public Add(int id)
        {
            this.Id = id;
        }
 
        // An instance void method handler.
        public void Addition()
        {
            Console.WriteLine("An event is recieved by Object {0} ", this.Id);
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            CalculatorEvent calculatorEvent = new CalculatorEvent();
 
            Add obj1 = new Add(1);
            Add obj2 = new Add(2);
            Add obj3 = new Add(3);
            Add obj4 = new Add(4);
 
            /* The handler/method (Addition) of each object 
               is registered/added to a multicast event chain 
               individually.*/
            calculatorEvent.ClickTasks += obj1.Addition;
            calculatorEvent.ClickTasks += obj2.Addition;
            calculatorEvent.ClickTasks += obj3.Addition;
            calculatorEvent.ClickTasks += obj4.Addition;
 
            // Fire an event through its object reference.
            calculatorEvent.OnOperations();
            Console.ReadKey();
        }
    }
    /*  The Output will be:
      
        An event is recieved by Object 1
        An event is recieved by Object 2
        An event is recieved by Object 3
        An event is recieved by Object 4
      
     */
}

Example – 2, A static method as an event handler:

using System;
namespace csharpAdvance
{
    // A void type delegate declration.
    delegate void ClickHandel();
 
    class CalculatorEvent
    {
        // Public event ClickTasks of delegate type is declared.
        public event ClickHandel ClickTasks;
 
        // Public void type method is declared.
        public void OnOperations()
        {
            if (ClickTasks != null)
                ClickTasks();
            else
                Console.WriteLine("Please register a handler to an event !");
        }
    }
 
    class Add
    {
        // A static void method handler.
        public static void Addition()
        {
            Console.WriteLine("An event is recieved by a class.");
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            CalculatorEvent calculatorEvent = new CalculatorEvent();
 
            // The handler/method (Addition) is registered/added to an event.
            calculatorEvent.ClickTasks += Add.Addition;
 
            // Fire an event through its object reference.
            calculatorEvent.OnOperations();
            Console.ReadKey();
        }
    }
    /*  The Output will be:
      
        An event is recieved by a class.
      
     */
}

How to C# Static vs Instance Methods As Event Handlers

Both static and instance methods can be used as event handlers in C#

Posted on 12-09-2016 

Comment:

To comment you must be logged in members.

Files with category

  • Sound Player in Visual Studio C++ .NET

    Sound Player in Visual Studio C++ .NET

    View: 0    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: none

    This sound player project use the libZPlay from http://libzplay.sourceforge.net . Methods are called from library libzplay.dll with [InteropServices::DllImport("")]. The use of the library is relatively easy.

  • Quiz Management System in C++

    Quiz Management System in C++

    View: 5    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: none

    Hello World...!!! Though I am not an expert programmer but today i want to share my little knowledge of C/C++ programming by sharing this simple Quiz Management System with you people. I hope this may help some students in their mini...

  • General Quiz

    General Quiz

    View: 77    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: none

    This is one of my first WPF projects, and it may be used as a template for a quiz test. The code can be improved but I think its useful for the ones who don't have experience with WPF.

  • Calculating Average and Grade

    Calculating Average and Grade

    View: 25    Download: 2   Comment: 0

    Category: C / C++ / MFC     Fields: none

    Calculating Average and Grade is a small program in order to find the result of students in the class.

  • H.K.M.S SUPREME TYRES

    H.K.M.S SUPREME TYRES

    View: 12    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: none

    H.K.M.S SUPREME TYRES --------------------- Goals: --------- * Brand Master * Size Master * User Master * Customer Master * Purchase Form * Order Form * Bill Form Software is as follows: ------------------------------ * Microsoft Visual Studio 2013 *...

  • Cronpp - a C++ library for CRON expressions

    Cronpp - a C++ library for CRON expressions

    View: 12    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: Other

    A CRON expression is a string composed of six fields (in some implementation seven), separated by a whites space, representing a time schedule. CRON expressions are used in various job schedulers (such as the Linux job scheduler, the Quartz...

  • Departmental store management system

    Departmental store management system

    View: 19    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: none

    This is a simple project on departmental store management system developed in c++ programming language . This project helps to keep all the information details of the items available in departmental store and maintains the stock and prints the bill....

  • Volume of a Cone in C

    Volume of a Cone in C

    View: 23    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: none

    Code Features: 1. Find the Volume of a cone 2. Find the value of other properties of the cone 3. Teach user how to compute the volume of the cone manually Thanks to following for the code: John Carlo C. Nañoz, Dondon Bagasbas, Gabby Peñones

 
File suggestion for you
Loading...
File top downloads
Loading...
Loading...
Codetitle - library source code to share, download the file to the community
Copyright © 2018. All rights reserved. codetitle Develope by Vinagon .Ltd