Click here to Skip to main content
15,891,868 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
here is some of my code:
C#
Dictionary<Int32, Dictionary<Int32, List<Int32>>> Staffs = new  Dictionary<int, Dictionary<int, List<int>>>();

and:
C#
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;
              }

...and...:
Staffs[staff][triad].Add(noteloc);
and then:
C#
foreach (int value in Staffs[1][1])
                       {
                           if (value == noteloc)
                           {

                               hi = true;
                               qq = value;

                           }
                       }
                       if (hi)
                           Staffs[1][1].Remove(qq);

//---------------------------------------------------------------
I added 3 Items in the list inner the dictionary
when I remove one of them :count of the list=2
but when I add 4 Items ,after remove Item I have :count=4
what can I do?

What I have tried:

I know the problem is for "return" in:
if (Staffs[staff][triad].Count >= 4)
{
if(myVar.sitnote)
MessageBox.Show("Maximum notes reached");

return;
But I don't know what can I do
Posted
Updated 20-Jul-16 23:54pm

1 solution

The problem is that you are using variables in one part of the code, and "magic numbers" in the other.
Variables:
C#
Staffs[staff].Add(triad, new List<int>());

"Magic numbers":
C#
Staffs[1][1].Remove(qq);
Unless both staff and triad are always 1 you won't see any change in the count.
Solution: never use "magic numbers"!
 
Share this answer
 
Comments
hojatIzadi 21-Jul-16 6:14am    
Hi OriginalGriff
you said true but I have this problem just when I have 4 items in the list .
result for 3 item (count=3) after remove is count=2.
result for 2 item (count=2) after remove is count=1.
result for 1 item (count=1) after remove is count=0.
OriginalGriff 21-Jul-16 6:37am    
Use the debugger and look at exactly what you have in the various dictionaries. If you aren't adding them all to the right list(s) then you will get "funny" results. And I can't check the code you use to add and remove them!
hojatIzadi 21-Jul-16 8:52am    
Griff please look at some more codes:
Int32 staff = 0;
Int32 triad = 0;


int h = (e.Y / 5) * 5;


if (e.Y >= 100 && e.Y < 300)
{
staff = 1;
noteloc = (h / 5) - 12;


}
else if (e.Y >= 340 && e.Y < 540)
{
staff = 2;
noteloc = (h / 5) - 60;
}
else if (e.Y >= 580 && e.Y < 780)
{
staff = 3;
noteloc = (h / 5) - 108;
}

if (e.X >= 100 && e.X < 300)
{
triad = 1;
}
else if (e.X >= 300 && e.X < 500)
{
triad = 2;
}
else if (e.X >= 500 && e.X < 700)
{
triad = 3;
}
else if (e.X >= 700 && e.X < 900)
{
triad = 4;
}

if (staff == 0 || triad == 0)
{
// MessageBox.Show("Out of range");
return;
}

if (staff > 4 || triad > 4)
{
MessageBox.Show("Out of range");
return;
}

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;
}
OriginalGriff 21-Jul-16 8:59am    
Use the debugger: follow the code through and see exactly what happens. I can't tell without running it because most of that is dependant on what looks to be a mouse location in an event handler! And that will depend on what data you are using - which I don't have. :laugh:
So use the debugger - it's not difficult at all!
hojatIzadi 21-Jul-16 9:04am    
Ok .thank you

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