Operator Overloading

L

Lee

Does VB.NET support overloaded operators? For instance, if I create a
complex number class, Complex, can I do code like this?

Dim a As New Complex(1.2, 3.4)
Dim b As New Complex(5.6, 7.89)
b = b * 4
a = a + b

I'm not sure if Operator Overloading is an OOP (and thus maybe .NET)
feature, or a C++/C# language feature.
 
S

Sueffel

Lee said:
Does VB.NET support overloaded operators? For instance, if I create a
complex number class, Complex, can I do code like this?

Dim a As New Complex(1.2, 3.4)
Dim b As New Complex(5.6, 7.89)
b = b * 4
a = a + b

I'm not sure if Operator Overloading is an OOP (and thus maybe .NET)
feature, or a C++/C# language feature.

Yes, you can overload like that if you have the function setup in the class
as such:

Public Class Complex
Public Sub New(Num1 as Integer, Num2 as Integer)
{Code here}
End Sub
End Class

This is based off your example, not sure where you're going, but this is how
you would overload the new sub.

HTH
Sueffel
 
H

Herfried K. Wagner [MVP]

* "Lee said:
Does VB.NET support overloaded operators? For instance, if I create a
complex number class, Complex, can I do code like this?

Dim a As New Complex(1.2, 3.4)
Dim b As New Complex(5.6, 7.89)
b = b * 4
a = a + b

Currently it's not supported, but it will be supported in the next
version of Visual Basic (VS.NET 2004, Whidbey).
I'm not sure if Operator Overloading is an OOP (and thus maybe .NET)
feature, or a C++/C# language feature.

It's syntactic sugar...

;-)
 
P

Patrick Steele [MVP]

Does VB.NET support overloaded operators?

Not in the current version. The next version (included with Whidbey --
early 2005) will support operator overloading.
 
S

Sueffel

Herfried K. Wagner said:
Currently it's not supported, but it will be supported in the next
version of Visual Basic (VS.NET 2004, Whidbey).


It's syntactic sugar...

;-)

Ooops, I read that one wrong! LOL, I thought he was asking bout Function
Overloading. I's a big goof!

Sueffel
 
J

Jay B. Harlow [MVP - Outlook]

Lee,
I'm not, there is NO precedence in VB6 or VB.NET for keywords with
underscores. ;-) If anything I would expect two (or more) words.

However if the work was "spelled" out, would it be:
Operator Plus
Operator Add
Operator Addition

Remember "+" is the Addition Operator.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbls7/html/vblrfVBSpec11_5_1.asp


Further I would expect the underlying IL function will be op_Addition , as
that is the .NET convertion, per the "Common Language Infrastructure
Standard" installed in "\Program Files\Microsoft Visual Studio .NET
2003\SDK\v1.1\Tool Developers Guide\docs" on VS.NET 2003. Look in "Partition
I Architecture - 9.3 Operator Overloading".


IMHO it should be "Operator +" it is obvious you are overloading the +
operator.

Although using "Operator Left Shift" (as opposed to Operator <<) would force
developers to consider that << is not the "send to stream" operator! ;-)

Hope this helps
Jay
 
H

Herfried K. Wagner [MVP]

* "Lee said:
Great article! I'm just suprised to see the overloaded operator function
named using the actual symbols. VB has always boasted "english"
words/keywords. I'm supprised they're going with the C++ style "Operator
+(...)" instead of something like Operator_Plus(...).

Mhm... In VB.NET, we type "1 + 1", not "1 Add 1" or something like
that... Using text replacements for the operators will be misleading.

;-)
 
C

Cor

Mhm... In VB.NET, we type "1 + 1", not "1 Add 1" or something like
that... Using text replacements for the operators will be misleading.

I was right, one of my answers to you with that function as integer question
was if you was trying to build your own operater overloading funtions.

Bij the way for real Cobol programmers it is still hard to use the operater
characters

There is a statement
Compute C = B + A but that is often called not good programming.

In nice Cobol it is
Move A to C
Add B to C (IBM style)
Add A to B giving C (Burroughts style) there processor was really build in
that way.
(the hardware command was op,addr1,addr2,addr3)

Also for multiply, dividing, etc. That did go real fast (A total
calculation in almost one cycle). But the cycles where much slower than now,
so the performance is now much much higher.

Cor
 
L

Lee

Great article! I'm just suprised to see the overloaded operator function
named using the actual symbols. VB has always boasted "english"
words/keywords. I'm supprised they're going with the C++ style "Operator
+(...)" instead of something like Operator_Plus(...).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top