dwwilson66
dwwilson66

Reputation: 7074

How do I set a constructor's parameters to an array of variable size?

HOMEWORK: We're working on a hangman program. I have the following method:

    public char[] selectWord()
    {
        String[] hangmanWords = new String[50] { "time", "person", "year", "way", "day", "thing", "man", "world", "life", "hand", "part", "child", "eye", "woman", "place", "work", "week", "case", "point", "government", "company", "number", "group", "problem", "fact", "good", "new", "first", "last", "long", "great", "little", "own", "other", "old", "right", "big", "high", "different", "small", "large", "next", "early", "young", "important", "few", "public", "bad", "same", "able" };
        Random wordIndex = new Random();
        int randomIndex = wordIndex.Next(0, 49); 
        wordToGuess = hangmanWords[randomIndex].ToUpper();
        char[] wordToGuessArray = wordToGuess.ToCharArray();                                         // convert word to array of letters
        return wordToGuessArray;  
    }

I would like to define a constructor for wordToGuessArray. Ideally I want the selectWord() method to set the wordToGuessArray parameter. However, I won't know the variable array length until after I run this method and I can't create a constructor without explicitly defining char[x]. This question has the array length set in main. Mine cannot due to requirements restrictions in the assignment. So where would this selectWord() method need to live in order to properly set my variable?

I'm having a hard time picturing a workable program flow for that.

Upvotes: 0

Views: 4562

Answers (2)

evanmcdonnal
evanmcdonnal

Reputation: 48154

I think you're somewhat confused. char[] wordToGuessArray = wordToGuess.ToCharArray(); is not only allocating the correct size array for you, it's also filling it with the contents of wordToGuess I have two suggested changes to make your code better.

public char[] selectWord()
{
    // get rid of size in this, when you do static init you don't need it
    String[] hangmanWords = new String[] { "time", "person", "year", "way", "day", "thing", "man", "world", "life", "hand", "part", "child", "eye", "woman", "place", "work", "week", "case", "point", "government", "company", "number", "group", "problem", "fact", "good", "new", "first", "last", "long", "great", "little", "own", "other", "old", "right", "big", "high", "different", "small", "large", "next", "early", "young", "important", "few", "public", "bad", "same", "able" };
    // combinme those bottom 5 or so lines into 1.
    return hangmanWords[new Random.Next(0, 49)].ToUpper().ToCharArray();
}

This method will simply return a character array which has the characters of a randomly selected word from the hangmanWords array. I believe that's what you want, right?

If you want the character array wordToGuessArray to be a property of your class then just declare it as public char[] wordsToGuessArray { get; set; } and make the method void, replacing the return with assingment to wordsToGuessArray.

Sample constructor;

 // field declared in my class, call it game
 public char[] wordsToGuessArray { get; set; }

 public Game(char[] word)
 {
       wordToGuessArray = word;
 }
 // call it like this
 Game game = new Game(selectWord());

If you want to set wordToGuessArray in the constructor you can make selectWord() a public static method in the same class in which case you would call it like Game.selectWord()

Upvotes: 1

Venson
Venson

Reputation: 1870

you may looking for the param keyword. It allows you to pass an undefind numbers of parameters to a Constructor like this example shows:

public static void UseParams2(params object[] list) 
{
   for ( int i = 0 ; i < list.Length ; i++ )
      Console.WriteLine(list[i]);
   Console.WriteLine();
}

Relegated to your post, your code could look like this:

public char[] selectWord(params string[] hangmanWords)
{
    Random wordIndex = new Random();
    int randomIndex = wordIndex.Next(0, hangmanWords.Count()); 
    wordToGuess = hangmanWords[randomIndex].ToUpper();
    char[] wordToGuessArray = wordToGuess.ToCharArray();                                         // convert word to array of letters
    return wordToGuessArray;  
}

Upvotes: 1

Related Questions