3

Here's the code in VB.Net

If Not queryItems Is Nothing Then
                For Each qItem As String In queryItems
                    qItem = qItem.ToLower()
                Next
End If

and it's "equivalent" code in c# (using sharpdevelop/developerfusion/telerik's converter/VS 2012 "paste as c#" method)

if (queryItems != null)
{
    foreach (string qItem in queryItems)
    {
        qItem = qItem.ToLower();
    }
}

The C# compiler (rightly so ) complains with the following

"Cannot assign to 'qItem' because it is a 'foreach iteration variable'"

I am wondering why this behavior is permitted in VB.Net?

Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
Sekhar
  • 5,614
  • 9
  • 38
  • 44
  • you may find information about this in [here](http://stackoverflow.com/questions/7838079/why-cant-we-assign-a-foreach-iteration-variable-whereas-we-can-completely-modi) – Mahmut Ali ÖZKURAN Mar 01 '13 at 23:22
  • 2
    It's not clear what it was expected to accomplish in the VB version - surely it wouldn't have actually "saved" the lower case version back to the original collection, would it? – Jon Skeet Mar 01 '13 at 23:22
  • Does this help ? http://stackoverflow.com/questions/2037988/changing-foreach-iteration-variable-and-implementation-differences-between-c-sha – Amit Mar 01 '13 at 23:25
  • @JonSkeet - very keen observation - I was doing a dumb conversion from one code to another - as adding changes was becoming painful. I can remove that code now! – Sekhar Mar 01 '13 at 23:28

2 Answers2

2

The crux of the question appears to be this

I am wondering why this behavior is permitted in VB.Net?

A better question may be the following

Why did C# prevent assignment to the foreach iteration variable?

If you look at the set of lopping constructs and languages C# is the odd ball here. In virtually every other case (even in Java foreach) it is legal to assign to the iteration variable of a looping construct. The only other cases I`m aware of are

  • F#: Variables are readonly by default though so this is just consistency with the rest of the language
  • Ada: Disallows assignment of the looping value in for statements

VB.Net is actually more consistent here with the norm than C#.

Unfortunately it's not clear why C# made this choice. There are a lot of speculative answers out there but until Eric or Anders does a blog post on this the real reason will remain unknown

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • I used to program in Java - but for some reason I always used to think that the iteration variables cannot be reassigned.. thanks for your input! – Sekhar Mar 01 '13 at 23:51
1

It's perfectly reasonable to want to set each string in (an array? a list?) to lower-case, in a loop.

SUGGESTION: just use a good old "for()" loop -

// Assuming array syntax...
if (queryItems != null)
{
    for (int i=0; i < queryItems.Length; i++)
    {
        queryItems[i] = queryItems[i].ToLower();
    }
}

IMHO...

PS: I'm not sure if the VB.Net version ever actually worked as intended. Do you know?

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • I wasn't paying attention to the logic or what it was doing - reading through and making changes in vb was taking longer - so I decided to convert this class. this time I was more interested in the language feature – Sekhar Mar 01 '13 at 23:30