### WEBINAR:On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017

## What Are Operators?

Here is a complete list of Operators available in Visual Basic.

Operator overloading takes place where different operators have different implementations, depending on their arguments.

## Today's Project

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.

```   Private X As Integer
Private Y As Integer
```

```   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
```

### Multiply

```   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.

## Putting It All Together

```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
```

## Conclusion

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.

#### Hannes DuPreez

Hannes du Preez has been a Microsoft MVP for Visual Basic from 2008 to 2017. He loves technology and loves Visual Basic. He loves writing articles and proving that Visual Basic is more powerful than what most believe. You are most welcome to reach him at: ojdupreez1978@gmail.com

## Related Articles

• There are no comments yet. Be the first to comment!

• You must have javascript enabled in order to post comments.

## Top White Papers and Webcasts

• As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
• The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
• Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...