Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C#: Why no 'Item' on System.Data.DataRow?

Tags:

c#

system.data

I'm rewriting/converting some VB-Code:

Dim dt As New System.Data.DataTable()
Dim dr As System.Data.DataRow = dt.NewRow()
Dim item = dr.Item("myItem")

C#:

System.Data.DataTable dt = new System.Data.DataTable();
System.Data.DataRow dr = dt.NewRow();
var item = dr.Item["myItem"];

I can't make it run under C#, the problems I have is the third row var item = dr.Item["myItem"];:

System.Data.DataRow' does not contain a definition for 'Item' and no extension method 'Item' accepting a first argument of type 'System.Data.DataRow' could be found (are you missing a using directive or an assembly reference?)

I referenced System.Data Version 4 in both projects. What am I missing here? Note: ItemArray exists in both...

like image 208
sl3dg3 Avatar asked Oct 24 '11 10:10

sl3dg3


People also ask

What does << mean in C?

<< is the left shift operator. It is shifting the number 1 to the left 0 bits, which is equivalent to the number 1 .


1 Answers

There is actually no "Item" property in C#. In VB the DataRow cell access is defined like this:

Default Public Property Item (
    column As DataColumn
) As Object

So there is a literal "Item" property. However, in C# it is defined like this:

public object this[
    DataColumn column
] { get; set; }

So this is the default property of the class / object. So you access it with the object name.

like image 57
Pax Avatar answered Sep 29 '22 17:09

Pax