与 VB .net Lambda 作斗争
我试图在一些 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。
我需要做什么来修复它?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在查看了您的代码并挠头之后,我找到了一个可行的解决方案。 现在,为什么这对你所拥有的有效,我不清楚。 也许这至少会帮助您朝正确的方向发展。 关键的区别是我有一个将值设置为 true/false 的方法。 其他一切都一样。
这是我的整个网络项目代码:
我希望这有帮助!
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:
I hope this helps!
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
问题在于如何解释 lambda。 在 VS2008 中,Function lambda 始终被解释为表达式而不是语句。 以下面的代码块为例,
在这种情况下,lambda del 内部的代码并没有进行赋值。 相反,它是在变量 x 和值 32 之间进行比较。原因是 VB 没有表达式即赋值的概念,只有语句可以是 VB 中的赋值。
为了在 lambda 表达式中进行赋值,您必须具有语句功能。 这直到 VS2010 才可用,但是当它可用时,您可以执行以下操作
本质上,任何不是单行 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
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
Essentially anything that is not a single line lambda is interpreted as a statement.