Operator overloading

J

Jon Cosby

VB .Net does support operator overloading, doesn't it? It seems like this
should overload binary addition, but VB doesn't recognize "Operator"

Public Shared Operator +(ByVal c1 as cnum, ByVal c2 as cnum) as cnum
' End of statement expected

End Operator
 
C

Cor

Jon,

No VB.net in this versions does not support operator overloading, it will in
the next.

You can read for it in this newsgroup in a messagesthread from yesterday

Cor
 
H

Herfried K. Wagner [MVP]

* "Jon Cosby said:
VB .Net does support operator overloading, doesn't it? It seems like this
should overload binary addition, but VB doesn't recognize "Operator"

Public Shared Operator +(ByVal c1 as cnum, ByVal c2 as cnum) as cnum
' End of statement expected

End Operator

Wait for VS.NET 2004, VB.NET currently doesn't support operator
overloading.
 
J

Jon Cosby

There's a section titled "Operator Overloading Usage Guidelines in Visual
Basic" in the documentation. What's that referring to?

Jon
 
H

Herfried K. Wagner [MVP]

* "Jon Cosby said:
There's a section titled "Operator Overloading Usage Guidelines in Visual
Basic" in the documentation. What's that referring to?

Can you post the link?
 
J

Jon Cosby

ms-help://MS.VSCC/MS.MSDNVS/cpgenref/html/cpconoperatoroverloadingusageguide
lines.htm
 
T

Tom Spink

You can actually implement operator overloading routines, but cannot use
them in VB.NET, well, you can, but not in the traditional sense. You need to
call the routines that implement operator overloading, instead of using the
actual operators.

The routines are defined as op_***

where *** is the type of operator to overload, e.g. op_Implicit etc

--
HTH,
-- Tom Spink, Über Geek

Woe be the day VBC.EXE says, "OrElse what?"

Please respond to the newsgroup,
so all can benefit
 
H

Herfried K. Wagner [MVP]

Tom,

* "Tom Spink said:
You can actually implement operator overloading routines, but cannot use
them in VB.NET, well, you can, but not in the traditional sense. You need to
call the routines that implement operator overloading, instead of using the
actual operators.

The routines are defined as op_***

where *** is the type of operator to overload, e.g. op_Implicit etc

That's what the article I referenced is saying.

;-)
 
J

Jay B. Harlow [MVP - Outlook]

Jon,
VB.NET 2002 & 2003 only supports indirectly consuming overloaded operators
by the method that Tom Spink & Herfried indicated.

As Herfried stated, you need to wait for Whidbey for VB.NET to support
creating & consuming overloaded operators.

For a preview of what it will look like see:

http://blogs.msdn.com/cambecc/archive/2003/10/20/51265.aspx

Hope this helps
Jay
 
M

Mattias Sjögren

Tom,
You can actually implement operator overloading routines,

No you can't. You can consume most operator functions defined in other
languages, but you can't define your own. Just giving it the right
name (the op_ prefix) isn't enough.



Mattias
 
T

Tom Spink

Okay! Fair enough... but just out of interest... what is 'enough'?

--
HTH,
-- Tom Spink, Über Geek

Woe be the day VBC.EXE says, "OrElse what?"

Please respond to the newsgroup,
so all can benefit
 
J

Jay B. Harlow [MVP - Outlook]

Tom,
Naming a VB.NET method op_Addition is not enough to allow C# to use the
overloaded operator.

When C# creates its overloaded + method a special IL attribute is placed on
the op_Addition method, this IL attribute can only be placed by compilers
(not angle braces, such as serializable).

For details on how overloaded operators are implemented at the IL level see:

http://msdn.microsoft.com/msdnmag/issues/02/02/ManagedC/default.aspx

My initial tests in Whidbey show they are using the same naming convention
for matching operators, obviously operators such as Like have no C# & C++
equivalent.

Hope this helps
Jay

Tom Spink said:
Okay! Fair enough... but just out of interest... what is 'enough'?

--
HTH,
-- Tom Spink, Über Geek

Woe be the day VBC.EXE says, "OrElse what?"

Please respond to the newsgroup,
so all can benefit
 
T

Tom Spink

Intriguing... Thanks :)

--
HTH,
-- Tom Spink, Über Geek

Woe be the day VBC.EXE says, "OrElse what?"

Please respond to the newsgroup,
so all can benefit
Jay B. Harlow said:
Tom,
Naming a VB.NET method op_Addition is not enough to allow C# to use the
overloaded operator.

When C# creates its overloaded + method a special IL attribute is placed on
the op_Addition method, this IL attribute can only be placed by compilers
(not angle braces, such as serializable).

For details on how overloaded operators are implemented at the IL level see:

http://msdn.microsoft.com/msdnmag/issues/02/02/ManagedC/default.aspx

My initial tests in Whidbey show they are using the same naming convention
for matching operators, obviously operators such as Like have no C# & C++
equivalent.

Hope this helps
Jay
 

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