Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does VB.NET have anonymous functions?

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
});
like image 536
Orion Edwards Avatar asked Mar 22 '09 21:03

Orion Edwards


People also ask

Which function is also known as anonymous function?

In Python, an anonymous function is a function that is defined without a name. While normal functions are defined using the def keyword in Python, anonymous functions are defined using the lambda keyword. Hence, anonymous functions are also called lambda functions.

Is lambda the same as anonymous function?

Anonymous functions, lambda expressions, or function literals are all the same thing. Lambda (or \lambda) is the name given to anonymous functions in some languages like Python. These are functions not bound by an explicit identifier.

Does C# have anonymous functions?

Anonymous MethodAnonymous methods in C# can be defined using the delegate keyword and can be assigned to a variable of delegate type.

What is a lambda function vb net?

A lambda expression is a function or subroutine without a name that can be used wherever a delegate is valid. Lambda expressions can be functions or subroutines and can be single-line or multi-line. You can pass values from the current scope to a lambda expression.


3 Answers

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)
like image 90
Sam Avatar answered Sep 23 '22 17:09

Sam


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.

like image 32
Denis Troller Avatar answered Sep 23 '22 17:09

Denis Troller


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

like image 28
Dustin Campbell Avatar answered Sep 24 '22 17:09

Dustin Campbell