The lower bound for arrays is always zero in Visual Basic .NET (311333)



The information in this article applies to:

  • Microsoft Visual Basic .NET (2002)

This article was previously published under Q311333

SUMMARY

In Microsoft Visual Basic .NET, the lower bound of every array dimension is zero (0). You cannot declare the lower bound for arrays with any other integer. In addition, the Option Base statement is not supported in Visual Basic .NET.

MORE INFORMATION

In Microsoft Visual Basic 6.0, you can define arrays with the lower bounds and upper bounds set to any integer. You can also use the ReDim statement to reassign a variant as an array.

In Visual Basic .NET, the lower bound for arrays is set to zero to enable interoperability with other languages. Furthermore, you cannot use ReDim unless the variable was previously declared with Dim As Array. The Option Base {0|1} has been removed in Visual Basic .NET because it is no longer required. Although this restricts how you define arrays, it enables you to pass arrays between Visual Basic .NET and any other language in the Microsoft .NET Framework.

The following statements are valid in Visual Basic 6.0 but not in Visual Basic .NET:
Dim myArray(1 To 10) As Integer  
 
Dim myVariantArray
ReDim myVariantArray(10)

Dim myArray() as Integer
ReDim myArray(10) as Integer
				
The following array declarations are valid in Visual Basic .NET:
Dim myNetArray(5) As Integer 
ReDim myNetArray(10)

'You can only use ReDim with an array that was previously declared.
'You cannot change data type with ReDim; the AS clause results in compile error.

Dim myNetArray() As Integer
ReDim myNetArray(10)
				
When you upgrade a Visual Basic 6.0 project to Visual Basic .NET, any Option Base statements are removed from the code. If an array is zero bound, it is not changed. However, if an array is non-zero bound, the lower bound is removed, and a warning is inserted in the code.

For example, the following Visual Basic 6.0 code:
Dim myArray(1 To 10) As Integer
				
appears as follows after you upgrade to Visual Basic .NET:
'UPGRADE_WARNING: Lower Bound of array myArray was changed from 1 to 0
Dim myArray (10) As Integer
				
In many cases, the upgraded code works as it did before. However, if the application logic relies on the non-zero lower bound, you must modify the application. All array declarations (Dim, ReDim, and LBound statements) are marked with warnings to help you review the changes.

Before you upgrade a Visual Basic 6.0 application to Visual Basic .NET, keep the following guidelines in mind:
  • Modify the Visual Basic 6.0 application to use zero bound arrays.
  • Avoid using ReDim as an array declaration.
  • Avoid using Option Base 1.

Modification Type:MinorLast Reviewed:1/25/2006
Keywords:kbvs2005swept kbvs2005doesnotapply kbmigrate kbinfo KB311333 kbAudDeveloper