explanation of datatable code

Abdoul Rahim

Member
Joined
Oct 16, 2017
Messages
15
Programming Experience
Beginner
Can somme one explaine me this " if (dt.row [0][0].tostring()=="this"){console.write ("this");}. High Tof or culumns those it refers To.?
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
2,741
Location
Sydney, Australia
Programming Experience
10+
High Tof or culumns those it refers To.?
I have no idea what you just said.
Can somme one explaine me this " if (dt.row [0][0].tostring()=="this"){console.write ("this");}.
That is almost certainly not a faithful representation of the code you want explained. It is a very good idea to copy and paste code so that you know that you're getting EXACTLY the right thing. If you type it out by hand then you can make mistakes, and you have made at least two there I think. I believe that the actual code would be this:
if (dt.Rows[0][0].ToString() == "this") { Console.Write ("this"); }

Presumably 'dt' is a DataTable, in which case there is no member named 'row' but there is one named 'Rows'. Just like a database table has columns that describe the data (type, size, etc) and rows that contain the data, so a DataTable has a Columns property that returns a DataColumnCollection that contains DataColumn objects that describe the data and a Rows property that returns a DataRowCollection that contains DataRow objects that contain the data.

In C#, as with C/C++, brackets are used to index. C# arrays and collections have elements and items at indexes 0 to (n - 1) where 'n' is the length of the array or collection. You provide the index of the element or item you want to refer to inside brackets. This:
dt.Rows[0]
is getting the item at index 0 in the Rows collection of 'dt', i.e. it is getting the first DataRow in the DataTable. A DataRow itself is a collection of field values for the columns in the table. This:
dt.Rows[0][0]
is getting the first row in the table and then getting the value of the first field in that row. You could expand that into two lines like this:
var row = dt.Rows[0];
var fieldValue = row[0];

In summary, that code testing whether the first field in the first row in the DataTable contains the text "this" and, if it does, writing "this" to the console.
 

Abdoul Rahim

Member
Joined
Oct 16, 2017
Messages
15
Programming Experience
Beginner
Thank you very much for the explanation. I have understood it for ever and ever. I am very sorry for the mistake while copying and pasting.
 
Top Bottom