Results 1 to 4 of 4

Thread: [RESOLVED] Var access in anonymous functions

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,372

    Resolved [RESOLVED] Var access in anonymous functions

    can someone explain to me how access to vars in anonymous functions is handled?

    having the following:
    Code:
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            Dim btn As New Button
            Controls.Add(btn)
            Dim i As Int32 = 1
            AddHandler btn.Click, Sub() MessageBox.Show(i.ToString)
    
            btn = New Button With {.Top = 20}
            Controls.Add(btn)
            i = 2
            AddHandler btn.Click, Sub() MessageBox.Show(i.ToString)
        End Sub
    both buttons showing "2" suggests that i is compiled byref in both anonymous functions, so the compiled code of both subs contains a pointer to i, right? but that would mean that i is preserved even when Form1_Load is exited, something that goes against the normal var scope rules. one other explanation for the observed behaviour would be that both subs are only compiled at the end for Form_Load, but that seems odd as well.
    If the second "i" is replaced by "j", then the behaviour is that one button shows "1", the other "2".

    then this one:
    Code:
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            Dim btn As New Button
            Controls.Add(btn)
            Dim o As New Dummy With {.Value = 1}
            AddHandler btn.Click, Sub() MessageBox.Show(o.Value.ToString)
    
            btn = New Button With {.Top = 20}
            Controls.Add(btn)
            o = New Dummy With {.Value = 2}
            AddHandler btn.Click, Sub() MessageBox.Show(o.Value.ToString)
        End Sub
    
        Private Class Dummy
            Property Value As Int32
        End Class
    suggests that "o" (which itself is a reference to a class instance) within the anonymous subs is byref, wait, that gives me an idea, how about this:
    Code:
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            Dim btn As New Button
            Controls.Add(btn)
            Dim o As New Dummy With {.Value = 1}
            AddHandler btn.Click, Sub() MessageBox.Show(o.Value.ToString)
    
            btn = New Button With {.Top = 20}
            Controls.Add(btn)
            o = New Dummy With {.Value = 2}
            AddHandler btn.Click, Sub()
                                      o.Value += 1
                                      MessageBox.Show(o.Value.ToString)
                                  End Sub
        End Sub
    
        Private Class Dummy
            Property Value As Int32
        End Class
    now that is really kind of strange as both subs use the same class instance even though there was a new instance created before the second sub was created. so is it really holding back compilation of anonymous functions until the end of the sub??

    thanks for any insight.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Var access in anonymous functions

    You should do some reading on closures in .NET and come back with any specific questions you have after that.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,372

    Re: Var access in anonymous functions

    ok, learned that Java is doing it in the way i would have expected. not sure of my feelings about the way .net handles it. can be very beneficial but also a pitfall.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,372

    Re: Var access in anonymous functions

    [removed wrong assumption]

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