简体   繁体   中英

working with converted VB to C# code

I have the following VB code:

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:

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. 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

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

Please what is the C# equivalent of reading the data from the table using DataTable? I simply mean how can i re-write my C# code so that it works.

Indexers in C# use square brackets - [] . You also don't need to access .Item :

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

In C# there is a distinction between methods and indexers. 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.

Try:

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

Edit: VB.NET and C# Comparison

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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