Carl Franklin

.NET Wonk

Operator Overloading in VB.NET Whidbey

So, I'm going through all of the new features of VB.NET Whdibey and posting my findings and samples. The next topic I tackled was operators. The first time I saw this I got it, so it was easy to implement. I never knew how much I needed this feature!  Consider the following class:

Public Class OrderItem

    Private _productid As String

    Private _quantity As Int32

    Private _unitprice As Decimal

    Public Property ProductID() As String

        Get

            Return _productid

        End Get

        Set(ByVal Value As String)

            _productid = Value

        End Set

    End Property

    Public Property Quantity() As Int32

        Get

            Return _quantity

        End Get

        Set(ByVal Value As Int32)

            _quantity = Value

        End Set

    End Property

    Public Property UnitPrice() As Decimal

        Get

            Return _unitprice

        End Get

        Set(ByVal Value As Decimal)

            _unitprice = Value

        End Set   

    End Property

    Public ReadOnly Property Total() As Decimal

        Get

            Return UnitPrice * Quantity

        End Get

    End Property

    Public Shared Operator +(ByVal Item1 As OrderItem, ByVal Item2 As OrderItem) As OrderItem

        If Item1.ProductID <> Item2.ProductID Then

            Throw New Exception("You can only add OrderItems where the ProductID is the same for both objects")

        ElseIf Item1.UnitPrice <> Item2.UnitPrice Then

            Throw New Exception("You can only add OrderItems where the UnitPrice is the same for both objects")

        Else

            Dim item As New OrderItem

            With item

                .ProductID = Item1.ProductID

                .Quantity = Item1.Quantity + Item2.Quantity

                .UnitPrice = Item1.UnitPrice

            End With

            Return item

        End If

    End Operator

End Class

It's all pretty straight-ahead until you get to the Operator. Public Shared Operator + is the meat of it. That's saying “I want this code to execute when someone adds two of my objects together with the + operator. The arguments represent the addends (is that the right word?) and the result is what's returned on the left of the equal sign.

My operator basically insures that the productid and unit price are the same for both objects, and then simply adds the quantities together.

Here is some code in a button to test it.

    Dim Item1 As New OrderItem

    Dim Item2 As New OrderItem

    Dim Item3 As OrderItem

    With Item1

        .ProductID = "1"

        .Quantity = 5

        .UnitPrice = 10

    End With

    With Item2

        .ProductID = "1"

        .Quantity = 4

        .UnitPrice = 10

    End With

    Item3 = Item1 + Item2

    '-- This shows 90!

    MsgBox(Item3.Total)

 

Comments

G. Andrew Duthie said:

That's cool stuff. And a nice, concise explanation. Thanks, Carl!
# November 14, 2003 12:38 PM

Josh said:

I think the word you were looking for is "operands". But if you are anything like me, you were probably making up your own word on purpose.
# November 14, 2003 5:32 PM

rahsan said:

Carl, i have doubt in operator overloading
above given code i tried to execute but at operator defination ti showing an error "Expected end of statement" and Operator this keyword is not recognizing. plz. give solution for it
# November 19, 2003 3:15 AM

Carl Franklin said:

I'm using the Alpha bits of Whidbey from the PDC. What version are you using?
# November 19, 2003 8:41 AM

Sparky said:

Like, it's 2003....? where have you guys been the last 5 years?
# November 20, 2003 8:08 PM

TrackBack said:

How far ahead do Micro$oft look, there is a two new versions of Visual Studio .Net (code named 'Whidbey' and 'Orcus' ) and a new Operationg system (codenamed 'Longhorn') on the way. I See that there are some very useful...
# January 29, 2004 2:31 AM

Alxdotnet said:

addends is right
# February 21, 2004 3:22 PM

ihkhj said:

gjhkh
# March 9, 2004 2:14 AM

TrackBack said:

# April 20, 2004 2:58 AM

Michael Ferrini said:

Ahhh finally operator overloading in Visual Basic.
# May 6, 2004 8:27 AM

Amol Borkar said:

Thanks for the grate help.

# September 21, 2007 5:55 AM

Lexapro. said:

Celexa lexapro. Lexapro and discharge. What is lexapro. Lexapro.

# July 17, 2008 2:11 AM

Yogesh (Student) said:

Thanks,

This is a Good article helpful to me and my friends

# December 9, 2008 7:56 AM

simple said:

You made it so complicated, you could have done this as simple as possible:

Item3 = Item1.quantity + Item2.quantity

why make it difficult for newbies???

# May 25, 2009 3:51 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)