将C#语句体lambda转换为VB [英] Convert C# statement body lambda to VB
问题描述
看起来VS8中的VB不支持/转换带有语句体的lambda表达式。我在C#应用程序中使用它们,但现在必须将其转换为VB。
It appears that VB in VS8 doesn't support/convert lambda expressions with a statement body. I'm using them in my C# application, but now must convert it to VB.
我正在动态创建一大堆控件,我想要在飞行中给他们事件处理程序。这样就可以从数据库中构建一个动态的用户界面。在下面的代码中,我将创建一个表单和一个复选框,使复选框控制窗体的可见性,在窗体中添加几个方法处理程序,然后将新创建的复选框添加到一个预先存在的窗体/面板/ etc中。例如,处理程序会影响复选框:
I'm creating a whole bunch of controls dynamically, and I want to be able to give them event handlers on the fly. This is so I can build a dynamic user interface from a database. In the following code I'll create a form and a checkbox, make the checkbox control the form's visibility, add a few method handlers to the form, and then add the newly created checkbox to a pre-existing form/panel/etc. Handlers for the form, for instance, affect the checkbox:
// Inside some loop creating a lot of these forms and matching checkboxes
Form frmTemp = frmTestPanels[i]; // New form in array
CheckBox chkSelectPanel; // New checkbox that selects this panel
chkSelectPanel = new CheckBox();
chkSelectPanel.Text = SomeName; // Give checkbox a label
chkSelectPanel.Click += (ss, ee) => // When clicked
{
label1.Text = SomeName; // Update a label
if (chkSelectPanel.Checked) // Show or hide the form
{
frmTemp.Show();
}
else
{
frmTemp.Hide();
}
};
frmTemp.VisibleChanged += (ss, ee) => // When form visibility changes
{
chkSelectPanel.Checked = frmTemp.Visible; // Reflect change to checkbox
ConfigurationFileChanged = true; // Update config file later
};
frmTemp.FormClosing += (ss, ee) => // When the form closes
{ // We're only pretending to close the form - it'll sit around until needed
chkSelectPanel.Checked = false; // Update the checkbox
frmTemp.Hide(); // Hide the form
ee.Cancel = true; // Cancel the close
};
flpSelectGroup.Controls.Add(chkSelectPanel); // Add checkbox to flow layout panel
// End of loop creating a bunch of these matching forms/checkboxes
当然,我得到转换错误,VB不支持匿名方法/ lambda表达式与语句正文
Of course, I'm getting the conversion error, "VB does not support anonymous methods/lambda expressions with a statement body"
我真的很喜欢能够在飞行中创建所有内容,然后让对象处理自己 - 我不需要添加任何特殊的功能来确定哪个窗体提供关闭事件,以便它可以搜索正确的复选框并更新复选框 - 它Just Works(TM)。
I really liked the ability to create everything on the fly, and then let the objects handle themselves - I don't need to add any special functions that figure out which form is giving the close event so it can search for the right checkbox and update the checkbox - It Just Works (TM).
不幸的是,它需要转换为VB。
Unfortunately it needs to be converted to VB.
- 将lambda /匿名语句体转换为可以在VB中工作的东西,最好的方式是什么?特别是当需要创建许多语句的时候,特别是当需要创建许多语句的时候?
-Adam
推荐答案
等待最接近的.NET 4版本,它将在VB中支持这样的事情。没有看到其他选择。
Wait for the nearest release of .NET 4, it will support things like this in VB. Don't see other alternative.
丑陋的选择是:
-
这个工作,但是你可以在一个函数中使用一个语句。
This work, but you can use a single statement in a function.
AddHandler Me.Click, Function(o, e) MessageBox.Show("text")
创建一些常规的Sub Foo
Create some regular Sub Foo
Public Sub Foo(ByVal o As Object, ByVal e As EventArgs)
MessageBox.Show("text")
End Sub
并使用 AddHandler
将其绑定到一个事件
and use AddHandler
to bind it to an event
AddHandler Me.Click, AddressOf Foo
这篇关于将C#语句体lambda转换为VB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!