Skip to main content

Microsoft Silverlight

Incorrect message in exception - NotifyCollectionChangedArgs ctorRSS Feed

(0)

shacktoms
shacktoms

Member

Member

185 points

149 Posts

Incorrect message in exception - NotifyCollectionChangedArgs ctor

I called  

e = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, null, this[0], 0)
and got the exception text... 

"Constructor supports only the 'Remove' action."

I think it should be

"Constructor supports only the 'Replace' action."

 

Jonathan Shen – MSFT
Jonathan...

All-Star

All-Star

24626 points

2,418 Posts

Microsoft

Re: Incorrect message in exception - NotifyCollectionChangedArgs ctor

Hi Shacktoms,

To make our investigation easier to perform, would you please provide a tiny repro here?  Thanks for your understanding.

Best regards,

Jonathan

Jonathan Shen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

shacktoms
shacktoms

Member

Member

185 points

149 Posts

Re: Incorrect message in exception - NotifyCollectionChangedArgs ctor

I already provided code that would reproduce the bad error message, clearly that code assumes you are implementing a collection object and are removing the first element from that collection.  But if you want something smaller, the bad error message can be reproduced very easiily, even without going to the trouble of implementing INotifyCollectionChanged in a class.

How about this?   New Silverlight project is named TestNotifyCollection.   Page.xaml looks like this...

<UserControl x:Class="TestNotifyCollection.Page"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             xmlns:local="clr-namespace:TestNotifyCollection"
             Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <local:Test/>
    </Grid>
</UserControl>

Page.xaml.cs looks like this

using System;
using System.Collections.Specialized;
using System.Windows.Controls;

namespace TestNotifyCollection {
    public class Test : ContentControl {
        public Test() {
            try {
                var e = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, null, this, 0);
                }
            catch (Exception e) {
                Content = e.Message;
                }
            }
        }

    public partial class Page : UserControl {
        public Page() {
            InitializeComponent();
            }
        }
    }

Note that in Blend, the message is different, I guess that can be taken as a failure of Blend to simulate the actual application. But if you actually build and run the application, even from Blend, or view it in Visual Studio 2008, you will see the incorrect error text in Siliverlight.

Obviously this is a really minor bug.  My code was broken, but I had noticed that the error message was incorrect in its descriptive text.

 

 

Jonathan Shen – MSFT
Jonathan...

All-Star

All-Star

24626 points

2,418 Posts

Microsoft

Re: Incorrect message in exception - NotifyCollectionChangedArgs ctor

Hi Shacktoms,

Sorry that I missed your reply.  If your problem persists there, please post another thread and let me know.  Thanks.

 

Best regards,

Jonathan

Jonathan Shen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

shacktoms
shacktoms

Member

Member

185 points

149 Posts

Re: Incorrect message in exception - NotifyCollectionChangedArgs ctor

I rebuilt and reran the test application and the bug is still there, the error message still says the constructor only supports the "Remove" operation, although it actually only supports the "Replace" operation.  Looks like a cut and paste error in the Silverlight code.

Which reply did you miss? The reply to your private message?  That looked like this...

I have looked at the reflection code with the Red Gate reflector and I think the bug is clearly there.  What I see in the code for the constructor is this... 

1    public NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction action, object newItem, object oldItem, int index)
2    {
3        this._newStartingIndex = -1;
4        this._oldStartingIndex = -1;
5        if (action != NotifyCollectionChangedAction.Replace)
6        {
7            StringBuilder builder = new StringBuilder();
8            builder.AppendFormat(Resx.GetString("NotifyCollectionChangedEventArgs_UnSupportedConstructorAction"), new object[] { NotifyCollectionChangedAction.Remove });
9            throw new NotSupportedException(builder.ToString());
10       }
11       this._action = action;
12       this._newItems = new object[] { newItem };
13       this._oldItems = new object[] { oldItem };
14       this._newStartingIndex = index;
15   }
16   
The decision that the call is in error is made (correctly) at line 5, and then the StringBuilder is created to build the error text.  But the action that is passed in to the error text (at line 8) is NotifyCollectionChangedAction.Remove, when it should be NotifyCollectionChangedAction.Replace.  So the text says it only supports "Remove" when it should say it supports only "Replace".

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities