开发者

Question about compilation error related to a use in the keyword/reserved word "out"

开发者 https://www.devze.com 2023-02-20 01:57 出处:网络
Hello I\'m having an error with this code: \"The out parameter \'o_BlockingSquaresArr\' must be assigned to before control leaves the current method\"

Hello I'm having an error with this code:

"The out parameter 'o_BlockingSquaresArr' must be assigned to before control leaves the current method"

Now this error paints each return statement of each method apart from the last one with red..

I 开发者_如何学Godon't understand what is the problem regarding my specific code

Please help me,

Thanks in Advance

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;
}


As the compiler error says, an out parameter has to be definitely assigned before any non-exceptional return of a method. I can't see any assignment to o_BlockingSquaresArr anywhere. Why are you even declaring it as an out parameter to start with?


An out parameter must be assigned a value before the method returns. In your isLegalMove method, o_BlockingSquaresArr is only assigned in the else block, so the compiler detects there are some cases where it is not initialized. You must make sure that all code paths in the method assign a value to o_BlockingSquaresArr before returning


You need to assign something to the out parameter in every execution path. In your case, you forget that in one case. Simply assign a default value of the beginning of the method so you don't run into it.

I can't tell you where as you didn't include the method name it is happening in.


In the IsLegalMove function, you need to assign a value to the o_BlockingSquaresArr variable


You need to assign something to out parameters in every (normally terminating) codepath. And you don't do that.

For example in some functions you only assign to the parameter inside the for-loop. And if the loop has 0 iterations this will never happen.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号