ARTICLE

Overloading in VB.NET

Posted by Manish Tewatia Articles | Visual Basic Language August 16, 2010
In this article I will explain you about overloading in VB.NET.
 
Reader Level:

VB.NET allows user-defined types to overload operators by defining static member functions using the operator keyword. The operator keyword is used to declare an operator in a class or struct declaration. Not all operators can be overloaded, and some that can be overloaded have certain restrictions, as listed in the given below Table.

Arithmetic Operators Concatenation Operators Comparison Operators Logical / Bitwise Operators
^ + = Not
- & <> And
*   < AndAlso
/   > Or
\   >= OrElse
mod   <= Xor
+      
-      

The given below example illustrates overloading on complex numbers.

Example of Operator Overloading 

    Imports System
    Public Class Complex
        Public real As Integer = 0
        Public imaginary As Integer = 0

        Public Sub New(ByVal real As Integer, ByVal imaginary As Integer)
            Me.real = real
            Me.imaginary = imaginary
        End Sub
        Public
Shared Operator +(ByVal c1 As Complex, ByVal c2 As Complex) As Complex
            Return New Complex(c1.real + c2.real, c1.imaginary + c2.imaginary)
        End Operator

        Public
Shared Sub Main()
            Dim num1 As New Complex(2, 3)
            Dim num2 As New Complex(3, 4)
            Dim sum As Complex = num1 + num2
            Console.WriteLine("Real: {0}", sum.real)
            Console.WriteLine("Imaginary: {0}", sum.imaginary)
            Console.ReadLine()
        End Sub
    End
Class

Screen Output Generated from the above code

overloading1.gif

The next example presents a more sophisticated example of operator overloading.

Example of Sophisticated Operator Overloading

    Imports System
    Class Rectangle
        Private iHeight As Integer
        Private
iWidth As Integer

        Public
Sub New()
            Height = 0
            Width = 0
        End Sub

        Public
Sub New(ByVal w As Integer, ByVal h As Integer)
            Width = w
            Height = h
        End Sub

        Public
Property Width() As Integer
            Get
                Return
iWidth
            End Get
            Set
(ByVal value As Integer)
                iWidth = value
            End Set
        End
Property

        Public
Property Height() As Integer
            Get
                Return
iHeight
            End Get
            Set
(ByVal value As Integer)
                iHeight = value
            End Set
        End
Property

        Public
ReadOnly Property Area() As Integer
            Get
                Return
Height * Width
            End Get
        End
Property
       
        Public Shared Operator =(ByVal a As Rectangle, ByVal b As Rectangle) As Boolean
            Return
((a.Height = b.Height) AndAlso (a.Width = b.Width))
        End Operator

        Public Shared Operator <>(ByVal a As Rectangle, ByVal b As Rectangle) As Boolean
            Return
Not (a = b)
        End Operator

        Public Shared Operator >(ByVal a As Rectangle, ByVal b As Rectangle) As Boolean
            Return
a.Area > b.Area
        End Operator

        Public Shared Operator <(ByVal a As Rectangle, ByVal b As Rectangle) As Boolean
            Return
Not (a > b)
        End Operator

        Public Shared Operator >=(ByVal a As Rectangle, ByVal b As Rectangle) As Boolean
            Return
(a > b) OrElse (a = b)
        End Operator

        Public Shared Operator <=(ByVal a As Rectangle, ByVal b As Rectangle) As Boolean
            Return
(a < b) OrElse (a = b)
        End Operator

        Public
Overrides Function Equals(ByVal o As Object) As Boolean
            Return
Me.Equals(o)
        End Function

        Public
Overrides Function GetHashCode() As Integer
            Return
Me.GetHashCode()
        End Function

        Public
Overrides Function ToString() As [String]
            Return "Height=" + Height + ",Width=" + Width
        End Function

        Public
Shared Sub Main()
            Dim objRect1 As New Rectangle()
            Dim objRect2 As New Rectangle()
            Dim objRect3 As New Rectangle(10, 15)
            objRect1.Height = 15
            objRect1.Width = 10
            objRect2.Height = 25
            objRect2.Width = 10
            Console.WriteLine("Rectangle#1 ", objRect1)
            Console.WriteLine("Rectangle#2 ", objRect2)
            Console.WriteLine("Rectangle#3 ", objRect3)
            If objRect1 = objRect2 Then
                Console.WriteLine("Rectangle1 & Rectangle2 are Equal.")
            Else
            If
objRect1 > objRect2 Then
                Console.WriteLine("Rectangle1 is greater than Rectangle2")
            Else
            Console.WriteLine("Rectangle1 is lesser than Rectangle2")
            End If
            End
If
            If
objRect1 = objRect3 Then
                Console.WriteLine("Rectangle1 & Rectangle3 are Equal.")
            Else
                Console.WriteLine("Rectangle1 & Rectangle3 are not Equal.")
            End If
            Console.ReadLine()
        End Sub
    End
Class

Screen Output Generated from the above code

overloading2.gif

Conclusion


Hope this article would have helped you in understanding Overloading in VB.NET.

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor