Search code examples
c#arraysreturn

Trying to return a 2d array from a method


Making a Hangman game. I want in this function to take the 'Answer' which has been validated elsewhere in my code and split it into its individual letters and then Hangman equivalent representative.
e.g.:

abc == ___   
hi world == __/_____ and so on..

I am using a 2d array 'UncoveredArray' to hold this data. I want to now return it to my main so that it can be used for the next step of the game in another method.

static void Main(string[] args)
{            
    NewGame Hangman = new NewGame();
    string Answer = Hangman.GetWord();
    
    var UncoveredArray = new char[Answer.Length, 2];       

    UncoveredArray = Hangman.ProcessWord(Answer, out UncoveredArray);
public char[] ProcessWord(string Answer, out char UncoveredArray)
{
    char[] chars = Answer.ToCharArray();
    var UncoveredArray = new char[chars.Length, 2];

    for (int i = 0; i < Answer.Length; i++)
    {
        if (chars[i] == ' ')
        {
            Console.Write("/");
            UncoveredArray[i, 0] = chars[i];
            UncoveredArray[i, 1] = '/';
        }
        else if (char.IsPunctuation(chars[i]))
        {
            Console.Write(chars[i]);
            UncoveredArray[i, 0] = chars[i];
            UncoveredArray[i, 1] = chars[i];
        }
        else
        {
            Console.Write("_");

            UncoveredArray[i, 0] = chars[i];
            UncoveredArray[i, 1] = '_';
        }
    }
    return UncoveredArray;
    //TODO: RETURN ARRAY 
}

Solution

  • The confusion arises from unnecessary use of an out parameter. Out params allow for args to be passed in by reference, while also allowing them to be initialized inline. This is different from a ref param which strictly is a pass by reference, that argument must have been initialized previously. One key usage of out parameters is with the .TryParse() methods, which return a boolean flag to show if the parse was successful, and if it was, you get the parsed variable "out" of it.

    Simplify things: just return a new instance

    public char[,] ProcessWord(string answer) {} //logic remains more or less the same
    
    //in Main() use it like
    char[,] uncoveredArray = hangman.ProcessWord(answer);
    

    Notice in your original method, you pass in the char[,] UncoveredArray as an output parameter and then immediately cobbler it with var UncoveredArray = new char[chars.Length, 2];. This won't compile, and if it did, it would make the the out param pointless in the first place. Be careful, you never want to attempt to initialize a new variable with the same name, especially when its taking on the name of a parameter. As far as I know C# doesn't support something like Rust/F# etc.'s "shadowing" which allows such a phenomenon

    Also the return type must be char[,] not char[]. The 1st is an actual 2D array, while the 2nd is 1D

    Use of explicit typing, rather than var helps out a lot in cases like this, it would've help you catch this error a bit easier. Please be sure to follow proper naming conventions for variables, classes, and methods as well