-
VB.net-->C#
最近在看书时看到一段VB.Net的代码.
Select Case itemType
Case ListItemType.Header
cell.Text = HeaderText
Case ListItemType.Item, ListItemType.AlternatingItem
AddHandler cell.DataBinding, AddressOf ItemDataBinding
Case ListItemType.EditItem
AddHandler cell.DataBinding, AddressOf EditItemDataBinding
Dim DDL As New DropDownList
cell.Controls.Add(DDL)
End Select
想把这段代码翻译成C#,碰到了两个问题.一,当使用开关语句时,符合两种情况执行相同的代码,VB.net中用个逗号分隔就OK了( Case ListItemType.Item, ListItemType.AlternatingItem),而C#中似乎没有这种写法.今天偶然看到一段代码,发现连写连个case中间不加break就OK了.二,关于AddHandler cell.DataBinding, AddressOf ItemDataBinding的含义,在查找资料后发现是VB中关联事件和方法的写法,类似C#中的事件订阅.翻译后的C#代码如下:
switch (itemType)
{
case ListItemType.Header:
{
cell.Text=HeaderText;
break;
}
case (ListItemType.Item):
case(ListItemType.AlternatingItem):
{
cell.DataBinding += new System.EventHandler(ItemDataBinding);
break;
}
// case(ListItemType.AlternatingItem):
// {
// cell.DataBinding += new System.EventHandler(ItemDataBinding);
// break;
// }
case(ListItemType.EditItem):
{
cell.DataBinding += new System.EventHandler(EditItemDataBinding);
DropDownList ddl=new DropDownList();
cell.Controls.Add(ddl);
break;
}
}
不知道大家有没有更好的写法,
出处:https://www.cnblogs.com/renrenqq/archive/2004/05/26/11558.html