ClassSystem.Array SyntaxArray.Copy(sourceArray, destinationArray, length) or: Array.Copy(sourceArray, sourceIndex, destinationArray, _ destinationIndex, length)
DescriptionThe Copy method makes a copy of all or part of an array. Since arrays are reference types, setting one array variable equal to another simply assigns a new reference to the same array. Consider the following code: Dim mainArray( ) As Integer = {1, 2, 3} Dim otherArray( ) As Integer otherArray = mainArray otherArray(0) = 10 MsgBox(mainArray(0)) ' Displays 10 Since changes to otherArray impacted mainArray, the two arrays are clearly the same. The Copy method makes a true copy of the elements, not simply a new reference to the same array. For arrays of value types, the new elements will be truly distinct. For arrays of reference types, the new elements can still impact the original reference data. Usage at a Glance
ExampleThis sample is similar to the code shown above in the "Description" section comments, but it uses the Copy method instead of a direct assignment. Dim mainArray( ) As Integer = {1, 2, 3} Dim otherArray( ) As Integer ReDim otherArray(UBound(mainArray) + 1) Array.Copy(mainArray, otherArray, UBound(mainArray) + 1) otherArray(0) = 10 MsgBox(mainArray(0)) ' Displays 1 Version DifferencesSince arrays were not reference types in VB 6, you could simply create a copy of an existing array through assignment, thus eliminating the need for a Copy method. See AlsoArray Class, Array.Sort Method |