473,320 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

ArrayCopy length question

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
Nov 21 '05 #1
8 1326
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

Nov 21 '05 #2
On 2004-08-29, mark <ma**@discussions.microsoft.com> 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.

Nov 21 '05 #3
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 <ma**@discussions.microsoft.com> 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.

Nov 21 '05 #4
On 2004-08-29, mark <ma**@discussions.microsoft.com> 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.
Nov 21 '05 #5

Correct you are. Thanks.

In fact the length should be (rows+1)X(cols+1)
"David" wrote:
On 2004-08-29, mark <ma**@discussions.microsoft.com> 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.

Nov 21 '05 #6
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

Nov 21 '05 #7
On 2004-08-30, Cor Ligthert <no**********@planet.nl> 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.

Nov 21 '05 #8
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
Nov 21 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: MLH | last post by:
I'm working with lots of long strings now, it seems. I have to import them & parse them constantly. The A97 memo field type supports only 32768 chars. What happens when this is processed... Dim...
7
by: Matt | last post by:
Have below code AcctNbr give me result of 30. That is the database column length, Column stores 10 why is giving me 30 ? while(objRead.Read()) { AcctNbr = (string)objRead.ToString().Length;...
4
by: moondaddy | last post by:
Is there a asp.net validator control that validates the length of the text being entered or does everyone just write jscript for this? -- moondaddy@nospam.com
3
by: Madhu | last post by:
I would like to know how the following will be handled in the .net framework: Pl. don't pay attention to the syntax int a int b a=2 a=2 a=2
1
by: Sathyaish | last post by:
I have the following scenario: Algorithm: 3DES Cipher Mode: CBC Key Size: 128-bit Block Size: 64 bit IV: 0x0000000000000000 (an eight byte array of zeros) The results I get using .NET with...
6
by: foreverbored75 | last post by:
Hello All! I am just learning c++ in school and I have the following question: Is there a way for the user to input the length of an array (console application) without using another variable?...
5
bucketbot
by: bucketbot | last post by:
If I have an array A and I wanted to copy it into a larger array, how would I do that with the arraycopy(...) method? thanks
1
by: Jordan | last post by:
I could seem to find a built in function that would serve the purpose of System.arraycopy() in java. I was able to accomplish it with something like this: def arraycopy(source, sourcepos,...
6
by: kgkgkg | last post by:
I'm sort of new to c++ but i do have years experience in other languages. So if this is a rediculous question and im just not seeing the obvious answer, don't reserve yourselves, bring on the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.