Arrays

The major change in arrays in Visual Basic .NET is in how an array's upper and lower bounds can be set. In Visual Basic 6.0, either you could specify just an upper value, in which case the Option Base setting for your code would determine whether the array had a lower bound of 0 or 1, or you could specify both the upper and lower bounds as a range. In Visual Basic .NET, all arrays have a lower bound of 0, which is why the Option Base statement no longer exists. Because of this explicit lower bound, specifying both the upper and lower bounds in an array declaration is no longer required or supported. In general, this change can cause a lot of confusion, and it can require quite a few changes in your code if you commonly specified the lower bound for arrays in your application. It is not uncommon to see a declaration like Dim years(1999 to 2003) As String in Visual Basic 6.0 code, but associating the actual array bounds with a real range of values cannot be done in Visual Basic .NET. Instead, you could declare your array using just an upper bound, and use a secondary variable to keep track of the starting point of the range.

Dim firstYear As Integer = 1999
Dim years(4) As String

In addition to being different from VB6, Visual Basic .NET array declarations differ from C#/C++/Java as well. In those other languages, declaring an array with a dimension of (4) would indicate an array with four elements, 0 to 3, in an array with a lower bound of 0. In Visual Basic .NET this is not the casean array declaration of (4) actually indicates an array with five elements (0 to 4). This behavior, although it conflicts with some other languages, is designed to avoid upgrade issues from Visual Basic 6.0. Because the Option Base statement allowed the same array declaration (Dim years(4) As String) to either indicate 0-4 (five elements) or 1-4 (four elements), Visual Basic .NET's array declarations have to err on the side of caution. An array declaration with a dimension of four actually indicates an array with five elements (0 to 4), which is the best choice to avoid breaking VB6 code, as code written assuming either 0 to 4 or 1 to 4 will still work against a .NET array that goes from 0 to 4.

The other array-related change in Visual Basic .NET is that it is now possible to initialize a dynamic (no declared length) array when you declare it using a special syntax.

Dim myCats() As String = {"Sisko", "Kira"}

Even though I initialized the array with two elements, I end up with an array with upper and lower bounds 0 and 1.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.172.195