PC Review


Reply
Thread Tools Rate Thread

ArrayCopy length question

 
 
=?Utf-8?B?bWFyaw==?=
Guest
Posts: n/a
 
      29th Aug 2004
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


--
mark
 
Reply With Quote
 
 
 
 
=?Utf-8?B?RGVubmlz?=
Guest
Posts: n/a
 
      29th Aug 2004
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.

"mark" wrote:

> 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
>
>
> --
> mark

 
Reply With Quote
 
David
Guest
Posts: n/a
 
      29th Aug 2004
On 2004-08-29, mark <(E-Mail Removed)> wrote:
> 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.



 
Reply With Quote
 
=?Utf-8?B?bWFyaw==?=
Guest
Posts: n/a
 
      29th Aug 2004
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?

"David" wrote:

> On 2004-08-29, mark <(E-Mail Removed)> wrote:
> > 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.
>
>
>
>

 
Reply With Quote
 
David
Guest
Posts: n/a
 
      30th Aug 2004
On 2004-08-29, mark <(E-Mail Removed)> wrote:
> 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.


 
Reply With Quote
 
=?Utf-8?B?bWFyaw==?=
Guest
Posts: n/a
 
      30th Aug 2004

Correct you are. Thanks.

In fact the length should be (rows+1)X(cols+1)


"David" wrote:

> On 2004-08-29, mark <(E-Mail Removed)> wrote:
> > 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.
>
>
>

 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      30th Aug 2004
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



 
Reply With Quote
 
David
Guest
Posts: n/a
 
      30th Aug 2004
On 2004-08-30, Cor Ligthert <(E-Mail Removed)> wrote:
> 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.


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.

 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      30th Aug 2004
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


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
array length question Madhu Microsoft ASP .NET 3 8th Jun 2006 10:42 AM
Question about the max length of a TextBox control =?Utf-8?B?RmxhY2s=?= Microsoft C# .NET 0 2nd Mar 2006 03:26 PM
ATA-100 cable length question aGaiN file_server@box.com Storage Devices 1 27th Aug 2004 10:15 AM
IDE cable length question Gabagimpy DIY PC 2 23rd Dec 2003 02:45 PM
Question about the length of IDE cables WooduCoodu DIY PC 7 27th Sep 2003 06:52 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:41 PM.