Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 5 - Understanding Custom Events using Visual B...
Moblin
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 
Moblin 
JMSL Numerical Library 
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

    Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    Understanding Custom Events using Visual Basic.NET 2005 - Handling events dynamically in Visual Basic 2005


    (Page 5 of 5 )

    In previous section, if you observe the following statement, it is declared at the class level, which means that it is outside all the methods:

      WithEvents obj As New Sample01

    This may not be convenient in every situation as we do create a lot of objects and remove them every time, especially in loops. The "WithEvents" cannot be available as part of method in a class. It can only be available with class level variables/objects.

    To solve the above situation, the "AddHandler" comes to the rescue. Let us rewrite our testing class as follows:

    Public Class Form2

     

      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim obj As New Sample01

        AddHandler obj.CalculationStatus, AddressOf Msg_CalculationStatus

        obj.X = 10

        obj.Y = 5

        Me.lblValue.Text = obj.GetCalculatedValue

      End Sub

     

      Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Dim obj As New Sample01

        AddHandler obj.CalculationStatus, AddressOf Msg_CalculationStatus

        obj.X = 10

        obj.Y = 0

        Me.lblValue.Text = obj.GetCalculatedValue

      End Sub

     

      Public Sub Msg_CalculationStatus(ByVal Msg As String)

        Me.lblMsg.Text = Msg

      End Sub

    End Class

    From the above class, you can observe that no instance is declared at the class level.  The object instantiation is done separately in each method and they get automatically erased from memory once the execution completes.

    To attach a method to an object dynamically (to handle an event), we should use the "AddHandler" statement as follows:

        AddHandler obj.CalculationStatus, AddressOf Msg_CalculationStatus

    The above statement says that the "CalculationStatus" event of the "obj" object will be handled by the "Msg_CalculationStatus" method.  And further, the same method can handle any number of events, even when attached to several objects.  Make sure that the signature of this method matches with that of the event.

    Finally, you can dynamically remove the handler using "RemoveHandler," if it is already attached using "AddHandler".

    In my next article, I shall introduce Delegates in Visual Basic 2005.  I hope you enjoyed the article and any suggestions, bugs, errors, enhancements etc. are highly appreciated at http://jagchat.spaces.live.com


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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

    - Entity Creation and Messaging in a VB.NET Te...
    - Movement and Player Statistics in a VB.NET T...
    - Creating and Drawing a Game Map in VB.NET
    - 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...





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