从字典中获取值

时间:2014-06-04 19:03:00

标签: c#

我要去Round-Robin算法。我所做的一切都必须动态地随机创建。当我想第二次检查我是否有并且在检查点中它的值大于0时,它表示"给定的密钥没有出现在字典中#34;。我该如何解决这个问题。

private int GetNextNodeToProcesssRandom()
{
    int NextNodeIndex = -1;

    if (NextNodeToProcess >= this.waytosave.Count)
    {
        NextNodeToProcess = 0;
    }

    for (int i = NextNodeToProcess; i < this.waytosave.Count; i++)
    {
        if (this.waytosave[i] > 0)//the problem appears here when the cod goes for the second time.
        {
            NextNodeIndex = i;
            break;
        }
    }

    NextNodeToProcess++;

    return NextNodeIndex;
} 

1 个答案:

答案 0 :(得分:0)

目前还不清楚你的具体目标是什么。但是,如果您想循环使用Dict,可以使用:

foreach(KeyValuePair<TKey, TValue> entry in MyDict)
{
    // do something with entry.Value or entry.Key
}

现在,如果您想在程序中添加支票,可以尝试:

    if (MyDict.ContainsKey(this.waytosave[i]))
///continue execution...

外观here可能对您有所帮助