VB.NET 4.0 Parallel.ForEach AddressOf Multiple Values?

You can do this via a Lambda Expression: Imports System.Threading. Tasks Sub Main() Dim secs() As Integer = {2, 3, 1} Dim Path as String = "Constant" Parallel. ForEach(secs, Sub(sec) Method2(sec, Path)) End Sub Sub Method2(ByVal I As Integer, path as string) Console.

WriteLine(i.ToString(), path) End Sub This will allow the compiler to create a closure that gets the value of Path, and pass it directly into your method.

Thanks, I can't add an answer yet for 3 more minutes. But I do get that same warning with Sub – Jasin Nov 5 at 0:58 @Jasin Is that warning a Visual Studio warning, or a Resharper/3rd party tool warning? I suspect it's a false warning... – Reed Copsey Nov 5 at 1:00 VS 2010 Pro, I will be happy to ignore it, just making sure.

– Jasin Nov 5 at 1:02 @Jasin The code I have above, exactly as typed, doesn't give that warning in Visual Studio 2010... (And it works correctly). Do you have Resharper or any extensions installed? – Reed Copsey Nov 5 at 1:03 Productivity Power Tools, Spell Checker.

- Perhaps do you think It matters that Path is actually from a ForEach in Paths? - My code is more complex then above, was just trying to get this part right. – Jasin Nov 5 at 1:06.

Certainly in C#, I'd use a lambda expression to call Method2 instead: Parallel. ForEach(secs, I => Method2(i, path)); Whether the equivalent will look elegant in VB is beyond the amount of good judgement I have about a language I don't use at 12.50am, but you don't have a lot of other choices really - assuming you don't want to create an instance of another class, passing path into the constructor, and then using a single-parameter method in that class... (Oh, and another option would be to change Method2 into a single parameter method where the parameter encapsulated both values. So I guess you do have a few options - but in C# at least, the lambda expression one would be the way to go.).

I converted this to Parallel. ForEach(Directories, Function(i) ScanDirectory(i, Path)) and get this warning (Using the iteration variable in a lambda expression may have unexpected results. Instead, create a local variable within the loop and assign it the value of the iteration variable.) Thoughts?

– Jasin Nov 5 at 0:54 @Jasin My answer shows the correct syntax for this in VB - you need Sub(i), not Function(i) – Reed Copsey Nov 5 at 0:55.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions