为什么我的清单计数没有减少 [英] Why the count of my list isn't decreased

查看:68
本文介绍了为什么我的清单计数没有减少的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的一些代码:

 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))
{
员工[staff] .Add(triad, new List< int>());
}

if (员工[员工] [triad] .Count > = 4
{
if (myVar.sitnote)
MessageBox.Show( 达到最大音符数);

return ;
}



......和......:

员工[员工] [triad]。添加(noteloc);

然后:

  foreach  int   value   员工[ 1 ] [ 1 ])
{
if value == noteloc)
{

hi = true ;
qq = value ;

}
}
如果(hi)
员工[ 1 ] [ 1 ]。删除(qq);



// - -------------------------------------------------- -----------

我在字典内的列表中添加了3个项目

当我删除其中一个时:list of count = 2

但是当我添加4项时,删除项目后我有:count = 4

我该怎么办?



我的尝试:



我知道问题是返回:

if(Staffs [staff] [triad] .Count> = 4)

{

if(myVar.sitnote)

MessageBox.Show (达到最大音符数);



返回;

但我不知道我能做什么

解决方案

问题是你在代码的一部分使用变量,在另一部分使用魔术数字。

变量:

员工[员工]。添加(三合一,列表< int>()); 



幻数:

员工[ 1 ] [ 1 ]。删除(qq); 

除非员工三元组都是总是1你不会看到计数的任何变化。

解决方案:永远不要使用魔术数字!


here is some of my code:

Dictionary<Int32, Dictionary<Int32, List<Int32>>> Staffs = new  Dictionary<int, Dictionary<int, List<int>>>();


and:

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:

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

解决方案

The problem is that you are using variables in one part of the code, and "magic numbers" in the other.
Variables:

Staffs[staff].Add(triad, new List<int>());


"Magic numbers":

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"!


这篇关于为什么我的清单计数没有减少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆