VB.net–>C#

來源:互聯網
上載者:User

最近在看書時看到一段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;
    }

   }
不知道大家有沒有更好的寫法,

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.