PDA

View Full Version : [OPEN] [#330] A suggestion to eradicate "Token is not unique" errors



michaeld
Aug 03, 2013, 10:50 AM
Token is not unique
-------------------
ID = ctl00_cp_TaskUC0_TDatesW_ClientInit
TagName = anchor
Match = <#:anchor id="ctl00_cp_TaskUC0_TDatesW_ClientInit" />


I don't know if this would be considered a bug, but I was tracking the occurrence of this issue within my code. It took a good number of hours but I finally realized it was because I was adding the same user control twice. Effectively to reproduce, all you need to do is the following:

Panel1.Items.Add( ctl ); Panel1.Items.Add( ctl );
Panel1.Bin.Add( ctl ); Panel1.Bin.Add( ctl );

Here's my reason for posting this. It seems to me that I had to work all the way backwards from Transformer which is very late way to discover the issue. It's not excepting out where the issue actually happens, and its easy to catch.

I looked at ItemsCollection<T> which is derived from List<T> and though I thought HashSet might be better, I tried it but there's a lot of dependencies on functions of IList and performance was unnecessary to affect Release versions, so instead I thought it would be easy to add the following so I tried:



new public void Add(T item)
{
#if DEBUG
if(base.Contains(item))
throw new Exception("Duplicate control");
#endif


this.CheckItem(item);


if (this.BeforeItemAdd != null)
{
this.BeforeItemAdd(item);
}

base.Add(item);


if (this.AfterItemAdd != null)
{
this.AfterItemAdd(item);
}
}


It worked. Now you just need to apply the same tests for AddRange and Insert and this problem saves another programmer some grief.

michaeld
Aug 03, 2013, 11:00 AM
I'm realizing it couldn't hurt to call out the name of the ClientID in the exception as well.

Vladimir
Aug 03, 2013, 6:19 PM
Well, your override doesn't cover all cases because you can add Ext.Net controls to ASP.NET native controls (Controls collection)
Or you can add one instance to different containers

In this case, you will have the same error. So, I am doubt that we can track it.
I guess that we should to change text of the error, for example 'Token is not unique, possible control duplicating'

michaeld
Aug 03, 2013, 11:42 PM
Well, your override doesn't cover all cases because you can add Ext.Net controls to ASP.NET native controls (Controls collection)

Yeah, this case is tricky.


Or you can add one instance to different containers In this case, you will have the same error. So, I am doubt that we can track it.

This one could be handled. Instead of the code I suggested above, you could have a shared HashTable resource you add all controls to in addition to the code that's supposed to be there. Debug mode only.



I guess that we should to change text of the error, for example 'Token is not unique, possible control duplicating'

At a minimum, communicative errors are helpful.

Daniil
Aug 28, 2013, 7:01 AM
At a minimum, communicative errors are helpful.

Created an Issue.
https://github.com/extnet/Ext.NET/issues/330