ArrayCopy length question

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
 
G

Guest

Not sure if this helps or if I'm reading it right but my documentation for
Array.Copyto method says that it's only for "one" dimensional arrays.
 
D

David

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).

You're running across the VB weirdness in array declaration...

Dim count as Integer = 3
Dim ar(count) As Integer

creates an array with ar.Length = 4, i.e., count + 1. Sure, it's
stupid, but that's the way it is. "count" in this case is not the
length of the array, but rather the upper bound of the index.

I'm honestly not sure that flattening a multi-dimensional array with
Array.Copy in the way you're doing is documented or reliable (another
poster suggests it is not), and it's the combination of the two things
that seems to be throwing you off here.
 
G

Guest

Actually, the documentation for array.copy says "When copying between
multidimensional arrays, the array behaves like a long one-dimensional array,
where the rows (or columns) are conceptually laid end to end."

It is the array.copyto method that is restricted to one dimensional arrays.


However your reply was helpful. My problem is if I attemt to perform the
console.write on the initil array (ArrA) without the array.copy intermediate,
I get a "Late binding not permitted with Strict On" error message.

Is there another work-around?
 
D

David

Actually, the documentation for array.copy says "When copying between
multidimensional arrays, the array behaves like a long one-dimensional array,
where the rows (or columns) are conceptually laid end to end."

It is the array.copyto method that is restricted to one dimensional arrays.

Well, it also states that the arrays must have the same number of
dimensions when using Array.Copy.
However your reply was helpful. My problem is if I attemt to perform the
console.write on the initil array (ArrA) without the array.copy intermediate,
I get a "Late binding not permitted with Strict On" error message.

Actually, I don't think you really have a problem. "column + 1" is
indeed the correct length of a single dimension of the array, as I
mentioned in the first post, and I thought that was the main worry.
 
C

Cor Ligthert

David,
creates an array with ar.Length = 4, i.e., count + 1. Sure, it's
stupid, but that's the way it is. "count" in this case is not the
length of the array, but rather the upper bound of the index.
It is stupid that an indexer starts at zero.

That is historical growth and we find it now normal. Some people have tried
to correct it; however, we seem to be very conservative and are still using
the zero.

Among those people who wanted to change it where the ones who invented
Basic.

Therefore, the VB methods use the One (first) as starting index. There is no
other way to keep an easy way of making an array for both methods possible
than with making it comfortable for Zero(Nothing) index as for One(First)
index.

Before you start telling why it starts with a zero that I know very good,
however should not be a problem for a program language. We humans start
telling at One using our fingers and not at Zero.

I am as well used to the zero however see not real a good explanation for
it, than a machine depended one.

Just my 2 eurocents.

Cor
 
D

David

David,

It is stupid that an indexer starts at zero.

That is historical growth and we find it now normal. Some people have tried
to correct it; however, we seem to be very conservative and are still using
the zero.

If VB.Net used 1-based arrays I would find that annoying, but not
stupid. The fact that some statements syntactically imply 1-based
arrays while others imply 0-based arrays, and the fact that declarations
are somewhere in the middle is what's stupid.
Among those people who wanted to change it where the ones who invented
Basic.

Therefore, the VB methods use the One (first) as starting index. There is no
other way to keep an easy way of making an array for both methods possible
than with making it comfortable for Zero(Nothing) index as for One(First)
index.

Before you start telling why it starts with a zero that I know very good,
however should not be a problem for a program language. We humans start
telling at One using our fingers and not at Zero.

I am as well used to the zero however see not real a good explanation for
it, than a machine depended one.

Actually, the argument for 0-based isn't machine dependent, it's
algorithmic-dependent. Whether one or the other is preferred generally
has to do with what you're counting and why (for example, try doing
graphics work with a 1-based array and you'll drive yourself nuts).

But that's neither here nor there. I really don't care much which way
the designers went on this one, I just wish they had gone one way or the
other. Trying to pretend that arrays are "sort of one-based, but really
0-based" causes nothing but confusion and ambiguity.
 
C

Cor Ligthert

David,

When I had written I thought, they could also have gone to use in the VB
namespace the 1 as indexer however change that automaticly in a Zero index
in the ILS. Would have been probably much more elegant, I am also forever
strugling with those extra rows in an Array.

However I was waiting for your answer to make this thread not to complex.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top