Method Overloading

There are cases when a class needs to achieve the same task starting with different input values—that is, with different arguments. You have seen examples of this behavior when you learned about constructors. The same rules apply to regular method overloading. The overloading of regular methods is less common than the constructor overloading, which is used in almost every class. A class that models a calculator could have a method called Add that can be overloaded to take different types of numeric values (Short, Integer, Long, and so on).

Public Class Calculator
   Public Overloads Function Add(ByVal x As Short, ByVal y As Short) As Long
      Return x + y
   End Function
   Public Overloads Function Add(ByVal x As Integer, ByVal y As Integer) ...

Get Visual Basic® .NET by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.