SunQuest
 
       Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 3 - Understanding Custom Events using Visual B...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
VeriSign Whitepapers 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC.NET

Understanding Custom Events using Visual Basic.NET 2005
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 9
    2007-08-08

    Table of Contents:
  • Understanding Custom Events using Visual Basic.NET 2005
  • A sample class without any event declarations: testing the class
  • Rewriting the sample class with eventing in Visual Basic 2005: coding the class
  • Rewriting the sample class with eventing in Visual Basic 2005: testing the class
  • Handling events dynamically in Visual Basic 2005

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Understanding Custom Events using Visual Basic.NET 2005 - Rewriting the sample class with eventing in Visual Basic 2005: coding the class


    (Page 3 of 5 )

    The following is the class which is rewritten to support events available in Visual Basic 2005.  Before trying to understand events, let us go through the following class:

    Public Class Sample01

      Private _x As Integer

      Private _y As Integer

     

      Public Event CalculationStatus(ByVal Msg As String)

     

      Public Property X() As Integer

        Get

          Return _x

        End Get

        Set(ByVal value As Integer)

          _x = value

        End Set

      End Property

     

      Public Property Y() As Integer

        Get

          Return _y

        End Get

        Set(ByVal value As Integer)

          _y = value

        End Set

      End Property

     

      Public Function GetCalculatedValue() As Double

        If Me.Y <> 0 Then

          Dim v As Double = Me.X / Me.Y

          RaiseEvent CalculationStatus("Successful")

          Return v

        Else 'division by zero

          RaiseEvent CalculationStatus("Division by zero is not permitted")

          Return 0

        End If

      End Function

     

    End Class

    Let us try to understand the above class.  The following is a new statement in the above class:

    Public Event CalculationStatus(ByVal Msg As String)

    The above declaration says that "CalculationStatus" is an event (part of "Sample01" class) which may be necessary to handle at the client level (say Windows Form).  It carries a message with the "Msg" parameter.

    Every event declared within the class must be fired to notify the client (say Windows Form) to handle the latest event.  If the event is not fired, the client would never know about its message or progress.  To fire an event, "RaiseEvent" will be used.  In the above class, we are firing two times in various situations (as part of an "if" condition).

    If you drag a button onto Windows Form, you can observe that it has lots of events to handle.  But mostly you will handle only the "Click" event.  Even the "Click" event is not compulsory to handle. If you don't handle any events, the button simply does nothing.  Similarly, with our class, when instantiated, you have the opportunity to handle the "CalculationStatus" event (which is still not compulsory).  If the Windows Form handles the event, it gives out the message; otherwise, it does not.

    In the next section we try to test the above class with a Windows Form.

    More Visual Basic.NET Articles
    More By Jagadish Chaterjee


       · Hello guys,This is my new article on "Events in Visual Basic 2005". This is...
       · Event handler methods take two parameters. The first is of type System..::.Object...
     

    VISUAL BASIC.NET ARTICLES

    - Working with Classes and Properties for Game...
    - Working with Loops, Arrays, and Collections ...
    - Learning Loops in VB.NET for Game Development
    - Learning VB.NET: Working with Variables, Con...
    - The Basics of VB.NET Through Text Game Devel...
    - Learning VB.NET Through Text Game Development
    - Types of Operators in Visual Basic
    - Operators
    - Understanding Custom Events using Visual Bas...
    - Polymorphism using Abstract Classes in Visua...
    - Shadowing using Shadows in Visual Basic.NET ...
    - Overloading and Overriding in Visual Basic.N...
    - More on Controlling Windows Fax Services Usi...
    - Programmatically Controlling Windows Fax Ser...
    - Focusing on Forms and Menus in Visual Basic





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway