0

I have the follow expression:

myGrid.ForEach(Sub(Model) myStreamWriter.WriteLine(Function(x As Integer) (x + 1)))

Of course it's not working. I want to write in a text file (WriteLine) a sequential number (1, 2, 3, etc.) for each record collected in the variable varGrid type: List (of mytable)

Any idea? It's fine CSharp code too.

Regards.

1 Answer 1

1

You could declare the counter outside of the foreach lambda, just as you would declare it outside of the scope of a foreach loop.

var x = 1;
myGrid.ForEach(model => myStreamWriter.WriteLine(x++.ToString()));
1
  • 1
    The same with VB.NET: myGrid.ForEach(Sub(Model) myStreamWriter.WriteLine(System.Math.Max(System.Threading.Interlocked.Increment(x), x - 1))). It worked!!! TKS!
    – Sebastián
    Jul 17, 2013 at 21:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.