none
How does this data assignment work? RRS feed

  • Question

  • 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.


    Tuesday, July 9, 2019 1:35 AM

Answers

  • Hi Robert,

    Thank you for posting here.

    Based on your description, you want to know how this data assignment work.

    >>the how come the line var drv = dv.AddNew(); doesn't trigger the AddingNew event, leading to infinite recursion and a quick crash

    Please note that the dv is a type of Dataview, so it will not trigger the AddingNew event. Because it will trigger this event when the type is BindingSource. 

    >>After all, the List that dv refers to is the same list RequestsBindingSource uses.

    They comes from the same list RequestsBindingSource uses, but if the type changes, it could not trigger the event.

    Hope my explanation could be helpful.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Robert in SF Wednesday, July 31, 2019 2:38 AM
    Tuesday, July 9, 2019 2:54 AM
    Moderator

All replies

  • Hello,

    Have you read the remarks in the Microsoft docs?

    https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.bindingsource.addnew?view=netframework-4.8


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, July 9, 2019 1:42 AM
    Moderator
  • Hi Robert,

    Thank you for posting here.

    Based on your description, you want to know how this data assignment work.

    >>the how come the line var drv = dv.AddNew(); doesn't trigger the AddingNew event, leading to infinite recursion and a quick crash

    Please note that the dv is a type of Dataview, so it will not trigger the AddingNew event. Because it will trigger this event when the type is BindingSource. 

    >>After all, the List that dv refers to is the same list RequestsBindingSource uses.

    They comes from the same list RequestsBindingSource uses, but if the type changes, it could not trigger the event.

    Hope my explanation could be helpful.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Robert in SF Wednesday, July 31, 2019 2:38 AM
    Tuesday, July 9, 2019 2:54 AM
    Moderator