Does VB.NET have anonymous functions?
Asked Answered
H

6

16

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
});
Healy answered 22/3, 2009 at 21:19 Comment(0)
C
35

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)
Cassel answered 22/5, 2011 at 23:44 Comment(2)
great example ... though I didn't realize carrots were meat :)Transferase
carrots: try the only orange meat!Zambrano
N
18

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.

Norwood answered 22/3, 2009 at 21:25 Comment(2)
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...Norwood
Here is a good article from cOdE mag the talks about anonymous methods and lambda expressions code-magazine.com/Article.aspx?quickid=0809081Undone
C
9

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

Colostrum answered 22/3, 2009 at 21:25 Comment(0)
I
0

in this example I have a list of operations but only want to find one from a list (of T) where the IDs match:

Return operations.Find(Function(p) p.OperationID = operationID)

operationID is a local variable passed in to the method and operations is a generic list.

Inductive answered 26/11, 2013 at 10:24 Comment(0)
R
-1

Anonymous isn't a delegate or function it's a strong dynamical type

you can use generic functions

Sub Main()
      Dim PersonDynamicType = AnonyFunc(New With {.Name = "david", .Family = "Fisher"})
      Console.Write(PersonDynamicType.Name)
End Sub

Function AnonyFunc(Of t)(v As t) As t
      Return v
End Function
Runt answered 20/6, 2015 at 13:10 Comment(1)
If it's strongly typed, then in what way is it dynamic? But an anonymous type is just that -- a type which doesn't have a name in the source code of the program. The compiler assigns it a name, but you cannot directly use this name from code.Brassard
G
-5

This is inaccurate. VB.NET does in fact have anonymous methods. Here is an example:

Private Function JsonToObject(Of T)(Value As String) As T
    Dim JavaScriptSerializer As New System.Web.Script.Serialization.JavaScriptSerializer()
    Return JavaScriptSerializer.Deserialize(Of T)(Value)
End Function

Dim People As Generic.List(Of Person) = JsonToObject(Of Generic.List(Of Person))(Json)
Gallon answered 6/2, 2012 at 14:5 Comment(1)
It's called JsonToObject. Sorry couldn't help myself :PMinnieminnnie

© 2022 - 2024 — McMap. All rights reserved.