473,671 Members | 2,305 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.ReadLin e()

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(arra y, 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(A rrB(i, j) & Chr(9))
Next
Console.WriteLi ne()
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 1345
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.ReadLin e()

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(arra y, 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(A rrB(i, j) & Chr(9))
Next
Console.WriteLi ne()
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**@discussio ns.microsoft.co m> 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
multidimensiona l 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**@discussio ns.microsoft.co m> 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**@discussio ns.microsoft.co m> wrote:
Actually, the documentation for array.copy says "When copying between
multidimensiona l 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**@discussio ns.microsoft.co m> wrote:
Actually, the documentation for array.copy says "When copying between
multidimensiona l 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**********@p lanet.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
11730
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 MyString As String Am I getting VLS declaration or a FLS declaration? Can I control which I get somehow? I have done some homework, but I don't understand
7
2539
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; Console.WriteLine("Length is {0}",AcctNbr.Length); }
4
9894
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
1650
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
8022
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 the following routine are:
6
2703
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? I made this program that finds the average of x number of numbers and stores the average in the last index of the array of the numbers. The other other variable I have is to determine the length of the array (totalNums). Is there a way to get...
5
9568
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
3478
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, dest, destpos, numelem): dest = source is there a built in version, or better way of doing this...
6
37253
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 bluntness So here my question... Like you may know, there are functions for finding the length of a string (myString.length()), but is it possible to find the length of a double?? More specifically if i have myDouble = 4.53 then i guess the length...
0
8473
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8390
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8597
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8667
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7428
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6222
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5692
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2808
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2048
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.