Solved

# Array & Assignement - Question

Posted on 2006-05-24
145 Views
Dim A(20), B(20), C(), D As Integer
For D = 0 To 19
A(D) = 3
B(D) = 4
Next
MsgBox(A(1) & vbTab & B(1))  'Outputs 3     4
C = A
A = B
B = C
MsgBox(A(1) & vbTab & B(1))  'Outputs 4    3

What exactly is happening during the C = A, A = B, B = C stage?

Are A, B, and C references to separate array objects that contain integers (originally)?

So, C = A makes C and A both point at the Array Object containing 20 3's, correct?

A = B changes the integer-array-reference, A, to point to the array containing 20 3's, correct?

Thanks, this is a little fuzzy to me in VB (whereas it is very eplicit how such things work in C++)
0
Question by:oxygen_728

LVL 14

Accepted Solution

I think you got it.

A() = 3's
B() = 4's

C = A --> C = 3's --> C points to the array A (with the 3's in it).
A = B --> A = 4's --> A now points to the array B (with the 4's in it)
B = C --> B = 3's --> B now points to C (and at this point C is pointing to A, so C has 3's in it.)

Does that make any sense?
0

Author Comment

Ya, I am more concerned with what exactly what is happening with the references on a very low level
0

LVL 34

Assisted Solution

I'm not sure whether this is, or illustrates, what you mean by "on a very low level".  But try this code

Dim A(10), B(20), C(), D As Integer
For D = 0 To 9
A(D) = 3
Next
For D = 0 To 19
B(D) = 4
Next
MsgBox(A.Length & vbTab & B.Length) ' & vbTab & C.Length)
C = A
MsgBox(A.Length & vbTab & B.Length & vbTab & C.Length)
A = B
MsgBox(A.Length & vbTab & B.Length & vbTab & C.Length)
B = C
MsgBox(A.Length & vbTab & B.Length & vbTab & C.Length)
C(1) = 9
MsgBox(A(1) & vbTab & B(1) & vbTab & C(1))

This illustrates, I think, that the "=" operator as between arrays of the same type makes that on the left REFER TO that on the right.  Up until the last two lines it might appear that it made that on the left into another object identical to that on the right.  But the fact that it is only the references that is made the same, rather than the objects themselves, is illustrated by the fact that changing a value in C also changes it in B.

Roger
0

Author Comment

thanks
0

## Featured Post

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…