简体   繁体   English

使用转换后的VB转换为C#代码

[英]working with converted VB to C# code

I have the following VB code: 我有以下VB代码:

Dim dt As DataTable = DAL.WMS_Collaboration_Fetch(0).Tables(0)

If dt.Rows.Count > 0 Then
    'Bind Dataset to Gridview
    Dim _WMS_CollaborationInfo As New WMS_CollaborationInfo
    With _WMS_CollaborationInfo
        .CollaborationName = dt.Rows(0).Item["CollaborationName").ToString
        .CollaborationID = dt.Rows(0).Item("CollaborationID").ToString
    End With

converted it to C# code like this: 将它转换为C#代码,如下所示:

DataTable dt = DAL.WMS_Collaboration_Fetch(0).Tables(0);

if (dt.Rows.Count > 0) {
    //Bind Dataset to Gridview
    WMS_CollaborationInfo _WMS_CollaborationInfo = new WMS_CollaborationInfo();
    {
        _WMS_CollaborationInfo.CollaborationName  = dt.Rows[0].Item["CollaborationName"].ToString;
        _WMS_CollaborationInfo.CollaborationID = dt.Rows[0].Item["CollaborationID"].ToString;
    }

I am, however, unable to run the C# code. 但是,我无法运行C#代码。 In VB, the table column, using DataTable, is accessed by just passing in the column name (well, I dont know much about VB) eg "CollaborationID" in 在VB中,使用DataTable的表列只需传入列名即可访问(好吧,我对VB不太了解),例如“CollaborationID”in

dt.Rows(0).Item("CollaborationID").ToString

Please what is the C# equivalent of reading the data from the table using DataTable? 请问使用DataTable从表中读取数据的C#等价物是什么? I simply mean how can i re-write my C# code so that it works. 我只是说我如何重新编写我的C#代码以便它可以工作。

Indexers in C# use square brackets - [] . C#中的索引器使用方括号 - [] You also don't need to access .Item : 您也不需要访问.Item

_WMS_CollaborationInfo.CollaborationID = 
           dt.Rows[0]["CollaborationID"].ToString();

In C# there is a distinction between methods and indexers. 在C#中,方法和索引器之间存在区别。 The first use parenthesis, the second squared braces. 第一个使用括号,第二个使用括号。 This took me a while to grasp while moving from VB where you use parenthesis for both. 从VB中移动时,我花了一些时间来掌握这两个问题。

Try: 尝试:

dt.Rows[0].Item["CollaborationID"].ToString()

Edit: VB.NET and C# Comparison 编辑:VB.NET和C#比较

I used to refer to this cheat sheet all the time, check out the Arrays section: http://www.harding.edu/fmccown/vbnet_csharp_comparison.html 我曾经一直参考这个备忘单,查看Arrays部分: http//www.harding.edu/fmccown/vbnet_csharp_comparison.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM