I get a runtime error:
An item with the same key has already been added.
private class ProductUpdate
{
public int Index = 0;
public string ProductCode;
}
private void UpdateProduct( List
{
Dictionary
for ( int i = 0; i < products.Count; i++ )
{
productUpdateDictionary.Add( products[i].code, new ProductUpdate { Index = i, ProductCode = products[i].code } );
}
}
I don't know how to solve this issue ,any body has any suggestion
Thanks a Lot!
VulpesPosted Mar 17, 2015, 6:00 PM
You can avoid the error by checking first whether the key already exists, like this:
However, this may not be entirely satisfactory and a different approach would be to modify products[i].code in some way so that it differs from the previous key and can therefore be added to the Dictionary.
krisPosted Mar 18, 2015, 5:20 AM