Lambda does not support no return value in variables

I am beginning to play with Lambdas and I came across something that I cannot figure out. I am iterating through my list and printing them to the screen using a lambda but I then tried to assign my lambda to a variable and I get an error.

Code Works:

Code Snippet

Dim people As New List(Of Person)
people.Add(New Person With {.Age = 12, .Name = "Bob"})
people.Add(New Person With {.Age = 18, .Name = "Cindy"})
people.Add(New Person With {.Age = 13})


Console.WriteLine("Results:")

people.ForEach(Function(p As Person) Console.WriteLine("> Name = {0}, Age = {1}", p.Name, p.Age))

If I change the last line to this I get an error "Expression does not produce a value"

Code Snippet

Dim js As Action(Of Person) = _

Function(p As Person) Console.WriteLine("> Name = {0}, Age = {1}", p.Name, p.Age)

people.ForEach(js)

I did find a bug report (291473) for this exact circumstance which was resolved as By Design.

I 'm sure that this is because I am not passing back a value or instance to be assigned to the var 'js'. But what stumps me is how can I use the lambda in place of System.Action? Is it because the lambda is compiled as a delegate?

[1897 byte] By [JoshBernard] at [2008-2-25]
# 1

I am not familiar with Action(Of Person) but my guess is that it has a return value. If that is the case the problem is that the lambda you created calls a method with no return value. Trying to assign that to a delegate with a return value is not possible.
JaredParsonsMSFT at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,Visual Basic Orcas...

Visual Studio Orcas

Site Classified