Does VB.NET have anonymous functions?
VB9 has only single-line anonymous functions. We're adding full statement and multi-line lambdas in VB10.
Visual Basic .NET has only lambda expressions.
It does not support 'anonymous delegates" in the current version, though it will (and on multiple lines at that) in VS2010.
Right now the only option is to declare your method somewhere and pass it with the Addressof operator.
It does in VB10:
Dim food = New With {
.ID = 1,
.Name = "Carrot",
.Type = (
Function(name As String)
If String.IsNullOrEmpty(name) Then Return String.Empty
Select Case name.ToLower()
Case "apple", "tomato": Return "Fruit"
Case "potato": Return "Vegetable"
End Select
Return "Meat"
End Function
)(.Name)
}
Dim type = food.Type
Or, corresponding to your code:
Sub HandleErrors(codeBlock As Action)
Try
codeBlock()
Catch e As Exception
' log exception, etc.
End Try
End Sub
HandleErrors(Sub()
Dim x = foo()
x.DoStuff()
' etc.
End Sub)