views:

1297

answers:

2

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
});
+7  A: 

VB9 has only single-line anonymous functions. We're adding full statement and multi-line lambdas in VB10.

Dustin Campbell
+8  A: 

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.

Denis Troller
Hrm. It seems VB.NET really is the red-headed stepchild of the .net world. C# has had multi line delegates since VS2005!
Orion Edwards
unfortunately this is a big gaping hole I think. I can't wait for this to be included.On the other hand, they did a nice job on the query syntax XML litterals and LinqToXML support...
Denis Troller
Here is a good article from cOdE mag the talks about anonymous methods and lambda expressions http://www.code-magazine.com/Article.aspx?quickid=0809081
Tom Alderman