The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Operator Overloading in VB.Net 2.0

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Raymond Lewallen

Posts: 312
Nickname: rlewallen
Registered: Apr, 2005

Raymond Lewallen is a .Net developer and Sql Server DBA
Operator Overloading in VB.Net 2.0 Posted: Apr 13, 2005 6:57 AM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Raymond Lewallen.
Original Post: Operator Overloading in VB.Net 2.0
Feed Title: Raymond Lewallen
Feed URL: /error.htm?aspxerrorpath=/blogs/raymond.lewallen/rss.aspx
Feed Description: Patterns and Practices, OOP, .Net and Sql
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Raymond Lewallen
Latest Posts From Raymond Lewallen

Advertisement

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.

Read: Operator Overloading in VB.Net 2.0

Topic: Router for Gamers Previous Topic   Next Topic Topic: Google Maps and Microsoft

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use