Consider the following, apparently innocent piece of VB code:

' create and initialize an array
Dim arr(10) As Integer
arr(0) = 100
' store it into a collection
Dim col As New Collection
col.Add(arr)
' modify the array, compare with the array in the collection
arr(0) = 200
If arr(0) = col(1)(0) Then
    txtResult.Text = "Equal"
Else
    txtResult.Text = "Not equal"
End If

The question: what will be displayed in the txtResult control: "Equal" or "Not Equal?

Surprisingly, the correct answer is "It depends on the Visual Basic version you are using!" In fact, it displays "Not Equal" under VB6 and "Equal" under VB.NET.

In fact, when passing the array to the Collection.Add method, VB6 performs a copy of the array, therefore the subsequent assignment to arr(0) doesn't affect the copy already stored in the collection and the two arr(0) elements are now different. Vice versa, VB.NET passes a reference to the System.Array object, therefore there is only one array in memory and the assignment to arr(0) affects the same array as seen from the Collection.

This behavior can be the cause of a very subtle bug when converting a complex VB6 application to VB.NET, and neither Upgrade Wizard (included in Visual Studio) nor any VB6 conversion tool on the market can automatically solve this problem. The first time we bumped into this problem it took us hours to track it down, and we wanted to save our customers such headaches.

Once you see where the problem is, you can fix it by simply cloning the array being stored to the collection

        col.Add(arr.Clone())

VB Migration Partner also offers an alternative solution to this problem, in the form of the VB6Collection class, which offers this and several other enhancement over the Microsoft.VisualBasic.Collection object.