Results 1 to 4 of 4

Thread: [RESOLVED] Lambda Expression Confusion

  1. #1

    Thread Starter
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    Resolved [RESOLVED] Lambda Expression Confusion

    Hi all,
    I've been wondering about lambda expressions for a while now.
    Consider the following code
    vb.net Code:
    1. Sub Main()
    2.         Dim I As Integer
    3.         SomeUselessSub(Sub()
    4.                            Dim K As Integer = I
    5.                        End Sub)
    6.  
    7.         SomeUselessSub(New Action(AddressOf DoSomething))
    8.     End Sub
    9.     Sub DoSomething()
    10.  
    11.     End Sub
    12.  
    13.     Sub SomeUselessSub(ByVal D As [Delegate])
    14.  
    15.     End Sub
    Both
    Code:
            SomeUselessSub(Sub()
                               Dim K As Integer = I
                           End Sub)
    And
    Code:
     SomeUselessSub(New Action(AddressOf DoSomething))
    Are valid calls to SomeUselessSub.
    But in the lambda expression I have access to all Sub Main's variables. Is it safe to use them ?

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Lambda Expression Confusion

    Since you can only use those variables in the lambda when calling the lambda from Sub Main, I would say yes it is safe to use them. It is really no different than passing them in as byref parameters (for value types) or simply just passing them in (for reference types).

  3. #3

    Thread Starter
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    Re: Lambda Expression Confusion

    I suspected as much. Thanks for confirming

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

  4. #4
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    710

    Re: [RESOLVED] Lambda Expression Confusion

    Just be aware that the value of the 'captured' local will be the value when the lambda is invoked, not declared.
    e.g.
    Code:
    Dim I As Integer = 0
    Dim o = Sub()
                    Dim K As Integer = I
               End Sub
    I = 1
    o()	'K within the lambda will be assigned 1, not 0
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width