Google Answers Logo
View Question
 
Q: VB.Net Populating Array with For Each/Next ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: VB.Net Populating Array with For Each/Next
Category: Computers > Programming
Asked by: rhodylady-ga
List Price: $2.00
Posted: 20 Nov 2002 10:27 PST
Expires: 20 Dec 2002 10:27 PST
Question ID: 111370
I'm using Visual Basic.Net. Many books I read explain how to use a For
Each / Next loop to populate an array.  It looks VERY simple, yet it
does not work for me.  I really want to do this with a For Each loop
and not a For Next loop or Do Loop. What am I doing wrong?  Here is my
code, within a click event of a button:

Dim intTotal(5) As Integer      'declare array - all zeroes now
Dim intOneElement As Integer

For Each intOneElement In intTotal
      intOneElement = 42
Next intOneElement

At the end of the subprocedure I'd expect each element of the array to
have 42 in it, yet each element still has 0.

Request for Question Clarification by theta-ga on 20 Nov 2002 11:38 PST
Where exactly in your code do you check for the value of the array
cotents, to see if they have changed or not ?

  The MSDN Library 
           MSDN : VB.NET>Loop Structures >For Each...Next Statements
          ( http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vaconUsingForEach.asp
)
  gives the following example for using a subroutine to set array
element value :
         Sub Preset(ByRef A() As Integer)
            Dim Elt As Integer
             For Each Elt In A
                Elt = 128
             Next Elt
         End Sub
As you can see, the lines of the for each next loop, exactly match
your code. So the problem maybe in your array value checking code,
outside the loop.
If you could post the full source for the subprocedure, perhaps we
could find a solution.

Clarification of Question by rhodylady-ga on 20 Nov 2002 12:03 PST
I appreciate such a QUICK response!

I'm using the Autos or Watch windows in Debug.  I set a breakpoint at
the end of the subprocedure and look at the contents of the array.

Alternatively, at the end of the For Each loop I've run a loop to take
the contents of the array and put them into a label.  The whole click
event would look like this:

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button2.Click
        Dim intTotal(5) As Integer
        Dim intOneElement As Integer
        Dim X As Integer

        For Each intOneElement In intTotal
            intOneElement = 42
        Next intOneElement

        For X = 0 To 5
            lblTest.Text += CStr(intTotal(X)) & " "
        Next
    End Sub
Answer  
Subject: Re: VB.Net Populating Array with For Each/Next
Answered By: molloch-ga on 20 Nov 2002 18:42 PST
Rated:5 out of 5 stars
 
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
rhodylady-ga rated this answer:5 out of 5 stars and gave an additional tip of: $1.00
Hallelujah - finally an explanation, AND one that I understand. 
Moolach - THANK YOU.

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy