How does this data assignment work?

  • Thread starter Thread starter Robert in SF
  • Start date Start date
R

Robert in SF

Guest
This is standard code to add default values when adding a new row to a binding source. In the code below, the new row will be created with a default date of now (assume this row has a field called "Date").

private void RequestsBindingSource_AddingNew(object sender, AddingNewEventArgs e)
{
var bs = sender as BindingSource;
var dv = bs.List as DataView;
var drv = dv.AddNew();
drv.Row.SetField<DateTime>("Date", DateTime.Now);
e.NewObject = drv;
}

I understand these assignments are done by reference. That is, the variable bs does not contain a copy of the value in sender, as it would if we were assigning ints. It contains a "reference" (which I assume is the address of but could be more complex) so the variables bs and sender both "point" to the same BindingSource (which in this case is called RequestsBindingSource).

But if my understanding is correct, the how come the line var drv = dv.AddNew(); doesn't trigger the AddingNew event, leading to infinite recursion and a quick crash? After all, the List that dv refers to is the same list RequestsBindingSource uses.

Continue reading...
 
Back
Top