1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: C# Random numbers

Discussion in 'Software Development' started by supergoten, Apr 14, 2010.

Thread Status:
Not open for further replies.
Advertisement
  1. supergoten

    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.
     
  2. supergoten

    supergoten Thread Starter

    Joined:
    Apr 14, 2009
    Messages:
    28
    Sorry for the double posting, but does anyone have any help?
     
  3. servalsoft

    servalsoft

    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...
    }
     
  4. supergoten

    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

    ???
     
  5. servalsoft

    servalsoft

    Joined:
    Apr 14, 2010
    Messages:
    6
    Can you post your source code so that I can take a look please
     
  6. supergoten

    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)
            {
    
            }
    
        }
    }
     
  7. servalsoft

    servalsoft

    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.
     
  8. supergoten

    supergoten Thread Starter

    Joined:
    Apr 14, 2009
    Messages:
    28
    Ok thanks I have it solved! You've been a big help!
     
  9. Sponsor

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 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/916951

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice