C# 关于与关键字/保留字“中的使用相关的编译错误的问题;“出去”;

C# 关于与关键字/保留字“中的使用相关的编译错误的问题;“出去”;,c#,visual-studio-2010,compiler-errors,keyword,C#,Visual Studio 2010,Compiler Errors,Keyword,你好 我的代码有错误: “在控件离开当前方法之前,必须将out参数'o_blockingsquaresar'分配给它” 现在,此错误将每个方法的每个返回语句与最后一个返回语句分开绘制为红色。 我不明白我的特定代码有什么问题 请帮帮我, 提前谢谢 internal bool isLegalMove(Square i_Move, out List<Square> o_BlockingSquaresArr) { bool result; if (m_GameBoar

你好 我的代码有错误:
在控件离开当前方法之前,必须将out参数'o_blockingsquaresar'分配给它”
现在,此错误将每个方法的每个返回语句与最后一个返回语句分开绘制为红色。
我不明白我的特定代码有什么问题
请帮帮我,
提前谢谢

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;
}
internal bool isLegalMove(方形i_Move,out List o_BlockingSquaresArr)
{
布尔结果;
if(m_GameBoard[i_Move.RowIndex,(int)i_Move.ColIndex].Coin!=null)
{
结果=假;
m_MessageBuffer=“您输入了一个已被删除的方块
已占用,请再试一次……”;
m_ErrorFlag=真;
}
其他的
{
结果=检查是否在一个方向(i_移动,o_阻挡方形)上移动;
}
返回结果;
}
内部bool检查是否为单向(方形i_移动,移出列表o_阻塞方形)
{
常数int k_八个方向=8;
bool是sequencefund,finalRes=false;

对于(int i=1;i您需要为每个执行路径中的
out
参数分配一些内容。在您的情况下,有一种情况下您忘记了这一点。只需为方法的开头分配一个默认值,这样您就不会遇到它


我不能告诉你在哪里,因为你没有包括它发生在哪里的方法名。

在方法返回之前,必须为
out
参数赋值。在你的
isLegalMove
方法中,
o_blockingsquaresar
只在
else
块中赋值,因此编译器检测到它在某些情况下会被赋值未初始化。您必须确保该方法中的所有代码路径都为
o_BlockingSquaresArr
赋值,然后在IsLegalMove函数中返回,您需要为o_BlockingSquaresArr变量赋值您需要为每个中的out参数赋值(通常终止)代码路径,但你不能这么做


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

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

有一个指定位置:它作为out参数传递给checkIfThereIsAtLeastOneSeqInOneDirection@Thomas:是的,但如果您一直将其作为参数传递给另一个方法,最终必须有一个显式赋值。(当然,除非它永远重复出现:)它以内部bool checkoponentsequencendirection结束。请参阅bottom@Mulder:在
checkopponentsequencendirection
中没有赋值给
o_BlockingSquaresArr
,是吗?你认为变量实际上是在哪里赋值的?穆德:那不是赋值。那是试图对未明确赋值的变量调用方法。赋值如下:
o_BlockingSquaresArr=new List()
。但这不是你所拥有的。即使你做到了,那也只是有条件的——赋值必须在你返回的时候发生,只要它不以异常结束。我不想这样做,因为赋值会在内部bool中发生几级checkOpponentSequenceInDirection@Mulder:除了作业没有发生,是吗?@Jon Skeet:是的,因为我认为从逻辑上讲,如果我在其他地方做作业,它会毁掉一切(从逻辑上讲)@Mulder:但你哪里都没有作业。你认为你在哪里有作业?