Method Overloading

Optional parameters are still supported in Visual Basic 2005. You will use them often when you call methods on the Office object models, because many of the methods in Word and Excel accept optional parameters. However, there is another way to provide methods that can take a variety of parameters in Visual Basic 2005. This feature is known as method overloading.

In Visual Basic 2005, you can create multiple methods that have the same name but differ only in the type or number of arguments. Each additional method defined is an overload of the method. For example, the code example in Figure 4.4 shows a Print method with three overloads. The first overload prints the document to a file; therefore, it contains some arguments specific to printing to a file, such as Append and OutputFileName. These arguments don't make sense in the second overload, which does not allow a document to be printed to a file. The third overload doesn't accept any arguments and instead prints the document with all the default arguments.

Public Suti Print(ByVal Append^As Boolean, ByVal OutputFileName As String, _

Figure 4.4. Providing overloaded methods

0 0

Post a comment