Fighting VB.net Lambdas

I am trying to use lambdas in some VB.Net code, in fact, I am trying to set a flag when calling databound.

A simplified expression looks like this:

Dim dropdownlist As New DropDownList()
dropdownlist.DataSource = New String() {"one", "two"}
Dim databoundCalled As Boolean = False
AddHandler dropdownlist.DataBound, Function(o, e) (databoundCalled = True)
dropdownlist.DataBind()

My understanding is that the databoundCalled variable should be set to true, it is clear that I am missing something, since the variable always remains false.

What do I need to do to fix this?

+5
source share
3 answers

A single Lambdas line in vb.net is ALWAYS an expression, what makes your lambda expression basically says: databoundCalled = True or (databoundCalled == True) if your guy is C # and not the given databoundCalled = True

+3
source

, , . , , , . , . , , true/false. .

-:

Partial Public Class _Default
    Inherits System.Web.UI.Page

    Dim databoundCalled As Boolean = False
    Dim dropdownlist As New DropDownList()

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.Write(databoundCalled)
        Bind()
        Response.Write(databoundCalled)

    End Sub

    Sub Bind()
        AddHandler dropdownlist.DataBound, Function(o, e) (SetValue(True))

        dropdownlist.DataSource = New String() {"one", "two"}
        dropdownlist.DataBind()
    End Sub

    Function SetValue(ByVal value As Boolean) As Boolean
        databoundCalled = value
        Return value
    End Function
End Class

, !

+7

, . VS2008 lambda , .

Dim x = 42
Dim del = Function() x = 32
del()

- . x 32. , VB , , VB.

. VS2010,

Dim del = Function()
           x = 32
          End Function

, , lambda, .

+1
source

All Articles