Alright let me start off by saying I am a complete noob when it comes to programming so try to explain stuff a little easier for me. So anyways I'm trying to make this game where it generates a random number and when you type in a guess it will tell you if that guess was larger than, smaller than, or equal to the randomly generated number and you'll keep guessing until you eventually figure out the answer. So I used this code here:
public partial class Form1 : Form
{
int guess;
bool equal;
bool greater;
bool less;
Random rd = new Random();
int rand_num = rd.Next(1, 100);
public Form1()
{
InitializeComponent();
}
private void btn_guess_Click(object sender, EventArgs e)
{
lbl_equalTo.Visible = false;
lbl_greaterThan.Visible = false;
lbl_lessThan.Visible = false;
string guess_txt = txtbox_guess.Text;
guess = int.Parse(guess_txt);
equal = guess == rand_num;
greater = guess <= rand_num;
less = guess >= rand_num;
lbl_equalTo.Visible = equal;
lbl_greaterThan.Visible = greater;
lbl_lessThan.Visible = less;
}
}
But for some reason at the very beginning when I try to turn the Random rd into an integer to use it doesn't recognize it giving me the error CS0236. I then thought alright, maybe I'll just move the int rand_num = rd.Next(1, 100); down into the button click thing, and it recognized it and everything. But then I realized, oh crap now what happens is every time I click the button it generates a new number instead of keeping the old one to try to guess. So why can't I put the random number generator in the public partial class and what solution could I use to solve the problem (btw I'm using c#)?