DataRow confusion!!!

G

Guest

I passed DataRow in the constructor of the form2 as follow

Public Sub New(ByVal row As DataRow)
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

Then on form load event of form2, I set the text property of textbox to
value from datarow as follows

txtCustID.Text = drw("Cust_ID")

When I edited value in the textbox, the changes where written back to the
DataGrid. How it could happen, even though I passed row ByVal and not by
ByRef?


Secondly, I tried binding DataRow to TextBox as follow

txtCustID.DataBindings.Add("Text", drw, "Cust_ID")

But, it resulted in error: "Cannot bind to property or coulmn Cust_ID on
DataSource"

What am I doing wrong?

Thanks
 
J

Joshua Flanagan

When I edited value in the textbox, the changes where written back to the
DataGrid. How it could happen, even though I passed row ByVal and not by
ByRef?

Passing objects ByVal means that the value of the variable (which
contains a memory address) is copied to the new function. This does not
mean the object data itself is copied to the new function. Simply the
memory address, which references the object. If you change the object
that your "row" variable points to within the form - for example, row =
new DataRow() - the external source will not be affected. However, if
you change the data stored within the DataRow object, both references
(the one within the form, and the one outside of the form) will see the
changes, since they point to the same object.

Passing an object ByRef is only used if you want to change which object
a variable references.


Joshua Flanagan
http://flimflan.com/blog
 
G

Guest

But isn’t it true that when a variable is passed ByRef then a pointer to
memory address is passed and when passed ByVal a copy of variable is passed.
So, when we pass variable of type DataRow as ByVal shouldn’t a copy of
variable is passed rather than a pointer to memory address
 
D

Dave

No, the pointer to the object on the heap is copied. The data is unchanged.

Instead, use DataRow.ItemArray or DataTable.Clone to pass in a copy of the data.
 
C

Cor Ligthert

Job,
When I edited value in the textbox, the changes where written back to the
DataGrid. How it could happen, even though I passed row ByVal and not by
ByRef?

Passing by value means for a value that you pass the value and for an object
that you pass the reference (pointer/address) inside the value.
txtCustID.DataBindings.Add("Text", drw, "Cust_ID")
What do you want to achieve with this?
There is no collection of Cust_ID items in a datarow.

I hope this helps,

Cor
 
C

Cor Ligthert

Dave,

I did not seen that this question was already answered.

However than looking more at your problem I assume that you don't have
option strict on in top of your program.

Because this should give an error and there is now a strange effect that I
did not know.
(You set the adres of txtCustID.Text to the same adress as drw("Cust_ID")

However probably did you mean.
txtCustID.Text = drw("Cust_ID").ToString

And than the "value" is placed in this textbox.

Cor
 
J

Joshua Flanagan

Your first sentence is correct, however you conclusion (the 2nd
sentence) is incorrect.

When we pass a VARIABLE, regardless of type, ByVal, a copy of the
VARIABLE is passed. That is true.

In the case of a DataRow, the VARIABLE contains a reference to the
DataRow object on the heap. A copy of that reference (since that is all
the variable contains) is then passed. The reference has been copied.
The object (DataRow) that is referenced has not.

It seems different for value types (like Booleans or Integers), but it
is actually consistent behavior. For value types, the variable contains
the actual value. It does not contain a reference to an object on the
heap. When you pass a value type (or any type) by value, the value of
the variable is copied. So if you change the variable within your
function, it does not affect the variable outside of the function.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top