Pages

Sunday, March 18, 2012

Events in .net with example


An event is a message sent by an object to signal the occurrence of an action. The action could be caused by user interaction, such as a mouse click, or it could be triggered by some other program logic. The object that raises the event is called the event sender. The object that captures the event and responds to it is called the event receiver.
In event communication, the event sender class does not know which object or method will receive (handle) the events it raises. What is needed is an intermediary (or pointer-like mechanism) between the source and the receiver. The .NET Framework defines a special type (Delegate) that provides the functionality of a function pointer.
The Observer pattern is one of the most well known patterns used in software engineering. This pattern states that a non-determined number of business entities – called subscribers – are interested in a change of state of another business entity – called publisher – and that the publisher does not care who these subscribers are or what and why they are concerned with its change of state.
Let me explain you this by small example.
Public Class FrmCar
    ' Declare GetCarDetails Delegate
    Public Delegate Sub GetCarDetails(ByVal Sender As Object, ByVal e As GetCarEventArgs)
    Private Sub BtnShowDetails_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnShowDetails.Click
        '   Instance of Interface ICarDetails
        Dim CarDetails As ICarDetails
        Dim CarImplementation As CarDetailsImplementation
        '   According to the selected value in combobox. Value in switch case will execute.
        Select Case Convert.ToString(cmbCar.SelectedItem.ToString())
            Case "Maruthi"
                '   Assigning maruti_swift class to ICarDetails interface object.
                CarDetails = New maruti_swift()
                '   initializing CarDetailsImplementation class object and sending the interface object as parameter.
                CarImplementation = New CarDetailsImplementation(CarDetails)
                '   Registering the maruti swift car values.
                CarDetails.RegisterCar("maruti swift", "2010", "Red")
            Case "Hyundai"
                '   Assigning maruti_swift class to ICarDetails interface object.
                CarDetails = New hyundai_santro()
                '   initializing CarDetailsImplementation class object and sending the interface object as parameter.
                CarImplementation = New CarDetailsImplementation(CarDetails)
                '   Registering the hyundai santro car values.
                CarDetails.RegisterCar("hyundai santro", "2011", "Black")
            Case "Skoda"
                '   Assigning maruti_swift class to ICarDetails interface object.
                CarDetails = New skoda_fabia()
                '   initializing CarDetailsImplementation class object and sending the interface object as parameter.
                CarImplementation = New CarDetailsImplementation(CarDetails)
                '   Registering the skoda fabia car values.
                CarDetails.RegisterCar("skoda fabia", "2012", "White")
            Case Else
                '   Null handling
                CarDetails = New NullHandler()
                CarImplementation = New CarDetailsImplementation(CarDetails)
        End Select
    End Sub
End Class

Public Class CarDetailsImplementation
    ''' Constructor of class where NewCar event is handled.   
    Public Sub New(ByVal obj As ICarDetails)
        AddHandler obj.NewCar, AddressOf ImplementCar
    End Sub

    ''' In ImplementCar function the logic of the particular class is implemented
    Private Sub ImplementCar(ByVal Sender As Object, ByVal e As GetCarEventArgs)
        MessageBox.Show("Car Info: Name :- " + e.CarName + " Model No. :- " + e.Model + " Color:- " + e.Color)
    End Sub
End Class

''' Maruti swift Car is Register with its value and NewCar event is raised.
''' The Raise event will call CarDetailsImplementation class and their the logic is implemented.

Public Class maruti_swift
    Implements ICarDetails
    Public Event NewCar(ByVal Sender As Object, ByVal e As GetCarEventArgs) Implements ICarDetails.NewCar

    Public Sub onNewCar(ByVal e As GetCarEventArgs) Implements ICarDetails.onNewCar
        RaiseEvent NewCar(Me, e)
    End Sub

    Public Sub RegisterCar(ByVal CarName As String, ByVal Model As String, ByVal Color As String) Implements ICarDetails.RegisterCar
        Dim e As New GetCarEventArgs(CarName, Model, Color)
        onNewCar(e)
    End Sub
End Class

''' Skoda Fabia Car is Register with its value and NewCar event is raised.
''' The Raise event will call CarDetailsImplementation class and their the logic is implemented.
Public Class skoda_fabia
    Implements ICarDetails
    Public Event NewCar(ByVal Sender As Object, ByVal e As GetCarEventArgs) Implements ICarDetails.NewCar

    Public Sub onNewCar(ByVal e As GetCarEventArgs) Implements ICarDetails.onNewCar
        RaiseEvent NewCar(Me, e)
    End Sub

    Public Sub RegisterCar(ByVal CarName As String, ByVal Model As String, ByVal Color As String) Implements ICarDetails.RegisterCar
        Dim e As New GetCarEventArgs(CarName, Model, Color)
        onNewCar(e)
    End Sub
End Class

''' Hyundai Santro Car is Register with its value and NewCar event is raised.
''' The Raise event will call CarDetailsImplementation class and their the logic is implemented.
Public Class hyundai_santro
    Implements ICarDetails

    Public Event NewCar(ByVal Sender As Object, ByVal e As GetCarEventArgs) Implements ICarDetails.NewCar

    Public Sub onNewCar(ByVal e As GetCarEventArgs) Implements ICarDetails.onNewCar
        RaiseEvent NewCar(Me, e)
    End Sub

    Public Sub RegisterCar(ByVal CarName As String, ByVal Model As String, ByVal Color As String) Implements ICarDetails.RegisterCar
        Dim e As New GetCarEventArgs(CarName, Model, Color)
        onNewCar(e)
    End Sub
End Class

''' Null handling.
Public Class NullHandler
    Implements ICarDetails

    Public Event NewCar(ByVal Sender As Object, ByVal e As GetCarEventArgs) Implements ICarDetails.NewCar

    Public Sub onNewCar(ByVal e As GetCarEventArgs) Implements ICarDetails.onNewCar

    End Sub

    Public Sub RegisterCar(ByVal CarName As String, ByVal Model As String, ByVal Color As String) Implements ICarDetails.RegisterCar

    End Sub
End Class






No comments: