Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 3 - Understanding Delegates using Visual Basic...
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 
Mobile Linux 
App Generation ROI 
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 Delegates using Visual Basic.NET 2005
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 9
    2008-10-08

    Table of Contents:
  • Understanding Delegates using Visual Basic.NET 2005
  • A simple introduction Continued
  • Delegates with parameters using Visual Basic 2005
  • Delegates to methods returning values using Visual Basic 2005
  • Callbacks using Delegates
  • Multicast Delegates

  • 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


    Understanding Delegates using Visual Basic.NET 2005 - Delegates with parameters using Visual Basic 2005


    (Page 3 of 6 )

    In my previous example, you were introduced to simple delegates. Now, I shall extend the same with parameters.

    To help us understand "delegates with parameters," I added a new class as follows:


    Public Class Sample02

    Private _x As Integer

    Private _y As Integer


    Public Sub New()


    End Sub


    Public Sub New(ByVal a As Integer, ByVal b As Integer)

    _x = a

    _y = b

    End Sub


    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 Sub Increment(ByVal IncrementValue As Integer)

    _x += IncrementValue

    _y += IncrementValue

    End Sub


    Public Sub Add()

    MessageBox.Show("Sum = " & (Me.X + Me.Y))

    End Sub


    Public Sub Multiply()

    MessageBox.Show("Product = " & (Me.X * Me.Y))

    End Sub

    End Class


    You can observe that a new method, "Increment" (which accepts a parameter), is added. Let us try to access it using delegate. The following is the code:


    'delegate with parameters

    Public Class Form4


    Delegate Sub Calculate()

    Delegate Sub IncreaseValues(ByVal value As Integer)


    Dim delegCalc As Calculate

    Dim delegIncrease As IncreaseValues


    Dim obj As New Sample02(10, 20)


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

    delegCalc = AddressOf obj.Add

    delegIncrease = AddressOf obj.Increment

    delegIncrease(100)

    delegCalc()

    End Sub


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

    delegCalc = AddressOf obj.Multiply

    delegIncrease = AddressOf obj.Increment

    delegIncrease(100)

    delegCalc()

    End Sub


    End Class


    Let us try to understand this step by step. Consider the following first:


    Delegate Sub IncreaseValues(ByVal value As Integer)


    "IncreaseValues" is a delegate type of class now. However, it only accepts  methods (which can be executed later) with just one parameter of type integer.


    Dim obj As New Sample02(10, 20)

    delegIncrease = AddressOf obj.Increment


    As the "Increment" method accepts a parameter of type integer, it can be assigned to the delegate object we declared earlier.

    Finally, the method gets executed with a value of 100 using the statement:


    delegIncrease(100)


    A delegate declaration can have multiple parameters, not only one. And further, those parameters could be of any type, from primitive data types to reference types!

    More Visual Basic.NET Articles
    More By Jagadish Chaterjee


       · Hi guys,This is my new article which goes in to my series of "OOPs in VB.NET". ...
     

    VISUAL BASIC.NET ARTICLES

    - LINQ to XML Programming Using Visual Basic.N...
    - Understanding Delegates using Visual Basic.N...
    - Create a Sudoku Puzzle Generator using VB.NET
    - 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 (F...
    - 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...

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT