0

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#)?

  • Please be sure to include the full error text and stack trace when you get errors. As I'm sure you don't, most of us don't remember the specific error codes, instead we remember the human-readable parts. – ProgrammingLlama Mar 15 '21 at 05:30

1 Answers1

0

CS0236 in a nutshell is, You cant assign an intial value from a another instance value above. The code here

  Random rd = new Random();
  int rand_num = rd.Next(1, 100);

Why is that? Compiler has optimization doing at the back and this will not make sure that rd will get instantiated before rand_num Thats why IDE is telling you its error, to avoid that issue.

So What you can do to overcome this? Simple add that to the constructor, so the rd is guaranteed to have value when you access it

    Random rd = new Random();
    int rand_num = null;
    public Form1()
    {
        InitializeComponent();
        rand_num = rd.Next(1, 100);
    }
keysl
  • 2,127
  • 1
  • 12
  • 16
  • 1
    Thanks it is working now and it makes sense. Only thing is that it says int is a non-nullable type so I just set it to -1. – DuckMaster17 Mar 15 '21 at 05:12
  • Oh I forgot to add ? to make it nullable field. sorry :) you can add 0 or ? to make it a nullable. Glad it help tho – keysl Mar 15 '21 at 07:01
  • There's no need to initialize `rand_num` to _anything_ with its declaration, if it's just going to be set again in the constructor. Doing so is just a waste of code and CPU time. Also, the claimed motivation for the error is baseless; the C# specification dictates initialization order for field initializers, so if the language were to allow non-static field initializers to reference other non-static members, there would be no ambiguity about which field is initialized first. – Peter Duniho Mar 15 '21 at 07:14
  • Your right looking at again it makes zero sense to assign anything to it if I’m just going to be changing it right away. I also looked up and read about field initializers, and now I understand that they are what allow you initialize a value to a field inline instead of having to do it later in a constructor, but the value you assign can’t be from another field and you would have to do that in the constructor which is the case here. Now I think I’ve learned enough to never make the mistake again. Hopefully. – DuckMaster17 Mar 15 '21 at 08:40