Search code examples
c#scoping

c# scoping issue, simple fix i'm sure


So I have the following code, and It works nearly flawlessly, except for the fact that no matter what I do I cannot get the parts that are like: for (int.parse(txtGuess.Text) == numbGen) it will not recognize 'numbGen' no matter where I place it in the code. I cannot place it inside the button click function because I don't want the number to change, unless they've gotten it correct or re-opened the form.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;


    namespace WindowsApplication1
    {
        public partial class rndNum : Form
        {
            public rndNum()
            {
            }

            private void rndNum_Load(object sender, EventArgs e)
            {
                int numbGen = RandMake(0, 100);
            }

            private void txtGuess_TextChanged(object sender, EventArgs e)
            {

            }

            private void btnEval_Click(object sender, EventArgs e)
            {
                int guesses = 0;
                while (txtGuess.Text != "")
                {
                    if (int.Parse(txtGuess.Text) == numbGen)
                    {
                        MessageBox.Show("You got it!", "Congratulations!");
                        break;
                    }
                    else if (int.Parse(txtGuess.Text) > numbGen)
                    {
                        MessageBox.Show("Sorry, but you're too high. The number was " + numbGen + "!", "Please try again.");
                        txtGuess.Clear();
                        txtGuess.Focus();
                        guesses++;
                        break;
                    }
                    else if (int.Parse(txtGuess.Text) < numbGen)
                    {
                        MessageBox.Show("Sorry, but you're too low. The number was " + numbGen + "!", "Please try again.");
                        txtGuess.Clear();
                        txtGuess.Focus();
                        guesses++;
                        break;
                    }
                }
            }



            private static int RandMake(int min, int max)
            {
                Random mkRnd = new Random();
                return mkRnd.Next(min, max);
            }
}

}


Solution

  • numbGen must be a class member.

    Change

            private void rndNum_Load(object sender, EventArgs e)
            {
                int numbGen = RandMake(0, 100);
            }
    

    to

            private int numbGen;
            private void rndNum_Load(object sender, EventArgs e)
            {
                numbGen = RandMake(0, 100);
            }
    

    actually you don't need to put the initialization in Form.Load, you can initialize a class member directly.

        public partial class rndNum : Form
        {
            private int numbGen = RandMake(0, 100);
            public rndNum()
            {
            }
    

    and to further refine: if you want to make sure the values is not changed you can make it readonly

        public partial class rndNum : Form
        {
            private readonly int numbGen = RandMake(0, 100);
            public rndNum()
            {
            }