Changes to Array Bounds Array bounds have gone through several revisions in Visual Basic .NET. Several books based on Beta versions of Visual Basic .NET covered this topic incorrectly because array implementation evolved in response to outcry from some developers who apparently had Microsoft's ear. By Beta 2 build 9254, the array behavior had reached its final form, but the Visual Basic .NET help still reported incorrect information as indicated by the following quote from the help topic "Array Bound Changes in Visual Basic":
Option Base is not supported in Visual Basic .NET. The array bounds are from 0 to n, meaning that every array has n + 1 elements. The best way to write code that is independent of such revisions is to use methods that dynamically return the upper and lower bounds of arrays: Dim A(10) As Integer Dim I As Integer For I = A.GetLowerBound(0) To A.GetUpperBound(0) ' some code Next I GetLowerBound and GetUpperBound are methods of System.Array. The argument to these two methods indicates the dimension of the array whose bound you want to know. A one-dimensional array is referenced by dimension 0. (Refer to the section "Using Arrays" in Chapter 3, "Basic Programming in VB.NET," for more on System.Array in Visual Basic .NET.) Array Size Can Change but the Number of Dimensions Is FixedYou can not initially declare an array with the ReDim statement in Visual Basic .NET. All arrays are dynamic in Visual Basic .NET and must initially be declared with a Dim statement in Visual Basic .NET. And, although you size the number of elements, the number of dimensions is fixed in Visual Basic .NET. This is a departure from the way arrays were declared and behaved in VB6. The following statements demonstrate some basic array declarations. Dim a(10) As Integer Dim a() As Integer = New Integer(10){} Dim a() As Integer = {0,1,2,3,4,5,6,7,8,9,10} The first statement declares an array containing 11 elements, indexable from 0 to 10. The array is uninitialized . The second statement uses the long form of array declaration, initializing an array capable of containing 11 integers. The final statement uses the array initializer syntax and creates an array containing 11 elements indexed from 0 to 10, containing the values 0 to 10. |
Team-Fly |
Top |