From what I can find on google, VB.NET only has one-statement lambdas, and not multi-statement anonymous functions. However, all the articles I read were talking about old versions of VB.NET, I couldn't find anything more recent than vs2008 beta 1 or 2.
So the question: How can I do this in VB.NET?
C# code:
private void HandleErrors( Action codeBlock ){
try{
codeBlock();
}catch(Exception e){
//log exception, etc
}
}
HandleErrors(() => {
var x = foo();
x.DoStuff();
etc
});
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)