VB Operator Overloading
You can easily overload an operator just as you would a sub or function in VB. This feature is available in prior versions of .net for C# but only available for VB in 2005.
An overloaded subroutine, for example, is one in which there exist more than one definition. For example, in the sample given below, sub New is overloaded. There is a version for creating a new myVec object into which you will assign the values, and there is the default version which sets all of the component variables to 0. In the class, as you can see below, mx,my,mz are private variables corresponding to the x,y,z components respectively
|
'two
constructors,
the 2nd
for
default
convenience
Public
Sub
New(ByVal
x
As
Single,
ByVal
y
As
Single,
ByVal
z
As
Single)
mx = x
my
= y
mz = z
End
Sub
|
Public Sub New() mx = 0 my = 0 mz = 0 End Sub |
Now, to overload an operator, you simple include it in the class which will use the new operator as seen below. In this case, we overload the addition of two numbers with that of two vectors, and the result of vector addition is a vector whose individual components are the sums of the individual components of the summand vectors.
'kbingham.net 5/23/07 'Operator overloading in VB.net 'this can be pasted into a new console application vb.net project Module Module1 'create a class into which the overloaded operator (+) will go Public Class myVec 'the private variables that make up the basis of our vector class Private mx, my, mz As Single 'two constructors, the 2nd for default convenience Public Sub New(ByVal x As Single, ByVal y As Single, ByVal z As Single) mx = x my = y mz = z End Sub Public Sub New() mx = 0 my = 0 mz = 0 End Sub 'properties not used, but we will keep good habits and not declare our 'component values as public (mx,my,mz) and instead will allow them access 'through properties given below; even though in this case, they are not needed since 'the operator overload is in the class and can access the private variables Public Property x() As Single Get Return mx End Get Set(ByVal value As Single) mx = value End Set End Property Public Property y() As Single Get Return my End Get Set(ByVal value As Single) my = value End Set End Property Public Property z() As Single Get Return mz End Get Set(ByVal value As Single) mz = value End Set End Property 'we will shadow (hide) the ToString method and create our own Public Shadows Function ToString() As String Return Me.x & "," & Me.y & "," & Me.z End Function 'now we simply define our operator overload. to add vectors, you add 'the individual components. We might also create inner product overloads 'or cross product overloads etc Public Shared Operator +(ByVal a As myVec, ByVal b As myVec) As myVec Dim result As myVec = New myVec result.x = a.x + b.x result.y = a.y + b.y result.z = a.z + b.z Return result End Operator End Class 'an implementation of this to demonstrate that just by putting 'c = a + b just as we would two primitive datatypes like integer or double 'that the result is a vector whose individual components have been summed as we wanted Sub Main() Dim a As myVec = New myVec(2, 3, 5) Dim b As myVec = New myVec(5, 6, 7) Dim c As myVec = New myVec c = a + b Console.WriteLine("My original vectors => " & a.ToString & "; " & b.ToString) Console.WriteLine("Their vector sum => " & c.ToString) Console.ReadLine() End Sub End Module
---------------------------------------
kurt bingham
5/23/07
Home