locked
Anonymous Methods in VB.NET

    Question

  • Hi,

    Do we have Anonymous Methods in VB.NET as we have in C#. How we need to write these Anonymous methods in VB.NET

    Sreejith

     

    Sunday, September 7, 2008 2:10 PM

Answers

  • Yes, the syntax is

    Code Snippet

    Function (arguments) expression

     

    here is an example

    Code Snippet

    Dim words As String() = New String() {"foo", "bar", "baz"}

    For Each word As String In words.Where(Function(w) w.StartsWith("b"))

      Console.WriteLine(word)

    Next

     

    that outputs

    bar
    baz

     

    Documentation is here: http://msdn.microsoft.com/en-us/library/bb531253.aspx

     

    Sunday, September 7, 2008 4:36 PM
  • VB.NET doesn't support multi-statement lambda expressions or anonymous methods.

    Joe
    Monday, September 8, 2008 5:17 AM

All replies

  • Yes, the syntax is

    Code Snippet

    Function (arguments) expression

     

    here is an example

    Code Snippet

    Dim words As String() = New String() {"foo", "bar", "baz"}

    For Each word As String In words.Where(Function(w) w.StartsWith("b"))

      Console.WriteLine(word)

    Next

     

    that outputs

    bar
    baz

     

    Documentation is here: http://msdn.microsoft.com/en-us/library/bb531253.aspx

     

    Sunday, September 7, 2008 4:36 PM
  • Hi Martin,

    What you have mentioned is Lamda experssions right? In VB.NET this expression should be a single line statement right? Do we can multiple line logic in the expression part?

     

    Sreejith

    Monday, September 8, 2008 4:46 AM
  • VB.NET doesn't support multi-statement lambda expressions or anonymous methods.

    Joe
    Monday, September 8, 2008 5:17 AM
  • I think you missed at present....let's be optimistic now!! Smile

    Tuesday, November 11, 2008 3:03 AM
  • Better late than never...

    There is the possiblity that you can get around the multi-line Lamda Expression limitation using extension methods on the input parameter (if there is only one input paramter).

        <Fact()>
        Public Sub ShouldDemonstrateLoggingUsingLambdaExpression()
            Dim mathEngine = New SimpleMath2
            mathEngine.SetLoggingCallbackMethod(Sub(message) _
                                                               message.MultiLineExtensionMethodReplacement())
            Dim retVal = mathEngine.Add(10, 10)
            Assert.Equal(20, retVal)
        End Sub

    Public Module MyExtension
        <System.Runtime.CompilerServices.Extension()>
        Public Sub MultiLineExtensionMethodReplacement(ByRef message As String)
            Console.WriteLine(message) ' pretend this is adequate logging :)
            Dim array2 = message.ToArray()
            Array.Reverse(array2)
            Dim newMessage = New String(array2)
            Console.WriteLine(newMessage)
        End Sub
    End Module

    This may help in simple situations.


    Dave White
    Wednesday, May 19, 2010 11:27 PM
  • I'd hope the following would also work if you want multi-line (haven't tried though)

    For Each word As String In words.Where(Function(w) : Return w.StartsWith("b") : End Function)
    
       Console.WriteLine(word)
    
    Next
    
    

    and also (note the : followed by _ )

    For Each word As String In words.Where(_
      Function(w) :_
        Return w.StartsWith("b") :_
      End Function)
    
       Console.WriteLine(word)
    
    Next
    
    


    Microsoft MVP J# 2004-2010, Borland Spirit of Delphi 2001
    Tuesday, October 25, 2011 6:44 PM