G
Guest
I have been modifying an application to enable debugging with Strict ON. I
need to use the Array.Copy method and I have encountered an anomaly that I do
not understand. I have reproduced the problem in the following simplified
console application.
Basically my problem is this: I dimension an array (ArrA) with a given
number of rows and columns. When I use array.copy to copy the array to a new
array (ArrB) with the length set to rows X columns, the ArrB is incomplete.
It appears I must set the length to rows X (columns+1).
Why?
Sub Main()
'initalize an array ArrA
Dim rows As Int32 = 3
Dim cols As Int32 = 4
Dim ArrA(rows, cols) As Double
ArrA(0, 0) = 1 : ArrA(0, 1) = 2 : ArrA(0, 2) = 3 : ArrA(0, 3) = 4
ArrA(1, 0) = 5 : ArrA(1, 1) = 6 : ArrA(1, 2) = 7 : ArrA(1, 3) = 8
ArrA(2, 0) = 9 : ArrA(2, 1) = 10 : ArrA(2, 2) = 11 : ArrA(2, 3) = 12
'call a subroutine which copies the array and writes the copy
CopyWrite(ArrA, rows, cols)
Console.ReadLine()
End Sub
' The CopyWrite subroutine
Sub CopyWrite(ByVal array As Array, ByVal rows As Int32, ByVal cols As
Int32)
'initialize the ArrB array
Dim ArrB(rows, cols) As Double
'copy
array.Copy(array, ArrB, rows * (cols + 1)) 'HERE IS THE LINE IN
QUESTION
'write
Dim i As Integer
Dim j As Integer
For i = 0 To 2
For j = 0 To 3
Console.Write(ArrB(i, j) & Chr(9))
Next
Console.WriteLine()
Next
End Sub
with length set to rows X (cols + 1) I get the correct array:
1 2 3 4
5 6 7 8
9 10 11 12
with length set to rows X cols I get this:
1 2 3 4
5 6 7 8
9 10 0 0
need to use the Array.Copy method and I have encountered an anomaly that I do
not understand. I have reproduced the problem in the following simplified
console application.
Basically my problem is this: I dimension an array (ArrA) with a given
number of rows and columns. When I use array.copy to copy the array to a new
array (ArrB) with the length set to rows X columns, the ArrB is incomplete.
It appears I must set the length to rows X (columns+1).
Why?
Sub Main()
'initalize an array ArrA
Dim rows As Int32 = 3
Dim cols As Int32 = 4
Dim ArrA(rows, cols) As Double
ArrA(0, 0) = 1 : ArrA(0, 1) = 2 : ArrA(0, 2) = 3 : ArrA(0, 3) = 4
ArrA(1, 0) = 5 : ArrA(1, 1) = 6 : ArrA(1, 2) = 7 : ArrA(1, 3) = 8
ArrA(2, 0) = 9 : ArrA(2, 1) = 10 : ArrA(2, 2) = 11 : ArrA(2, 3) = 12
'call a subroutine which copies the array and writes the copy
CopyWrite(ArrA, rows, cols)
Console.ReadLine()
End Sub
' The CopyWrite subroutine
Sub CopyWrite(ByVal array As Array, ByVal rows As Int32, ByVal cols As
Int32)
'initialize the ArrB array
Dim ArrB(rows, cols) As Double
'copy
array.Copy(array, ArrB, rows * (cols + 1)) 'HERE IS THE LINE IN
QUESTION
'write
Dim i As Integer
Dim j As Integer
For i = 0 To 2
For j = 0 To 3
Console.Write(ArrB(i, j) & Chr(9))
Next
Console.WriteLine()
Next
End Sub
with length set to rows X (cols + 1) I get the correct array:
1 2 3 4
5 6 7 8
9 10 11 12
with length set to rows X cols I get this:
1 2 3 4
5 6 7 8
9 10 0 0