我正在尝试将一些C#代码移植到VB,而我认为C#代码的VB版本不会编译但是会抛出错误:
42:10
在本地变量声明中无效。
在此代码中,自定义用户控件的实例被添加到WithEvents
的子类中;要添加的控件数等于数据源中的项数; eventhandlers附加到用户控件中声明的事件:
这是原始的C#代码:
TableLayout
这是对应的VB代码:
private void BindUI ()
{
while (this.Controls.Count < DataSource.DefaultView.Count)
{
var uc = new FooControl ();
uc.BeginEditing += uc_BeginEditing;
uc.DoUpdate += uc_DoUpdate;
uc.DoDelete += uc_DoDelete;
uc.Name = "uc" + this.Controls.Count.ToString ();
this.Controls.Add (uc);
this.SetRow (uc, this.Controls.Count - 1);
this.SetColumn (uc, 0);
}
}
在VB.Net中将侦听器附加到Private Sub BindUI()
While Me.Controls.Count < DataSource.DefaultView.Count
Dim WithEvents uc As New FooControl ' INVALID CODE
AddHandler uc.BeginEditing, AddressOf uc_BeginEditing
AddHandler uc.DoUpdate, AddressOf uc_DoUpdate
AddHandler uc.DoDelete, AddressOf uc_DoDelete
uc.Name = "uc" + Me.Controls.Count.ToString()
Me.Controls.Add(uc)
Me.SetRow(uc, Me.Controls.Count - 1)
Me.SetColumn(uc, 0)
End While
End Sub
的这些实例的正确方法是什么?