Raymond Lewallen

Sponsors

The Lounge

Wicked Cool Jobs

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Operator Overloading in VB.Net 2.0

The .Net framework version 2.0 brings many new things to developers in all languages. This has been talked about quite a bit, but I just wanted to refresh everybody on one of things I know I'm looking forward to. Operator overloading.  If you've done any programming in C#, none of this is new to you.  If you are strictly VB.Net, or new to any .Net language, then read on.

Operator overloading is the act of making something happen when somebody wants to use an operator, such as + (plus sign) to add two or more objects together. At present, you can't do this in VB.Net in fx 1.0, 1.1. Let's look at how we accomplish adding objects together in current release versions of VB.Net

The VB.Net 1.1 way

Public Class RoadTrip

 

    Public Sub New()

    End Sub

 

    Public Sub New(ByVal milesTraveled As Int32)

        CheckValue(milesTraveled)

        _miles = milesTraveled

    End Sub

 

    Private Sub CheckValue(ByVal value As Int32)

        If value < 0 Then Throw New ArgumentOutOfRangeException("Value")

    End Sub

 

    Private _miles As Int32

    Public Property Miles() As Int32

        Get

            Return _miles

        End Get

        Set(ByVal Value As Int32)

            CheckValue(Value)

            _miles = Value

        End Set

    End Property

 

    Public Shared Function Add(ByVal roadTripOne As RoadTrip, ByVal roadTripTwo As RoadTrip) As RoadTrip

        Dim newRoadTrip As New RoadTrip

        newRoadTrip.Miles = roadTripOne.Miles + roadTripTwo.Miles

        Return newRoadTrip

    End Function

 

End Class

Nothing wrong with that.  Just pop in an add function and we get the functionality that we want.  Consider a slight drawback.  The API user has to know there is an Add method, which intellisense pretty much provides to the user, so its really not even a drawback.  Let’s implement the above code:

Implementing the above 1.1 code

Public Sub ShowExample()

        Dim roadTripOne As New RoadTrip(100)

        Dim roadTripTwo As New RoadTrip(250)

        Console.WriteLine("Road trip one was {0} miles.", roadTripOne.Miles)

        Console.WriteLine("Road trip two was {0} miles.", roadTripTwo.Miles)

        Dim totalTrip As RoadTrip

        totalTrip = RoadTrip.Add(roadTripOne, roadTripTwo)

        Console.WriteLine("Total trip was {0} miles.", totalTrip.Miles)

    End Sub

The output

Road trip one was 100 miles.

Road trip two was 250 miles.

Total trip was 350 miles.

What operator overloading allows us to do is not have the Add method, but include an operator that performs the same actions.  In this case, it will be + (plus sign)

The VB.Net 2.0 way

Public Class RoadTrip

 

    Public Sub New()

    End Sub

 

    Public Sub New(ByVal milesTraveled As Int32)

        CheckValue(milesTraveled)

        _miles = milesTraveled

    End Sub

 

    Private Sub CheckValue(ByVal value As Int32)

        If value < 0 Then Throw New ArgumentOutOfRangeException("Value")

    End Sub

 

    Private _miles As Int32

    Public Property Miles() As Int32

        Get

            Return _miles

        End Get

        Set(ByVal Value As Int32)

            CheckValue(Value)

            _miles = Value

        End Set

    End Property

 

    Public Shared Operator +(ByVal roadTripOne As RoadTrip, ByVal roadTripTwo As RoadTrip) As RoadTrip

        Dim newRoadTrip As New RoadTrip

        newRoadTrip.Miles = roadTripOne.Miles + roadTripTwo.Miles

        Return newRoadTrip

    End Operator

 

End Class

Implementing the above 2.0 code

Public Sub ShowExample()

        Dim roadTripOne As New RoadTrip(100)

        Dim roadTripTwo As New RoadTrip(250)

        Console.WriteLine("Road trip one was {0} miles.", roadTripOne.Miles)

        Console.WriteLine("Road trip two was {0} miles.", roadTripTwo.Miles)

        Dim totalTrip As RoadTrip

        totalTrip = roadTripOne + roadTripTwo

        Console.WriteLine("Total trip was {0} miles.", totalTrip.Miles)

    End Sub

The output

Road trip one was 100 miles.

Road trip two was 250 miles.

Total trip was 350 miles.

 All in all, in my opinion, easier to write, easier to read, easier to understand.


Posted Mon, Apr 11 2005 8:00 PM by Raymond Lewallen
Filed under:

[Advertisement]

Comments

Rik Hemsley wrote re: Operator Overloading in VB.Net 2.0
on Tue, Apr 12 2005 7:41 AM
Good example, apart from the choice of class. A RoadTrip likely has a start point and an end point. I know it doesn't in your example, but would the reader would be forgiven for assuming they would be added in future? If so, how then would you add two RoadTrips? What if the two RoadTrips being added do not have a common point where one starts and the other ends? Of course, this is possible too, but then how can it make sense to add two RoadTrips and get a distance?

In the case of a RoadTrip, I'd suggest that the user should write totalTrip = roadTripOne.Miles + roadTripTwo.Miles.

The Daily Grind linked to this article today with the comment 'An interesting new feature, but one that I fear is destined to be overused by those developers more interested in appearing cool than in being sensible'. I'm now experiencing the same fear.

Rik
Wei Chung wrote re: Operator Overloading in VB.Net 2.0
on Wed, Sep 7 2005 10:00 PM
Hi Raymond,

It's wonderful to hear this. I can see this would be very useful, and would like to contribute an idea to extend your sample.

Given that we have

RoadTrip1
A to B , 30 miles

RoadTrip2
B to C , 50 miles

RoadTrip3
B to D , 100 miles

RoadTrip4 = RoadTrip1 + RoadTrip3

where we can actually get:
A to B to D , 130 miles,

isn't it great?

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Devlicio.us