Hi rhodylady,
Unfortuantly, this is an oversight in the VB.Net documentation as far
as I know. Arrays do work this way but only with referenced objects,
not with value types.
Value Types are simple VB.Net objects such as Integers or Strings.
These are always used "ByVal" or by value.
Referenced Objects are usually more complex objects, like a collection
or some class object you have designed yourself.
Its tricky to understand, but when using a value type, you are
basically taking a copy of that object when you implement a
For..Each..Next loop. Try to think of it as 2 integer type variables:
Dim iFirstInteger as Integer
Dim iSecondInteger as Integer
iSecondInteger = 0
iFirstInteger = iSecondInteger
iFirstInteger = 20
Would you expect iSecondInteger to equal 20 or 0? Of course you would
expect it to equal 0 and it would. This is because the VALUE of
iSecondInteger is assigned to iFirstInteger, not a REFERENCE to the
object.
Now apply this to your For..Each..Next loop:
Dim iFirstInteger as Integer
Dim iSecondIntegers(5) as Integer 'This is an array of
iSecondIntegers
For Each iFirstInteger in iSecondIntegers
iFirstInteger = 20
Next
You are looping through an array of ISecondIntegers, telling
iFirstInteger to equal iSecondInteger, and then setting the value of
iFirstInteger to 20. It doesn't matter that its an array of Values,
its the same as the example above, just repeated 6 times.
The correct way to do this is:
Dim A(5) as Integer
For Elt = 0 to A.GetUpperBound(0)
A(Elt) = 20
Next
This way you are addressing each element of the array directly.
More information on this problem and understanding the differences
between values and references can be found on Microsofts MSDN:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbls7/html/vblrfVBSpec6_1.asp
If you wish this to be further clarified, please let me know.
Good luck,
Molloch |