VB6 permits to dynamically change the rank (i.e. the number of dimensions) of an array, as in this code snippet:
Dim arr() As Integer
Sub InitArray(ByVal rank As Integer)
Dim i As Integer, j As Integer
If rank = 1 Then
ReDim arr(100) As Integer
For i = 1 To UBound(arr)
arr(i) = i
Next
Else
ReDim arr(100, 20)
For i = 1 To UBound(arr)
For j = 1 To UBound(arr, 2)
arr(j, i) = i * j
Next
Next
End If
End Sub
VB.NET and C# don’t allow you to change the rank of an array, therefore the converted .NET code would raise one instance of the following error:
Number of indices exceeds the number of dimensions in the indexed array
for each statement where the array is accessed with two indexes. These is no definitive solution to this problem, however VB Migration Partner allows you to significantly decrease the number of this compilation error, using a combination of pragmas:
Dim arr() As Integer
An explanation of each pragma is in order. The ParseReplace pragma causes VB Migration Partner to realize that there are actually two arrays (arr and arr2) and the ArrayRank pragma specifies that arr2 is a 2-dimensional array. The first subsequent PreProcess pragma changes arr into arr2 if the array reference is followed by two indexes that are separated by a comma – as in arr(1,2) - whereas the second PreProcess pragma changes array references that appear in LBound and UBound method calls. The neat result is:
Private arr() As Short
Private arr2(,) As Short
Public Sub InitArray(ByVal rank As Short)
Dim i As Short
Dim j As Short
If rank = 1 Then
ReDim arr(100)
For i = 1 To UBound6(arr)
arr(i) = i
Next
Else
ReDim arr2(100, 20)
For i = 1 To UBound6(arr)
For j = 1 To UBound6(arr2, 2)
arr2(j, i) = i * j
Next
Next
End If
End Sub
private short[] arr = null;
private short[,] arr2 = null;
public void InitArray(short rank)
{
short i = 0;
short j = 0;
if (rank == 1)
{
arr = new short[101];
for (i = 1; i <= VB6Helpers.UBound(arr);i++)
{
arr[i] = i;
}
}
else
{
arr2 = new short[101, 21];
for (i = 1; i <= VB6Helpers.UBound(arr); i++)
{
for (j = 1; j <= VB6Helpers.UBound(arr2, 2); j++)
{
arr2[j, i] = i * j;
}
}
}
}
As you see, all occurrences have now been fixed correctly, except for the statement marked with a remark. You can fix that single issue manually or by slightly editing the original VB6 code as follows:
For i = 1 To UBound(arr, 1)
It’s interesting to notice that the ParseReplace and PreProcess pragmas perform their chores before VB Migration Partner parses the VB6 code. This is important because VB Migratio Partner can correctly deduce the rank of array parameters of method that receive those arrays.