关于在关键字/保留字“out”中使用的编译错误的问题

时间:2011-03-25 22:50:55

标签: c# visual-studio-2010 compiler-errors keyword

您好 我在使用此代码时遇到错误:
必须在控制离开当前方法之前指定out参数'o_BlockingSquaresArr'
现在这个错误描绘了每个方法的每个返回语句,而不是最后一个方法的红色。
我不明白我的具体代码有什么问题 请帮帮我,
在此先感谢

internal bool isLegalMove(Square i_Move, out List<Square> o_BlockingSquaresArr) 
{
     bool result;

     if (m_GameBoard[i_Move.RowIndex, (int)i_Move.ColIndex].Coin != null)   
     {
          result = false;
          m_MessageBuffer = "You have enterd a square which is already
                             occupied, please try again...";       

          m_ErrorFlag=true;                                                                               
     }
     else
     {
          result = checkIfThereIsAtLeastOneSeqInOneDirection(i_Move,out o_BlockingSquaresArr);       
     }
     return result;

}

internal bool checkIfThereIsAtLeastOneSeqInOneDirection(Square i_Move, out List<Square> o_BlockingSquaresArr)
{
    const int k_EightDirections = 8;
    bool isSequenceFound, finalRes = false;

    for (int i = 1; i <= k_EightDirections; i++)    
    {
        isSequenceFound = checkOpponentSequenceInDirection(i_Move, (eDirections)i, out o_BlockingSquaresArr);     
        if (isSequenceFound)
        {
            finalRes = true;                                                                                      
        }                                                                                                         

    }
    return finalRes;
}

internal bool checkOpponentSequenceInDirection(Square i_Move, eDirections i_Direction, out List<Square> o_BlockingSquaresArr) 
{
     //I've shortened this code only relevant things  
     Square o_AdjacentSquare = new Square();
     adjacentCoin = doSwitchAndRetrieveAdjacentCoin(i_Move, i_Direction, out o_AdjacentSquare);

     // ...

     if (isThereAnOpponentSequence)
     {
          o_BlockingSquaresArr.Add(o_AdjacentSquare); 
     }
     return isThereAnOpponentSequence;
}

5 个答案:

答案 0 :(得分:3)

正如编译器错误所述,必须在方法的任何非异常返回之前明确分配out参数。我无法在任何地方看到任何分配给o_BlockingSquaresArr。您为什么甚至将其声明为out参数?

答案 1 :(得分:2)

必须在方法返回之前为out参数指定值。在isLegalMove方法中,o_BlockingSquaresArr仅在else块中分配,因此编译器会检测到某些情况下未初始化o_BlockingSquaresArr。您必须确保方法中的所有代码路径在返回之前将值分配给{{1}}

答案 2 :(得分:1)

您需要在每个执行路径中为out参数指定一些内容。在你的情况下,你忘了在一个案例中。只需指定方法开头的默认值,这样就不会遇到它。

我不知道你在哪里没有包含它正在发生的方法名称。

答案 3 :(得分:0)

在IsLegalMove函数中,您需要为o_BlockingSquaresArr变量赋值

答案 4 :(得分:0)

您需要在每个(通常是终止的)代码路径中为out参数指定一些内容。你不这样做。

例如,在某些函数中,您只能分配给for循环中的参数。如果循环有0次迭代,这将永远不会发生。