Shittu Joseph Olugbenga
Shittu Joseph Olugbenga

Reputation: 6476

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) e.g "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.

Upvotes: 2

Views: 676

Answers (2)

Oded
Oded

Reputation: 498942

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

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

Upvotes: 5

Ulises
Ulises

Reputation: 13419

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

Upvotes: 5

Related Questions