I'm trying to find an equivalent of VB's "System.Data.DataRow.Item" but I can't find one. I'm re-writing VB code to C# and I'm new to C#. Noob question, probably. I figured you guys would have some good insight. The code snippet is below. I found another stack overflow post with a similar question but the answers weren't helpful to me so I'm posting this.
Here's the error as well: Error CS1061 'DataRow' does not contain a definition for 'Item' and no accessible extension method 'Item' accepting a first argument of type 'DataRow' could be found (are you missing a using directive or an assembly reference?)
.......................
// C# code
if (Reader.HasRows) // check that data exists
{
var winshare = new DataTable();
winshare.Load(Reader);
foreach (DataRow row in winshare.Rows)
{
string path = row.Item["List_Item"];
path = path @"\Out";
GlobalVariables.pwc = row.Item["Sublist_Id"];
.......................
.......................
// VB code
If Reader.HasRows Then // check that data exists
Dim winshare As DataTable = New DataTable
winshare.Load(Reader)
For Each row As DataRow In winshare.Rows
Dim path As String = CStr(row.Item("List_Item"))
path = path "\Out"
pwc = CStr(row.Item("Sublist_Id")) // Used to determine archive path also
.......................
CodePudding user response:
VB has default properties and C# has indexers. In VB, you can specify the default property explicitly, e.g.
pwc = CStr(row.Item("Sublist_Id"))
or implicitly:
pwc = CStr(row("Sublist_Id"))
C# indexers are basiclly the same as the implicit option:
pwc = (string)row["Sublist_Id"];
In either language, I would tend to recommend a bit of LINQ to DataSet anyway:
pwc = row.Field(Of String)("Sublist_Id")
pwc = row.Field<string>("Sublist_Id");
CodePudding user response:
In C# you need to use brackets [] not parens () as they aren't used in this fashion.
row.Item["List_Item"].ToString();
The Item method appears to be there otherwise.
https://docs.microsoft.com/en-us/dotnet/api/system.data.datarow.item?view=net-6.0