Using For Each in VB.NET

  • Thread starter Thread starter John
  • Start date Start date
J

John

When I try to declare my element in (as opposed to before)
my "For Each" statement I get a syntax error. When I try
to do the same thing in C# I have no problem.

This does not work...
For Each node As XmlNode In nodeList
Me.ListBox1.Items.Add(node.InnerText)
Next

This does...
Dim node As XmlNode
For Each node In nodeList
Me.ListBox1.Items.Add(node.InnerText)
Next

And this works in C#
foreach (XmlNode node in nodeList)
this.listBox1.Items.Add(node.InnerText);

According to Help | About in Visual Studio .NET, I am
using MS Development Environment 2002, version 7.0.9466
and MS .NET Framework 1.0, version 1.0.3705 (I have
installed Service Pack 2).
 
John said:
When I try to declare my element in (as opposed to before)
my "For Each" statement I get a syntax error. When I try
to do the same thing in C# I have no problem.

This does not work...
For Each node As XmlNode In nodeList
Me.ListBox1.Items.Add(node.InnerText)
Next

This syntax was added to VB in the DotNet Framework 1.1. So you would need
to use Visual Studio .NET 2003.

David
 
* "John said:
When I try to declare my element in (as opposed to before)
my "For Each" statement I get a syntax error. When I try
to do the same thing in C# I have no problem.

This does not work...
For Each node As XmlNode In nodeList
Me.ListBox1.Items.Add(node.InnerText)
Next

This does...
Dim node As XmlNode
For Each node In nodeList
Me.ListBox1.Items.Add(node.InnerText)
Next

And this works in C#
foreach (XmlNode node in nodeList)
this.listBox1.Items.Add(node.InnerText);

According to Help | About in Visual Studio .NET, I am
using MS Development Environment 2002, version 7.0.9466
and MS .NET Framework 1.0, version 1.0.3705 (I have
installed Service Pack 2).

You will have to upgrade to VS.NET 2003 in order to use this language
feature in VB.NET too.
 
Back
Top