locked
NewItems initialized but values not set RRS feed

  • Question

  • I am using the ManyToMany Control and when unchecking an item the OldItems object is set as expected to the unchecked item but when checking an item the NewItems object is only initialized and its values are not set to the checked item, the property ID is 0...

    Here is the code I added for testing:

    Private Sub Treatment_Method_Changed(e As System.Collections.Specialized.NotifyCollectionChangedEventArgs)
        Select Case e.Action
            Case NotifyCollectionChangedAction.Add
                Debug.WriteLine("NewItems.Count: " & e.NewItems.Count)
                Debug.WriteLine("NewStartingIndex: " & e.NewStartingIndex)
                Debug.WriteLine(e.NewItems(0).Method.ID & " : " & e.NewItems(0).Method.Name)
            Case NotifyCollectionChangedAction.Remove
                Debug.WriteLine("OldItems.Count: " & e.OldItems.Count)
                Debug.WriteLine("OldStartingIndex: " & e.OldStartingIndex)
                Debug.WriteLine(e.OldItems(0).Method.ID & " : " & e.OldItems(0).Method.Name)
        End Select
    End Sub

    When checking an item I get a NullReferenceException of course since NewItems is not set but I can see it got initialized when I print it in the Immediate Window:

    When checking an item I get a NullReferenceException of course since NewItems is not set but I can see it got initialized when I print it in the Immediate Window: ? e.NewItems(0) {LightSwitchApplication.Treatment_Method} LightSwitchApplication.Treatment_Method: {LightSwitchApplication.Treatment_Method}

    Thanks!

    Saturday, June 8, 2013 5:46 PM

All replies

  • It's not that e.NewItems or e.NewItems(0) is null, it's more likely that e.NewItems(0).Method is null. When adding new entities, it's properties are only initialized after the creation of the entity (unless you wrote code in the <EntityName>_Created method). All of the visual collections over that entity type will be notified that the entity was added to the collection before you have a chance to set properties external to the creation of the entity, hence why the Method property on the new entity is null inside this event handler.

    Justin Anderson, LightSwitch Development Team

    Saturday, June 8, 2013 8:56 PM
    Moderator
  • e.NewItems(0).ID is 0 so that's why I say it is null... and when I uncheck and item e.OldItems(0).ID is set to the ID of the unchecked item as expected so why e.NewItems(0).ID is not set to the ID of the checked item? and also e.OldItems(0).Method is not null when unchecking an item so I would expect e.NewItems(0).Method not been null when checking an item...
    Saturday, June 8, 2013 9:06 PM
  • Then don't call it null because that has a completely different meaning.

    The Id of newly created entities is always 0. The Id of newly created entities is not resolved until it is actually saved at the data source, when it actually then exists, and then the Id is updated with the actual value. Existing entities will have a value for Id that is non-zero.

    The reason why e.NewItems(0).Method is null is the same explanation that I gave above. Code that sets properties on an entity is run after the creation of the entity and this event is raised on the creation of the entity, so the property values are not set yet. On deletion of an entity or disassociation of the entity from the navigation property, both which will raise this event on collections, the properties on the entity are set to whatever values the entity had when loaded from the data source and merged with any changes you made to those properties, which is why they have the values you see when you inspect e.OldItem(0).

    Take a look at the following picture while I was debugging:

    The yellow highlight is the currently executing line. The green highlight is the currently called line in the stack frame that I am currently looking at. As you can see, I'm calling this.Table1Items.AddNew() and the line to set Property1 has not been called yet. But, you see that the Table1Items_Changed event is called as a response to creating the new entity instance. Looking at the Output tool window, it's Id property is 0 (because it hasn't been saved yet) and it's Property1 property is null (because it hasn't been set yet).


    Justin Anderson, LightSwitch Development Team

    Saturday, June 8, 2013 9:33 PM
    Moderator
  • I understand that e.NewItems(0).ID is 0 since it is not saved yet but the related child entity already exist and I wanted to check it's value. What I am trying to achieve with the Many-To-Many Control is to automatically check all the nodes under a root node when that root node is checked. What other event you would suggest me to look at to do that?

    Saturday, June 8, 2013 10:29 PM