Click here to Skip to main content
16,004,887 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
AnswerRe: Theory and practice of XML layout? Pin
Luc Pattyn14-Sep-10 5:17
sitebuilderLuc Pattyn14-Sep-10 5:17 
GeneralRe: Theory and practice of XML layout? Pin
Gregory Gadow15-Sep-10 3:50
Gregory Gadow15-Sep-10 3:50 
GeneralRe: Theory and practice of XML layout? Pin
Luc Pattyn15-Sep-10 5:41
sitebuilderLuc Pattyn15-Sep-10 5:41 
GeneralRe: Theory and practice of XML layout? Pin
Gregory Gadow15-Sep-10 6:02
Gregory Gadow15-Sep-10 6:02 
GeneralRe: Theory and practice of XML layout? Pin
PIEBALDconsult14-Sep-10 17:59
mvePIEBALDconsult14-Sep-10 17:59 
GeneralRe: Theory and practice of XML layout? Pin
Gregory Gadow15-Sep-10 3:46
Gregory Gadow15-Sep-10 3:46 
GeneralRe: Theory and practice of XML layout? Pin
PIEBALDconsult15-Sep-10 15:54
mvePIEBALDconsult15-Sep-10 15:54 
GeneralYou can't assign to an array element within a For Each loop! [modified] Pin
Peter R. Fletcher13-Sep-10 5:17
Peter R. Fletcher13-Sep-10 5:17 
It is possible that I am the only person in the world who didn't already know this Smile | :) , but there is one very important difference between the behavior of a For Each loop that iterates through an array (or an array of Structures, which was the context that has just bitten me!) and the corresponding For Next loop. When you use For Each, the item that is passed into the loop is effectively passed by Value. Any assignment that you make to it within the loop is lost as soon as you leave it. If you put exactly the same code within an equivalent For Next loop, the assignments "stick".

The following very short Console Application demonstrates this:

Module Module1
    Dim intArray(10) As Integer
    Sub Main()
        Dim intI As Integer
        For intI = 0 To 10 ' fill the array
            intArray(intI) = intI
        Next

        For Each item In intArray ' show that the data has been stored
            Console.Write(item.ToString & ", ")
        Next
        Console.Write(vbCrLf)

        intI = 10
        For Each item In intArray ' fill it again in reverse order
            item = intI
            Console.Write(item.ToString & ", ") ' show that contents of the "local" copy of the array element has been changed
            intI -= 1
        Next
        Console.Write(vbCrLf)

        For Each item In intArray
            Console.Write(item.ToString & ", ") ' but the contents of the actual array have not!
        Next
        Console.Write(vbCrLf)
        Console.ReadLine()
    End Sub

End Module


I was (eventually) able to find documentation of this behavior in the VB .Net docs (ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/dv_vbalr/html/ebce3120-95c3-42b1-b70b-fa7da40c75e2.htm), but I could not find any explicit mention of it in any of the books I have or in any other on-line resources, even though all of the latter do only show For Each being used in ways that work. I am hoping that this note may help others to avoid the head-scratching that I went through before if figured out why my code was not working.

modified on Monday, September 13, 2010 11:27 AM

GeneralRe: You can't assign to an array element within a For Each loop! Pin
Luc Pattyn13-Sep-10 6:13
sitebuilderLuc Pattyn13-Sep-10 6:13 
GeneralRe: You can't assign to an array element within a For Each loop! Pin
Peter R. Fletcher13-Sep-10 8:23
Peter R. Fletcher13-Sep-10 8:23 
GeneralRe: You can't assign to an array element within a For Each loop! Pin
Luc Pattyn13-Sep-10 8:33
sitebuilderLuc Pattyn13-Sep-10 8:33 
GeneralRe: You can't assign to an array element within a For Each loop! Pin
Peter R. Fletcher13-Sep-10 9:24
Peter R. Fletcher13-Sep-10 9:24 
GeneralRe: You can't assign to an array element within a For Each loop! Pin
Luc Pattyn13-Sep-10 9:28
sitebuilderLuc Pattyn13-Sep-10 9:28 
GeneralRe: You can't assign to an array element within a For Each loop! [modified] Pin
harold aptroot13-Sep-10 11:18
harold aptroot13-Sep-10 11:18 
GeneralRe: You can't assign to an array element within a For Each loop! Pin
Luc Pattyn13-Sep-10 11:25
sitebuilderLuc Pattyn13-Sep-10 11:25 
GeneralRe: You can't assign to an array element within a For Each loop! Pin
harold aptroot13-Sep-10 11:29
harold aptroot13-Sep-10 11:29 
GeneralRe: You can't assign to an array element within a For Each loop! Pin
Luc Pattyn13-Sep-10 11:43
sitebuilderLuc Pattyn13-Sep-10 11:43 
GeneralRe: You can't assign to an array element within a For Each loop! Pin
harold aptroot13-Sep-10 12:00
harold aptroot13-Sep-10 12:00 
GeneralRe: You can't assign to an array element within a For Each loop! Pin
DaveyM6913-Sep-10 12:27
professionalDaveyM6913-Sep-10 12:27 
Questionhow do I auto relocate component when resizing the window? Pin
bimbambumbum12-Sep-10 14:09
bimbambumbum12-Sep-10 14:09 
AnswerRe: how do I auto relocate component when resizing the window? Pin
PIEBALDconsult12-Sep-10 14:30
mvePIEBALDconsult12-Sep-10 14:30 
GeneralRe: how do I auto relocate component when resizing the window? Pin
bimbambumbum12-Sep-10 14:39
bimbambumbum12-Sep-10 14:39 
Questiondatagridview Pin
Jefry boycot12-Sep-10 2:28
Jefry boycot12-Sep-10 2:28 
AnswerRe: datagridview Pin
Abhinav S12-Sep-10 3:45
Abhinav S12-Sep-10 3:45 
GeneralRe: datagridview Pin
Jefry boycot16-Sep-10 7:17
Jefry boycot16-Sep-10 7:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.