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;
开发者_如何学JAVA }
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);
}
}
}
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()
{
}
int numbGen;
private void rndNum_Load(object sender, EventArgs e)
{
numbGen = RandMake(0, 100);
}
try this!!!
The others already commented on the scoping issue you noticed. But your RandMake
method is flawed too. You should not create an instance of Random
for each number, but reuse the instance.
The problem here is that new Random()
uses the time as seed, and the time only changes every few milliseconds. This means that if you call RandMake
several times within that time interval you will get the same "random" number.
This doesn't seem to be a immediate problem because you only call it once, but you should be aware of this in the future.
When you put numbGen's declaration on the inside of a method, the number only exist in that functions scope. You have to place it outside like this:
int numbGen;
private void rndNum_Load(object sender, EventArgs e)
{
numbGen = RandMake(0, 100);
}
精彩评论