Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Struggling with VB .net Lambdas

Tags:

vb.net

lambda

I'm trying to use lambdas in some VB.Net code, essentially I'm trying to set a flag when databound is called.

Simplified it 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, clearly I'm missing something as the variable always remains false.

What do I need to do to fix it?

like image 614
ilivewithian Avatar asked Jun 10 '09 08:06

ilivewithian


2 Answers

After looking over your code and scratching my head, I found a solution that works. Now, why this works over what you have, I am not clear. Maybe this will at least help you in the right direction. The key difference is I have a method that sets the value to true/false. Everything else is the same.

Here is my entire web project code:

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

I hope this helps!

like image 62
CodeLikeBeaker Avatar answered Nov 12 '22 18:11

CodeLikeBeaker


Single line Lambdas in vb.net ALWAYS are expressions , what your lambda expression is doing is basically saying does databoundCalled = True or (databoundCalled == True) if your a c# guy , not set databoundCalled = True

like image 43
almog.ori Avatar answered Nov 12 '22 18:11

almog.ori