WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button

Object Oriented Programming for VB.NET - Part 2

by Budi Kurniawan
12/17/2001

The new version of Visual Basic, VB7 and also known as VB.NET, offers the full features of an OOP language. Even though the concept of objects is not entirely new to VB programmers, writing code in an OOP way probably is. Here is an article for VB programmers who want to learn OOP and have a quick grasp of it. You also might want to check out part one of this series.

Method Overloading

A method is either a subroutine or a function. The difference, of course, is that a function in Visual Basic returns a value, while a subroutine does not. Both functions and subroutines can accept arguments.

When choosing method names, you should, of course, use a name that reflects what the method does. But there are situations where you need methods that do similar things but accept different lists of arguments. For example, you may need a method that prints a double, as well as a method that prints a string. What do you do? You can write two methods and call them PrintDouble and PrintString respectively. But you really want to call both methods Print, because that is what they do.

OOP languages, including VB.NET, allow multiple methods to have the same name, as long as those methods have different lists of arguments. This is called method overloading. Each of the methods with the same name is comfortably called an overload. In VB.NET, you add the keyword Overloads as the first part in the method signature. The use of the Overloads keyword is optional, however; you can still have methods with the same name without using the keyword. For example, consider Listing 14, which shows the class Calculator with two Add methods. The first overload accepts two integers and the second overload accepts two doubles.


Imports System

Class Calculator

  Overloads Public Function Add(a As Integer, b As Integer) As Integer
    Add = a + b
  End Function

 Overloads Public Function Add(a As Double, b As Double) As Double
    Add = a + b
  End Function

End Class


Module Module1

  Public Sub Main()

    Dim counter As Calculator
    counter = New Calculator()
    ' pass two integers
    Console.WriteLine(counter.Add(1, 5))
    ' pass two doubles
    Console.WriteLine(counter.Add(1.3, 5.9))
 
  End Sub
End Module

Related Reading

VB .NET Language in a NutshellVB .NET Language in a Nutshell
By Steven Roman, Ron Petrusha & Paul Lomax
Table of Contents
Index
Full Description
Read Online -- Safari

In the Main sub in the Module1 module, we instantiate a Calculator object and call the Add method twice. On the first call to the Add method, we pass two integers, 1 and 5.


' pass two integers
Console.WriteLine(counter.Add(1, 5))

On the second call, we pass two doubles, 1.3 and 5.9.


' pass two doubles
Console.WriteLine(counter.Add(1.3, 5.9))

Comment on this articleHow's your OOP? Post comments and questions about programming in VB.NET.
Post your comments

The smart thing is, the Common Language Runtime knows which method overload to call. The first call to the Add method returns 6, the second method returns 7.2.

Method overloading can also be found almost everywhere in the .NET Framework Class Library. Take the Console class in the System namespace as an example. This class has a method named Write that outputs the representation of a value to the console. The Write method has 18 overloads. There is one overload that accepts a Boolean, one that accepts an integer, and so on. These overloads make sure that you can pass any type of data to the Write method and the method will still work correctly.

Beginners to OOP often make the mistake of thinking that methods can be overloads if the return values are of different types. This is not the case. In order to be an overload, the method must accept a unique set of arguments. Two methods with the same name that accept the same list of arguments will not compile, even though they return different return types.

For instance, the Calculator class in Listing 15 will not compile because the two Add methods have the same set of arguments, even though the types of their return values are different.

Listing 15: Incorrect method overloading


Imports System
Class Calculator

  Overloads Public Function Add(a As Integer, b As Double) As Decimal
    Add = a + b
  End Function

  Overloads Public Function Add(a As Integer, b As Double) As Double
    Add = a + b
  End Function

End Class

When you compile the class, it will report the following compile error:

error BC30301: 'Public Overloads Function Add(a As Integer, b As Double) As Decimal' and 'Public Overloads Function Add(a As Integer, b As Double) As Double' differ only by return type. They cannot overload each other.

Why doesn't the compiler allow that? Because a function can be called without expecting a return value. For example, in the case of the erroneous Calculator class, if the compiler allowed that and the programmer called the Add function by passing an integer and a double like this:


  Dim counter As Calculator
  counter = New Calculator()
  ' pass two integers
 counter.Add(1, 5.89)
it would not be clear which overload is being called.

Pages: 1, 2, 3

Next Pagearrow