ARTICLE

Operator Overloading in VB.NET

Posted by Rohatash Kumar Articles | Visual Basic 2010 August 30, 2010
This article shows the Operator Overloading in VB.NET.
 
Reader Level:

In this article we will see the operator overloading in Visual Basic.net.

 When an operator can perform more than one operations, specially with objects, known as operator overloading.A function name can be replaced with the operator using operator keyword.

Important points related to Operator Overloading or guidelines:

  • Don't change an operator's semantic meaning. The result of an operator should be intuitive.
  • Make certain overloaded operators are shared methods.
  • You cannot overload assignment e.g., the "=" operator .

 VB.NET allows the following operators to be overloaded:

  • + (increment/plus - unary and binary)>
  • - (decrement/minus - unary and binary)
  • Not (unary)
  • * (multiply - binary)
  • / (divide - binary)
  • \ (integer divide - binary)
  • & (concatenate - binary)
  • Like (binary)
  • Mod (binary)
  • And (binary)
  • Or (binary)
  • ^ (raise to power - binary)
  • << (left shift - binary)
  • >> (right shift - binary)
  • = (equals - binary)
  • <> (not equals - binary)
  • < (less than - binary)
  • > (greater than - binary)
  • <= (less than or equal to - binary)
  • >= (greater than or equal to - binary)

 This example  shows the (+) Operator Overloading in VB.NET:

Module module1

    Class Size

        Private feet As Integer, inch As Integer

        Public Sub New(ByVal f As Integer, ByVal i As Integer)

            feet = f

            inch = i

        End Sub

        Public Sub ShowSize()

            Console.WriteLine("Size is {0} feet and {1} inch", feet, inch)

        End Sub

        Public Shared Function Add(ByVal x As Size, ByVal y As Size) As Size       'add method

            Dim f As Integer = x.feet + y.feet

            Dim i As Integer = x.inch + y.inch

            If i >= 12 Then

                i = i - 12

                f += 1

            End If

            Return New Size(f, i)

        End Function

        Public Shared Operator +(ByVal x As Size, ByVal y As Size) As Size             ' add method replace with operator(+) using operator keyword

            Dim f As Integer = x.feet + y.feet

            Dim i As Integer = x.inch + y.inch

            If i >= 12 Then

                i = i - 12

                f += 1

            End If

            Return New Size(f, i)

        End Operator

        Public Shared Sub Main()

            Dim s1 As New Size(5, 9)

            Dim s2 As New Size(8, 8)

            Dim s3 As Size = Size.Add(s1, s2)                    ' add method perform the task

          'Dim s3 As Size = s1 +s2                           ' + operator perform the task  

                      s3.ShowSize()

        End Sub

    End Class

End Module

 

OUTPUT :

 

ooi.gif


In the above example (+) operator perform more than one operation. the function name add replace with the operator using Operator keyword.

Login to add your contents and source code to this article
share this article :
post comment
 
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. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Diagram
Become a Sponsor