Grochinator
Grochinator

Reputation: 1

C# Cannot convert randomized string into the Textbox

    private void Start_Click(object sender, EventArgs e)
    {

    string[] words = new string[10];
        words[0] = "Starks";
        words[1] = "Lannisters";
        words[2] = "Tullys";
        words[3] = "Greyjoys";
        words[4] = "Arryns";
        words[5] = "Baratheons";
        words[6] = "Tyrells";
        words[7] = "Martells";
        words[8] = "Targaryans";
        words[9] = "Braavosi";

        Random rword = new Random();


        int randomNumber = rword.Next(10);

        string asterickWordIntial = words[randomNumber].ToUpper();
        string nonAsterickWord = words[randomNumber].ToUpper();

        string asterickWordFinal = asterickWordIntial.Replace("A", "*");
        asterickWordFinal = asterickWordIntial.Replace("B", "*");
        asterickWordFinal = asterickWordIntial.Replace("C", "*");
        asterickWordFinal = asterickWordIntial.Replace("D", "*");
        asterickWordFinal = asterickWordIntial.Replace("E", "*");
        asterickWordFinal = asterickWordIntial.Replace("F", "*");
        asterickWordFinal = asterickWordIntial.Replace("G", "*");
        asterickWordFinal = asterickWordIntial.Replace("H", "*");
        asterickWordFinal = asterickWordIntial.Replace("I", "*");
        asterickWordFinal = asterickWordIntial.Replace("J", "*");
        asterickWordFinal = asterickWordIntial.Replace("K", "*");
        asterickWordFinal = asterickWordIntial.Replace("L", "*");
        asterickWordFinal = asterickWordIntial.Replace("M", "*");
        asterickWordFinal = asterickWordIntial.Replace("N", "*");
        asterickWordFinal = asterickWordIntial.Replace("O", "*");
        asterickWordFinal = asterickWordIntial.Replace("P", "*");
        asterickWordFinal = asterickWordIntial.Replace("Q", "*");
        asterickWordFinal = asterickWordIntial.Replace("R", "*");
        asterickWordFinal = asterickWordIntial.Replace("S", "*");
        asterickWordFinal = asterickWordIntial.Replace("T", "*");
        asterickWordFinal = asterickWordIntial.Replace("U", "*");
        asterickWordFinal = asterickWordIntial.Replace("V", "*");
        asterickWordFinal = asterickWordIntial.Replace("W", "*");
        asterickWordFinal = asterickWordIntial.Replace("X", "*");
        asterickWordFinal = asterickWordIntial.Replace("Y", "*");
        asterickWordFinal = asterickWordIntial.Replace("Z", "*");

        asterickWordFinal = HangTxtBox;
    }

When I set asterickWordFinal to HangTxtBox I'm getting a the following message

"Cannot implicitly convert type System.Window.Forms.TextBox to string"

But I heard if you set asterickWordFinal.Text = HangTxtBox would get to it right, however I'm getting this message.

"string does not contain a definition for Text accepting a first argument for of type string could be found"

So I'm I'm wondering if I'm missing something in the system, the namespace or do I have to try to override? Thank you for the help!

Upvotes: 0

Views: 72

Answers (3)

test
test

Reputation: 2639

This is a side-answer, your asterisk code can be simplified quite a bit. All you are really doing is creating a string that has as many asterisks as there are letters in the random word you pick.

You can actually do that quite easily like so:

...

string asterickWordIntial = words[randomNumber].ToUpper();

// create a string with the * character repeated as many times as the
// length of the random word
string asterickWordFinal = new string('*', asterickWordInitial.Length);

HangTxtBox.Text = asterickWordFinal;

Note that your code assumes no words can have non-letter characters, which is probably fine for hangman, you just have to make sure you know that when generating the word list.

Upvotes: -1

sshanzel
sshanzel

Reputation: 389

Are you trying to get the value of the textbox? if so it should be like this asterickWordFinal = Convert.ToString(HangTxtBox.Text);

Upvotes: 0

David
David

Reputation: 218827

You've got it backwards. The = assignment operator doesn't take the left value and put it in the right variable, it takes the right value and puts it in the left variable. Like this:

HangTxtBox.Text = asterickWordFinal;

This essentially means:

Take the value of asterickWordFinal and put it in HangTxtBox.Text.

Upvotes: 2

Related Questions