Static Event Binding in VB.NET

In this article we will describe Static Event Binding.
  • 2869
 

Static Event Binding:

The Visual studio.NET use the static event binding you might to be wondering when you ask it to skeleton definitions for your event handler methods , so understanding this kind of event binding will be important when you working with an event driven application framework such as window forms or web forms.

Defining Static Event:

If You want to create  a new class named xyz to act as event listener using static event binding . you can define a class as an event listener by adding one or more fields with the WithEvent keyword: 

Public Class classname
    Private WithEvents event name As raiseevents classname
    public sub Methodname(byval arguments AS Integer,String,Decimal,etc)
         Handles eventname.Delegateobject 
    End Sub
     
    'other member omitted

End
Class

Coding for Static Event Binding:

Public Delegate Sub Transaction(ByVal amount As Integer)
Public
Class Bank
    Public Event Tran As Transaction
    Public Sub withdraw(ByVal amount As Integer)
        If (amount > 5000) Then
            RaiseEvent Tran(amount)
        Else
            Console.WriteLine("please enter 5000 to above amount"
        End If
    End Sub

End
Class
Public
Class Accountauditor1
    Private WithEvents account As Bank
    Public Sub handler1(ByVal amount As Integer) Handles account.Tran 
        MsgBox("handler first for withdraw")
        Console.WriteLine("Please enter amount want to withdraw")
        Dim x As Integer
        x = Int32.Parse(Console.ReadLine())
        Dim y As Integer
        y = amount - x
        Console.WriteLine("your amount is now" & y)  
    End Sub
    Public Sub New(ByVal source As Bank)
        Me.account = source    
    End Sub
  
End
Class
Public
Class Accountauditor2
    Private WithEvents account As Bank
    Public Sub handler2(ByVal amount As Integer) Handles account.Tran 
        MsgBox("handler second for deposit")
        Console.WriteLine("Please enter amount want to deposit")
        Dim x As Integer
        x = Int32.Parse(Console.ReadLine())
        Dim y As Integer
        y = amount + x
        Console.WriteLine("your amount is now" & y) 
        Exit Sub
 
    End Sub
    Public Sub New(ByVal source As Bank) 
        Me.account = source    
    End Sub
  
End
Class
Module
Test
    Sub Main()
        Dim account1 As New Bank
        Dim listner1 As New Accountauditor1(account1)
        Dim listner2 As New Accountauditor2(account1)
        Dim options As String
        Console.WriteLine("if you interset write yes other wise no"
        options = Console.ReadLine() 
        Dim x As Integer
 
        Do Until options = "no"
            Console.WriteLine("pleze enter your account blance"
            x = Int32.Parse(Console.ReadLine()) 
            account1.withdraw(x)
            Console.WriteLine("if you interset write yes other wise no"
            options = Console.ReadLine() 
        Loop
  
    End Sub

End
Module 

Output:

Output-of-static-event-bind.gif

© 2019 DotNetHeaven. All rights reserved.