Using Barcode printer for ASP.NET Control to generate, create QR Code image in ASP.NET applications.

www.OnBarcode.com

Note that the Overloads keyword is optional, but if you use it for one overloaded method you must use it for all of them. Not only does method overloading make the code less cluttered, it also makes it more efficient. The compiler decides which version of the Item function is called, and no test is necessary at run time:

Using Barcode creator for Reporting Service Control to generate, create ECC200 image in Reporting Service applications.

www.OnBarcode.com

Just as important, the compiler can flag invalid arguments, so you don t have to trap arguments of invalid type:

*** The following code doesn t compile (if Option Strict is On).

Dim value As Double = 1.23

result = myObj.Item(value)

Method overloading lets you solve cases that are almost unmanageable under Visual Basic 6. For example, say that you re implementing an InstrWord function, which searches whole words and exposes a syntax similar to that of the standard InStr func tion. The problem with InStr is that it comes with two syntax forms:

result = InStr(text, search)

result = InStr(index, text, search, Optional compareMethod)

If you want your InstrWord function to closely mimic the InStr function, your only option under Visual Basic 6 is to declare a single function that takes variants and resolves all the possible cases at run time. The Visual Basic .NET solution is much sim pler, also because you can have the simpler form delegate to the most complete one. (Notice that I have omitted the optional Overloads keyword.)

Function InstrWord(ByVal source As String, _ ByVal search As String) As Long First case is just a special case of the more general case. Return InstrWord(1, source, search, CompareMethod.Binary) End Function Function InstrWord(ByVal index As Long, _ ByVal text As String, ByVal search As String, _ ByVal Optional cmpMethod As CompareMethod = CompareMethod.Binary) As Long Second case is the more general case. End Function

Again, not only is the code cleaner and easier to maintain, it is also more efficient (because the compiler makes its decisions at compile time) and robust (because invalid calls don t even compile). IntelliSense correctly recognizes overloaded methods and displays a list of all the supported syntax forms. You can visit all of them using the up and down arrow keys:

Method overloading lets you get rid of optional arguments, even though this decision requires that you create a distinct overloaded version for each possible optional argu ment. The problem with optional arguments is that a few .NET languages most nota bly, C# don t recognize them. Therefore, C# developers calling a Visual Basic .NET

Part II:

Object-Oriented Programming

method must pass all the arguments whether they re required or optional. If you plan to expose those methods to languages other than Visual Basic, you should implement overloaded methods rather than methods with optional arguments. Keep in mind that optional arguments are resolved when you compile the client code, not when you compile the method that defines them. If the client code omits one or more optional arguments, the compiler adds the necessary (hidden) statements that push those arguments default values onto the stack. This can lead to versioning prob lems when the client code and the target method belong to different assemblies and therefore are compiled separately. If you then recompile the method and change the default value of the optional argument, you should recompile all its clients as well; oth erwise, they ll pass the wrong value. You won t have this problem if you stay clear of optional arguments and replace them with overloaded methods.