将 C# 语句体 lambda 转换为 VB
VS8 中的 VB 似乎不支持/转换带有语句主体的 lambda 表达式。我在 C# 应用程序中使用它们,但现在必须将其转换为 VB。
我正在动态创建一大堆控件,并且我希望能够动态地为它们提供事件处理程序。这样我就可以从数据库构建动态用户界面。在下面的代码中,我将创建一个表单和一个复选框,使复选框控制表单的可见性,向表单添加一些方法处理程序,然后将新创建的复选框添加到预先存在的表单/面板/等中。例如,表单的处理程序会影响复选框:
// 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 表达式
我真的很喜欢动态创建所有内容的能力,然后让对象自行处理 - 我不需要添加任何特殊函数来确定给出的形式关闭事件,以便它可以搜索正确的复选框并更新该复选框 - It Just Works (TM)。
不幸的是它需要转换为VB。
将 lambda/匿名语句体转换为可在 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.
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
Of course, I'm getting the conversion error:
VB does not support anonymous methods/lambda expressions with a statement body
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).
Unfortunately it needs to be converted to VB.
What is the best way to convert lambda/anonymous statement bodies into something that will work in VB, especially when one needs to create many of them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
等待.NET 4的最新版本,它将支持VB中的此类功能。看不到其他选择。
丑陋的替代方案是:
这可行,但您可以在函数中使用单个语句。
创建一些常规的 Sub Foo
并使用
AddHandler
将其绑定到事件Wait for the nearest release of .NET 4, it will support things like this in VB. Don't see other alternative.
Ugly alternatives are:
This work, but you can use a single statement in a function.
Create some regular Sub Foo
and use
AddHandler
to bind it to an event您能否创建一个新类,在构造函数中接受
Form
并将chkSelectPanel
作为字段,从而允许您使用实例方法作为事件处理程序?Could you make a new class that accepts the
Form
in the constructor and haschkSelectPanel
as a field, allowing you to use instance methods as your event handlers?