I am working with Microsoft Visual C# and keep getting the error "Use of unassigned local variable 'letter'" and everything I've tried has not fixed it, not to mention looking on the web. What is the issue?

private void Btn_Submit_Click(object sender, EventArgs e)
            char letter;
            int number;

            Txt_Letter.Text = letter.ToString(); // the "letter" before ToString is showing up the error
            number = Convert.ToInt32(Txt_Number.Text);

            if (((letter >= 'a' && letter <= 'z') ||
                (letter >= 'A' && letter <= 'Z')) &&
                (number > 0 && number < 27))
                Alphanum(letter, number);
                MessageBox.Show("Please pick a letter and number between 1 and 26.");
                Txt_Letter.Text = "";
                Txt_Number.Text = "";


the issue is you had declared letter

char letter;

so it is null and when you do this

Txt_Letter.Text = letter.ToString();

cannot say null.ToString() so need to think what should it be

Hmm alright well how do I fix that then? If I type

letter = Txt_Letter.Text

the error that pops up is "Cannot implicitly convert type 'string' to 'char'", I'm not sure how to correctly convert the letter variable to string.

You should give your variables an initial value when you create them (especially chars and strings).

If you want the letter variable to have an empty value you can use:

string letter = string.Empty;


string letter = "";
This article has been dead for over six months. Start a new discussion instead.