CSI 2783: Programming Visual Basic .NET
Course Map >> Lesson 6 (Functions and Methods) >> More on VB .NET Methods

More on VB .NET Methods

The methods in the System.Math class represent just one group of methods available to you in VB .NET. Many, if not all of the functions discussed in "What is a function?" are available as methods of the various classes or structures in the VB .NET Namespaces.

As an example, consider the System.String structure. If you open the object browser and expand the references for the Microsoft .NET Core Library dll (mscorlib.dll), then expand the System Namespace (represented by {} System in the object browser...) you'll see a list of all the entities included in this namespace. Select the "String" structure. In the right-hand pane of the object browser you should now see a list of all the members of this structure.

As you scroll down the list, you may begin to notice methods (indicated by ) that might look familiar.

There are six methods called IndexOf(), whose definition, if you look closely, bear a striking resemblance to the Visual Basic InStr() and Mid() functions.


 

There are other methods, ToUpper() and ToLower() which operate on a string or character in a way similar to the Visual Basic UCase() and LCase() functions.


There is a method called Replace() which operates like the Visual Basic Replace() function.

 

So you may be asking yourself, "When would I use a method of the String structure instead of a Visual Basic string function?"

The answer is, use the one that makes the most sense for the circumstances.

The Visual Basic string functions like Mid(), InStr(), and UCase() are an inherent part of the Visual Basic .NET runtime and may be used without qualification in your code. For example, you can convert a string to upper case using this syntax:

Dim strSomeString As String

strSomeString = UCase("hello")

These functions are, for the most part, the same as functions by the same names found in previous versions of the VB language. This means that code written in earlier versions of VB can be ported to VB .NET without changing the lines that call these functions.

The methods of the System.String structure (.ToUpper(), .Replace(), .IndexOf(), etc.) are derived directly from the .NET Framework. Because of this, they will work the same way, whether you're programming in Visual Basic, C#, C++ or JScript. Microsoft recommends that you use these for all new code you develop.

If you're a Visual Basic .NET programmer, it's your choice whether to use the Visual Basic-only versions or the standard .NET Framework versions. (There are a couple of areas where Visual Basic provides functionality not provided by the Framework, however.) Dr. GUI recommends that you strongly consider sticking with the standard .NET Framework APIs where it's convenient to do so. Doing so will help you develop your skills in ways that can be used with other languages later on
(Dr. Gui .NET #5)