GhostBehr
GhostBehr

Reputation: 23

Constructor pointing to a property not accesing set keyword

I'm just learning C# and trying to get some practice working with classes as opposed to writing everything in Main, so I'm trying to write a console game of hangman.

I'm initializing the guessed letter with this line:

Guess guess = new Guess(Console.ReadLine());

Which points to this constructor in the Guess.cs file:

public Guess(string guessString)
    {
        CurrentGuess = Convert.ToChar(guessString);
    }

CurrentGuess referring to this property:

private char currentGuess

public char CurrentGuess
    {
        get
        {
            return currentGuess;
        }

        set
        {
            foreach (char letter in guessList)
            {
                if (letter == value)
                {
                    GuessedAlready = true;
                    break;
                }
            }

            if (GuessedAlready == false)
            {
                value = currentGuess;
                guessesMade++;
                guessList[guessesMade-1] = value;

            }
        }
    }

When I run the program and input a char value, it isn't getting set to currentGuess. Also Visual Studio is telling me that currentGuess is never getting assigned to, and is keeping it's current value (which is blank).

Why isn't the set keyword working? I've followed the path to currentGuess over and over and I can't see it, so I'm assuming it's a reason I don't know yet.

Any help would be appreciated!

Upvotes: 2

Views: 77

Answers (1)

rene
rene

Reputation: 42414

You have to swap this assingment in your setter

value = currentGuess;

into

currentGuess = value;

In the set method the value is the incoming parameter. In your case you want to store that value in your local class variable curtentGuess;

Upvotes: 2

Related Questions