Firstly I must note that this could mess things up pretty bad in your code so keep that in mind before getting all to excited.
I am talking about custom operators for classes and structures, that if done with grace can help you a lot. I will begin with introducing the concept of operators first for those of you who might have forgotten about it and then I'll show you how to create your own.
What is an operator anyway?
The recognition of operators is universal, in maths as well as in programming. The typical operator takes two arguments and performs an operation on the two. E.g (1+1) and (strName2 = strName2), + and = are the operators in those cases. All operators perform either addition, subtraction, division, multiplication, modulus or a comparative =, <>, <, >, <=, >= operator. Logical operators include OR, XOR, AND, NOT etc.
Operators in .NET.
The means in which operators works in .net differ for each data type, for example + and – is not recognized for String but instead for numerics. All classes and structs you create on your own will only have support for comparative operators, and rarely the concative & operator. All types supports the logical operators though. What so great about the framework is that you can override these operators and implement your own that fits the nature of your own types.
Let's define a class like this:
Public Class Vehicle
Private strManufacturer As String
Public Property Manufacturer As String
Get
return strManufacturer
End Get
Set(ByVal value as string)
strManufacturer = value
End Set
End Property
End Class
And write some simple code to compare those instances:
Dim ericsCar as New Vehicle
ericsCar.Manufacturer = "Volvo"
Dim sarahsCar as New Vehicle
sarahsCar.Manufacturer = "Ford"
If ericsCar = sarahsCar Then
Console.WriteLine("You will never read this in your debugger since = is not defined for Vehicle.")
Else
Console.WriteLine("You lucky bastard.")
End If
There is no = operator for the Vehicle class by default, instead we must use the Is operator which compares references, in other words if ericsCar and sarahsCar shares the SAME object in memory. The comparison takes no concern if the two objects actually are logically equal vehicles.
(I could achieve the same goal by comparing the Manufacturer string property of the two objects…but that's not in favor of talking about custom operators so screw that :P).
Now; I think there are two aspects in this. Firstly, a good programmer never programs IN a language he programs INTO it. If something in the language doesn't meet his needs he will EXTEND it (this is in favor of overloading operators). Secondly, mixing with operators can be confusing for other developers because it's rarely done for complex types (you expect 1+1 to return 2 not 4). You rather tend to see it in more simple structs and low-level types.
Implementing our own = operator for our Vehicle class
Public Class Vehicle
Private strManufacturer As String
Public Property Manufacturer As String
Get
return strManufacturer
End Get
Set(ByVal value as string)
strManufacturer = value
End Set
End Property
Public Shared Operator =(ByVal objVehicle1 as Vehicle, ByVal objVehicle2 as Vehicle) As Boolean
If objVehicle1.Manufacturer.Equals(objVehicle2.Manufacturer)
Return True
Else
Return False
End If
End Operator
Public Shared Operator <>(ByVal objVehicle1 as Vehicle, ByVal objVehicle2 as Vehicle) As Boolean
If Not objVehicle1.Manufacturer.Equals(objVehicle2.Manufacturer)
Return True
Else
Return False
End If
End Operator
End Class
Now you can use = and <> for all your objects of the type Vehicle and you can evaluate them just as I want. You can even add arithmetic operators.
Final word of caution
Overloaded operators can be helpful in situations where you want more control over your classes and structures and how they behave in your solution. But I would recommend you to be careful when implanting them. Make sure you return somewhat of an expected value (don't modify * to return a divison etc.) but with modifications more suitable for your needs. It is also useful to be more aware of operators in VB.net in general.