Overloading de Operador
¢Suporta overload de operadores padrão (classes e structures)
¢' Classe com overload de +
¢Public Class Dem
¢ Private x As Byte
¢ Private y As Byte
¢
¢ Public Sub New(ByVal i As _
¢ Byte, ByVal j As Byte)
¢ Me.x = i
¢ Me.y = j
¢ End Sub
¢ Public Overrides Function _
¢ ToString() As String
¢ Return String.Format( _
¢ "[{0},{1}]", x, y)
¢ End Function
¢ Public Shared Operator +( _
¢ ByVal d1 As Dem, _
¢ ByVal d2 As Dem) As Dem
¢ Return New Dem( _
¢ d1.x + d2.x,d1.y + d2.y)
¢ End Operator
¢End Class
¢
¢' Teste
¢Dim d1 As Dem = New Dem(1, 0)
¢Dim d2 As Dem = New Dem(2, 2)
¢Dim d3 As Dem = d1 + d2
¢d3 += d1
¢
¢Trace.Write(d3) ' Exibe [4,2]

| O Visual Basic 2005 agora suporta overloading de operadores, conforme o exemplo exibido. Note que o overloading do operador ‘+’ automaticamente fornece a funcionalidade do operador ‘+=’.