Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for updates to a single datatable row

Tags:

vb.net

ado.net

Why does the first method below work? I would have thought that the variable 'row' is completely separate from anything else, such as a row in DataTable1. Also, is there a drawback to using the first method? For changes to lots of columns in a single row, I'd rather do it that way.

dim row = Me.DataSet1.DataTable1(0)
row.Item1=123
DataTable1TableAdapter.Update(Me.DataSet1.DataTable1)

OR

DataSet1.DataTable1(0).Item1=123
DataTable1TableAdapter.Update(Me.DataSet1.DataTable1)
like image 370
John Avatar asked Jan 23 '26 13:01

John


1 Answers

I believe setting row the way you are it is using vb .net type inference. when you set it equal to Me.DataSet1.DataTable1(0) any changes made to row will act as reference to Me.DataSet1.DataTable1(0). @Bala you very right they are equivalent.

If i can interject personal preference i would definitely go with option two, no need to use a needless variable, and it's obviously cleaner and more straight forward to read. AS far as a best practice, if you don't need the variable don't initialize it. especially one that directly references a value in your Table. Hope this helps.

like image 72
Mabdullah Avatar answered Jan 26 '26 23:01

Mabdullah