Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views4 pages

Overloading

Uploaded by

edjudge123uk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Overloading

Uploaded by

edjudge123uk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Posted in VB.

NET | VISUAL BASIC LANGUAGE on November 07, 2019


Tags: Overload, Overloading, VB.NET
In this article I will explain you about overloading in VB.NET.

 2423

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.

Logical /
Arithmeti Compariso
Concatenatio Bitwise
c n
Operators n Operators Operators
Operator
s
^ + = 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

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

Conclusion

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

You might also like