Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have a list in my code and I want to decrease count of the list when I remove an Item from it

What I have tried:

I test mylist.count--; and I used a variable for my work but it is not sutable
Posted
Updated 20-Jul-16 21:56pm
v2
Comments
Anisuzzaman Sumon 21-Jul-16 3:30am    
show up your code.Your question is not so clear!
Ralf Meier 21-Jul-16 3:40am    
If you remove an Item from your List then List.Count ist automaticly decreased.
How do you remove an Item from your List ? Show the relevant code-part ...
hojatIzadi 21-Jul-16 3:56am    
Dictionary<int32, dictionary<int32,="" list<int32="">>> Staffs = new Dictionary<int, dictionary<int,="" list<int="">>>();
//---------------
if (!Staffs.Keys.Contains(staff))
{
Staffs.Add(staff, new Dictionary<int, list<int="">>());
}

if (!Staffs[staff].Keys.Contains(triad))
{
Staffs[staff].Add(triad, new List<int>());
}

if (Staffs[staff][triad].Count >= 4)
{
if(myVar.sitnote)
MessageBox.Show("Maximum notes reached");

return;
}
//--------------------
if (staff == 1 && triad == 1)
{
triad1 = Staffs[1][1].ToArray(); Array.Sort(triad1);
mypoint.Add(newpoint);

}
//--------------------------
foreach (int value in Staffs[1][1])
{
if (value == noteloc)
{

a = true;
qq = value;

}
}
if (a)
Staffs[1][1].Remove(qq);
triad1 = Staffs[1][1].ToArray();

1 solution

You don't need to. If you are using a standard List<T> or (Gawd forbid) even an ArrayList the act of adding or removing an item alters the count automatically.
There is no good case where anyone should have to call a method to remove an item and also remember to update the items count, even if you "brew your own" collection - the remove method should always maintain the count correctly.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900