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?
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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With