
Untitled
By: a guest on
Jul 20th, 2012 | syntax:
None | size: 0.83 KB | hits: 11 | expires: Never
Why Removing item from list in this way is impossible?
public void RemoveFrmList(int ProdId)
{
int _index;
foreach (Products item in BoughtItems)
{
if (item.ProductID == ProdId)
{
_index = BoughtItems.IndexOf(item);
}
}
BoughtItems.RemoveAt(_index);
}
int _index;
foreach (Products item in BoughtItems)
{
//Code here is not executed during runtime for reasons stated above.
}
BoughtItems.RemoveAt(_index); //error here because _index was not assigned!
int _index = -1;
foreach (...)
{
//...
}
if (_index != -1){
BoughtItems.RemoveAt(_index);
}
else
{
//handle case if needed
}
public void RemoveFrmList(int ProdId)
{
BoughtItems.RemoveAll( item => item.ProductID == ProdId );
}