For Each VB syntax

bri189a

Senior Contributor
Joined
Sep 11, 2003
Messages
1,004
Location
VA
In C# you can:

foreach(string s in MyStrings)
Console.WriteLine(s);

Note that s is declared on the spot, the closes I've been able to find for VB is the following:

Dim s As String
For Each s In MyStrings
Console.WriteLine(s)
Next s

I much rather have:
ForEach s as String In MyStrings
Console.WriteLine(s)
Next s

because all the extra Dim lines in the middle of a function just to do a loop looks, feels, and smells sloppy, but putting them at the top of the function just looks messy. There has to be away to declare and use a variable in a loop in VB. I've noticed the same issue with a regular for loop.
 
That's why it wouldn't work then...I have VB 2002. Will that work for iteration loop?

Visual Basic:
   For i As Integer = 1 To 10
  Console.WriteLine(i)
  Next i
(also how come my carriage returns aren't showing up in this code block?)
Thanks!
 
Last edited by a moderator:
Back
Top