简体   繁体   中英

C# converted from VB doesn't work

I used code converter to go from VB to C# and I get errors in c#. specifically, error on Item and on string.join(",", Flop.ToArray) . Error says it doesn't contain a definition for item but it works in VB.

VB

Dim Flop As New List(Of String)
    For x As Integer = 0 To Dataset9.Tables(0).Rows.Count - 1 'ROWS
        Flop.Add(Dataset9.Tables(0).Rows(x).Item("Id"))
    Next
    strAllRoleNames = String.Join(",", Flop.ToArray)

C#

List<string> Flop = new List<string>();

    for (int x = 0; x <= Dataset9.Tables[0].Rows.Count - 1; x++)
      {
         Flop.Add(Dataset9.Tables[0].Rows[x].Item["Id"]);
      }
 strAllRoleNames = string.Join(",", Flop.ToArray);

Try this:

List<string> Flop = new List<string>();

    for (int x = 0; x <= Dataset9.Tables[0].Rows.Count - 1; x++)
      {
         Flop.Add(Dataset9.Tables[0].Rows[x]["Id"].ToString());
      }
 strAllRoleNames = string.Join(",", Flop.ToArray());

They three keys that were missing here

  1. Accessing the item in a row, you need to use the C# default indexer as Item doesn't exist in C#
  2. Since the cell in a row is an object and you want a string, need to explicitly call ToString
  3. When calling ToArray, you need the () at the end in C#

尝试...

Flop.Add(Dataset9.Tables[0].Rows[x]["Id"].ToString());

ToArray is a method()

List<string> Flop = new List<string>();

    for (int x = 0; x <= Dataset9.Tables[0].Rows.Count - 1; x++)
      {
        Flop.Add(Dataset9.Tables[0].Rows[x]["Id"]);
      }
 strAllRoleNames = string.Join(",", Flop.ToArray());

In a more concise way you can try below instead:

strAllRoleNames = string.Join(",", Dataset9.Tables[0].AsEnumerable()
                                   .Select(C => Convert.ToString(C["Id"]))
                                   .ToArray());

Try the below changes:

Dataset9.Tables[0].Rows[x].Item["Id"] => Dataset9.Tables[0].Rows[x]["Id"]

Flop.ToArray => Flop.ToArray()    

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