与 VB .net Lambda 作斗争

发布于 2024-07-23 12:05:53 字数 426 浏览 3 评论 0 原文

我试图在一些 VB.Net 代码中使用 lambda,本质上我试图在调用数据绑定时设置一个标志。

简化后看起来像这样:

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()

我的理解是 databoundCalled 变量应该设置为 true,显然我错过了一些东西,因为该变量始终保持 false。

我需要做什么来修复它?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

生死何惧 2024-07-30 12:05:53

在查看了您的代码并挠头之后,我找到了一个可行的解决方案。 现在,为什么这对你所拥有的有效,我不清楚。 也许这至少会帮助您朝正确的方向发展。 关键的区别是我有一个将值设置为 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

我希望这有帮助!

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!

四叶草在未来唯美盛开 2024-07-30 12:05:53

vb.net 中的单行 Lambda 始终是表达式,您的 lambda 表达式所做的基本上是说 databoundCalled = True 或 (databoundCalled == True) 如果您的 ac# 人员未设置 databoundCalled = True

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

緦唸λ蓇 2024-07-30 12:05:53

问题在于如何解释 lambda。 在 VS2008 中,Function lambda 始终被解释为表达式而不是语句。 以下面的代码块为例,

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

在这种情况下,lambda del 内部的代码并没有进行赋值。 相反,它是在变量 x 和值 32 之间进行比较。原因是 VB 没有表达式即赋值的概念,只有语句可以是 VB 中的赋值。

为了在 lambda 表达式中进行赋值,您必须具有语句功能。 这直到 VS2010 才可用,但是当它可用时,您可以执行以下操作

Dim del = Function()
           x = 32
          End Function

本质上,任何不是单行 lambda 的内容都会被解释为语句。

The problem is how lambdas are interpreted. In VS2008 a Function lambda is always interpreted as an expression and not a statement. Take the following block of code as an example

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

In this case, the code inside the lambda del is not doing an assignment. It is instead doing a comparison between the variable x and the value 32. The reason why is that VB has no concept of an expression that is an assignment, only a statement can be an assignment in VB.

In order to do an assignment in a lambda expression you must have statement capabilities. This won't be available until VS2010 but when it is you can do the following

Dim del = Function()
           x = 32
          End Function

Essentially anything that is not a single line lambda is interpreted as a statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文