Solved: C# Random numbers

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

supergoten

Thread Starter
Joined
Apr 14, 2009
Messages
28
I'm working on a C# windows app that picks a random number, then based on the guesses, will tell you if the number is higher or lower than the one the user put in it. But I can't get it to work, easy question but how to i pull the int target into the form1.cs code?

Program.cs code:
Code:
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace RandomNumberGame
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Random r = new Random();
            int target = r.Next(0, 100);
            Application.Run(new Form1());
        }
    }
}
Form1.cs Code:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Chapter6Exercise10
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnNumber_Click(object sender, EventArgs e)
        {

            int x;
           x = Convert.ToInt32(txtNumber.Text);
           x = int.Parse(txtNumber.Text);

            if (x < target)
            {
                this.BackColor = System.Drawing.Color.Firebrick;
            }
            else
                if (x > target)
                {
                    this.BackColor = System.Drawing.Color.DodgerBlue;
                }
                else
                    {
                        this.BackColor = System.Drawing.Color.Khaki;
                        MessageBox.Show("Congrats!");
                    }

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

    }
}
Thanks for any help you can give.
 
Joined
Apr 14, 2010
Messages
6
Do not declare your "Target" variable inside the Program.cs class. You can write a tiny function/method that returns a random number from inside your form1.cs class, so that every time you need a random number you just call that method .ie


Code:
public int randomNumber ()
     {
          Random r = new Random();
          return (r.next (0, 100));
     }
Now every time you call that function it returns a random number between (0, 100). so you can now make your comparison this way:

Code:
if (x < randomNumber())
{
//Do something...
}
 

supergoten

Thread Starter
Joined
Apr 14, 2009
Messages
28
It looks like it is almost done, but there is one error:

'randomNumber': cannot declare instance members in a static class

???
 

supergoten

Thread Starter
Joined
Apr 14, 2009
Messages
28
Sure, no problem.

Program.cs:
Code:
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace RandomNumberGame
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
        public int randomNumber()
        {
            Random r = new Random();
            return (r.next(0, 100));
        }

    }
}
Form1.cs
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Chapter6Exercise10
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnNumber_Click(object sender, EventArgs e)
        {
            int x;
           x = Convert.ToInt32(txtNumber.Text);
           x = int.Parse(txtNumber.Text);

           if (x < randomNumber())
            {
                this.BackColor = System.Drawing.Color.Firebrick;
            }
            else
                if (x > randomNumber())

                {
                    this.BackColor = System.Drawing.Color.DodgerBlue;
                }
                else
                    {
                        this.BackColor = System.Drawing.Color.Khaki;
                        MessageBox.Show("Congrats!");
                    }

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

    }
}
 
Joined
Apr 14, 2010
Messages
6
1/ This line is useless x = Convert.ToInt32(txtNumber.Text);
2/ The randomNumder function should not be in program.cs, but in form.cs

I doubt you'll "ever" need to add any code at all to your program.cs class. Not at this programming level anyhow.
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Members online

No members online now.
Top