ARTICLE

Static Event Binding in VB.NET

Posted by Munesh Sharma Articles | VB.NET Articles March 25, 2011
In this article we will describe Static Event Binding
 
Reader Level:

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

 

 

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor