In this article, you will learn about Operator overloading in Visual Basic.
What Are Operators?
Here is a complete list of Operators available in Visual Basic.
What Is Operator Overloading?
This article is a very good guideline on Operators and Operator Overloading.
Create a Console application and add a new class to it. You may name the class anything you like, but keep in mind that my object names might differ from yours. Add the following code into your newly created class.
Add the following member variables to your class:
Private X As Integer Private Y As Integer
Add the constructor:
Sub New(ByVal x2 As Integer, _ ByVal y2 As Integer) X = x2 Y = y2 End Sub
In the New sub, or the Constructor, I have added two arguments that we will supply to the calling class. Whatever values we supply here will be used in calculations with the new overridden operators.
The Plus Operator
Public Shared Operator +(ByVal Param1 As Operators, _ ByVal Param2 As Operators) _ As Operators Return New Operators(Param1.X + Param2.X, _ Param1.Y + Param2.Y) End Operator
In the preceding code, the plus operator gets overridden to calculate Param1.X + Param2.X as well as Param1.Y + Param2.Y, thus providing a new skeleton for the plus operation.
The Minus Operator
Public Shared Operator -(ByVal Param1 As Operators) _ As Operators Return New Operators(-Param1.X, -Param1.Y) End Operator
Public Shared Operator *(ByVal Param1 As Operators, _ ByVal Param2 As Integer) _ As Integer Return (Param1.X * Param2) + (Param1.Y * Param2) End Operator
Greater Than and Less Than Operators
Public Shared Operator >(ByVal Param1 As Operators, _ ByVal Param2 As Operators) _ As Boolean Return (Param1.X + Param1.Y) > (Param2.X + Param2.Y) End Operator Public Shared Operator <(ByVal Param1 As Operators, _ ByVal Param2 As Operators) _ As Boolean Return Not Param1 > Param2 End Operator
Narrowing and Widening Operator(s)
Public Shared Narrowing Operator CType(ByVal Param1 _ As Operators) As Point Return New Point(Param1.X, Param1.Y) End Operator
A widening conversion changes a value to a data type that can allow for any possible value of the original data. Widening conversions preserve the source value but can change its representation. This occurs if you convert from an integral type to Decimal, or from Char to String.
A narrowing conversion changes a value to a data type that might not be able to hold some of the possible values. For example, a fractional value is rounded when it is converted to an integral type, and a numeric type being converted to Boolean is reduced to either True or False.
Here is more information on the Narrowing and Widening operators.
Putting It All Together
Add the following into a newly added Module.
Module Module1 Sub Main() Dim bool As Boolean Dim int As Integer Dim Var1 As New Operators(4, 5) Dim Var2 As New Operators(2, -1) Var1 = Var1 + Var2 'Calls 1 Console.WriteLine(Var1) Var1 = -Var1 'Calls 2 Console.WriteLine(Var1) int = Var2 * 4 'Calls 3 Console.WriteLine(int) Dim pt As Point = CType(Var1, Point) 'Calls 4 Console.WriteLine(pt.ToString) bool = Var1 > Var2 'Calls 5 Console.WriteLine(bool) End Sub End Module
There is still a lot learn about Operator overloading. This little article's aim was just to show you how it can be done; obviously, the onus now rests on your shoulders to continue exploring this topic.