Array assignments: how do they work precisely?
Hi all,
could someone explain to me (or point to a website explaining it) how assignments of an array work precisely? I read the helpfile but am confused.
Preparation steps:
dim a(100) as byte
dim b(100) as byte
for i = 0 to 100
a(i)=i
next i
Now: why do I somewtimes see
b=a'and now b is supposed to be filled with 0 to 100 as well
And other times I see people using a loop:
for j=1 to 100
b(j)=a(j)
next j
to supposedly arrive at the same result.
Is there a difference?
Thanks, Kees
[907 byte] By [
kesim] at [2007-12-24]
b and a are "references" to arrays, they are not the arrays themself. So if you just do
b = a
then b will contain a reference to the same array as a.
The result in this case is that if you do
a(0) = 2
then b(0) will become 2 also, because it's the same array in fact.
In the second case, with the for loop b and a are references do 2 different arrays and the for loop copies values form the a array to the b array. So in this case if you do
a(0) = 2
then b(0) won't be affected by this, it will keeps it's original value.
"b = a" just results in b referring to the same array. That is, a change in array 'a' will be noticed on 'b' (since they're the same array).
The example loop you provided copies the values from one array to another. It's a pointless exercise since the Copy method does this in one method call.
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter, VB to C++ converter
Clear VB: Cleans up VB.NET code
C# Code Metrics: Quick metrics for C#