4m7u1
4m7u1

Reputation: 11

C# - Random Number Generator error

When I run this and play the game, only the number 0 is being generated each time. Can you help me figure what the problem is?

public partial class MainPage : PhoneApplicationPage
{
    int numberguessed;
    // Constructor
    public MainPage()
    {
        InitializeComponent();
        Random randnum = new Random();
        int numberguessed = randnum.Next(0,1000);
    }



    private void myButton_Click(object sender, RoutedEventArgs e)
    {
        myTextBlock.Text = " No worries ! Go again .. ";
        myTextbox.Text = "";
        myTextbox.Focus();
    }

    private void myButton2_Click(object sender, RoutedEventArgs e)
    {
        //string sval = myTextbox.Text;
        int ival = System.Convert.ToInt32(myTextbox.Text);
        if (ival == numberguessed)
            myTextBlock.Text = " You won ";
        else if (ival < numberguessed)
            myTextBlock.Text = "Your guess is too low !";
        else if (ival > numberguessed)
            myTextBlock.Text = "Your guess is too high !";
    }

    private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
        myTextbox.Focus();
    }

Upvotes: 1

Views: 217

Answers (3)

Farax
Farax

Reputation: 1477

change this part

int numberguessed;
    // Constructor
    public MainPage()
    {
        InitializeComponent();
        Random randnum = new Random();
        int numberguessed = randnum.Next(0,1000);
    }

To

 private int numberguessed;
    // Constructor
    public MainPage()
    {
        InitializeComponent();
        Random randnum = new Random();
        numberguessed = randnum.Next(0,1000);
    }

The problem is the fact that once you are declaring your field/property in the class and then creating a similar field with the declaration of int numberguessed again inside the constructor. This latter field only remains in scope inside the constructor and dies off once the constructor ends. However, the default value of all int fields are 0 and the field you are accessing is the one defined OUTSIDE the class. Hence you are getting only a default value.

Upvotes: 0

Rapha&#235;l Althaus
Rapha&#235;l Althaus

Reputation: 60493

You declare numberguessed as a field, and then redeclare a new local variable int numberguessed in MainPage(). In the other methods, the field value will be used. As it's not initialized, it will have the default value for an int, 0.

int numberguessed;
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            Random randnum = new Random();
            //remove int there like this
            //int numberguessed = randnum.Next(0,1000);
           numberguessed = randnum.Next(0,1000);
        }

By the way you should have a warning (or maybe it's just resharper doing it) stating that

Local variable numberguessed hides fields .MainPage.numberguessed

Upvotes: 4

BenM
BenM

Reputation: 4278

in this part

public MainPage()
    {
        InitializeComponent();
        Random randnum = new Random();
        int numberguessed = randnum.Next(0,1000);
    }

You are overwritting your top level numberguessed variable by prefixing it with "int". Change it to:

public MainPage()
    {
        InitializeComponent();
        Random randnum = new Random();
        numberguessed = randnum.Next(0,1000);
    }

Upvotes: 4

Related Questions