Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · ASP.NET Newsletter Application · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Newsletter
Enter your email address to receive full length articles at Stardeveloper:


Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (41)
  ADO (16)
  ADO.NET (11)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)

Main Category  Other
  Website Maintenance (3)
Log In
UserName Or Email:

Password:

Auto-Login:

Hosted by Securewebs.com
 
Home : .NET : VB.NET : Gate to Delegates in VB.NET
 
Read full length articles at Stardeveloper using Twitter Follow on Twitter Facebook Facebook fan page Email Get Articles via Email RSS Get Articles via RSS Feed

Gate to Delegates in VB.NET

by Arun Nair.

Visual Basic .NET has not only extended the versatility of the Visual Basic language but also harnessed the power of the .NET framework, the new "Delegate" feature is one of them. So now you don't get the sneer of the self proclaimed C++ or Java programmers looking down on VB programmers as mere mortals!

More about Delegates, what is the closest you can think of a Delegate? Well a Delegate is an entity that is entrusted with the task of representation, assign or passing on information. In code sense, it means a Delegate is entrusted with a Method to report information back to it when a certain task(which the Method expects) is accomplished outside the Method's class.

Now does that sound familiar to you? Well, think Event Handlers and you got Delegates into the picture. Let me demystify, when a Mouse move event occurs its the Form_MouseMove method that is invoked. But who does the work of passing the invokation from the event to the method? Its the MouseEventHandler Delegate that is entrusted with representing the Form_MouseMove method and whose job is report back to Form_MouseMove when a MouseMove event occurs. Now delegates are Typesafe which means the Delegate can work with a Method or "callback" only if it has the same signature as the Delegate. Delegates form an integral part of the .NET framework and the more you know the better!

Now that I've hopefully clarified what a Delegate is, lets see an example of a Printer to give you a clearer picture. We'll create a Printer class which has basic attributes like the Name of the Printer and the Ink level in the cartridge. Now the Printer class has a delegate CartridgeEventHandler which is invoked by the InkLevel property when the level of ink in the cartridge passes through certain intervals. The delegate in turn invokes the Method it represents or is entrusted with dutifully passing on information to it.

Public Class Printer

  ' ** The attributes of the Printer
  Private m_sName As String ' ** Name of the Printer
  Private m_nInkLevelinCartridge As Short ' ** Ink Level in the cartridge

  ' ** Enumeration of the various ink states of the cartridge
  Public Enum CartridgeState
    EMPTY
    LOW
    HALF
    FULL
  End Enum 

  ' *** IMPORTANT
  ' Declare the Delegate CartridgeEventHandler which is capable of invoking a
  ' Method with the same signature, i.e any Method which accepts a CartridgeState
  ' enumeration as parameter and that returns a Void 

  Public Delegate Sub CartridgeEventHandler(ByVal nState As CartridgeState)
  Private m_dlgCEH As CartridgeEventHandler

  ' ** This method will be used by the Caller to pass the Delegate of the
  ' function,
  ' i.e the Delegate will carry the Address of the Method 
  Public Sub SetDlgRef(ByVal dlgCartridge As CartridgeEventHandler)
    m_dlgCEH = dlgCartridge
  End Sub

  Public Property Name() As String
    Get
      Return m_sName
    End Get

    Set(ByVal Value As String)
      If Value.Length = 0 Or Value.Length > 20 Then
        m_sName = "Not Specified"
      Else
        m_sName = Value
      End If
    End Set
  End Property

  '** Read or set the level of ink in the cartridge
  ' the range of the ink level is 0-100.       
  Public Property InkLevel() As Short
    Get
      Return m_nInkLevelinCartridge
    End Get

    Set(ByVal Value As Short)
      If Value < 0 Or Value > 100 Then
        m_nInkLevelinCartridge = 0
      Else
        m_nInkLevelinCartridge = Value
        ' ** Invoke the delegate and pass the state according to the ink level
        Select Case m_nInkLevelinCartridge
          Case 100                                                           
            m_dlgCEH.Invoke(CartridgeState.FULL)
          Case 45 To 55
            m_dlgCEH.Invoke(CartridgeState.HALF)
          Case 1 To 20
            m_dlgCEH.Invoke(CartridgeState.LOW)
          Case 0
            m_dlgCEH.Invoke(CartridgeState.EMPTY)
        End Select

      End If
    End Set
  End Property

End Class

 ( 1 Remaining ) Next

Comments/Questions ( Threads: 1, Comments: 1 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. calling of paint event of a form by clicking button control

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed.

 
© 1999 - 2010 Stardeveloper.com, All Rights Reserved.