Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6655215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:29:23+00:00 2026-05-26T01:29:23+00:00

So I have the following code, and It works nearly flawlessly, except for the

  • 0

So I have the following code, and It works nearly flawlessly, except for the fact that no matter what I do I cannot get the parts that are like: for (int.parse(txtGuess.Text) == numbGen) it will not recognize ‘numbGen’ no matter where I place it in the code. I cannot place it inside the button click function because I don’t want the number to change, unless they’ve gotten it correct or re-opened the form.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;


    namespace WindowsApplication1
    {
        public partial class rndNum : Form
        {
            public rndNum()
            {
            }

            private void rndNum_Load(object sender, EventArgs e)
            {
                int numbGen = RandMake(0, 100);
            }

            private void txtGuess_TextChanged(object sender, EventArgs e)
            {

            }

            private void btnEval_Click(object sender, EventArgs e)
            {
                int guesses = 0;
                while (txtGuess.Text != "")
                {
                    if (int.Parse(txtGuess.Text) == numbGen)
                    {
                        MessageBox.Show("You got it!", "Congratulations!");
                        break;
                    }
                    else if (int.Parse(txtGuess.Text) > numbGen)
                    {
                        MessageBox.Show("Sorry, but you're too high. The number was " + numbGen + "!", "Please try again.");
                        txtGuess.Clear();
                        txtGuess.Focus();
                        guesses++;
                        break;
                    }
                    else if (int.Parse(txtGuess.Text) < numbGen)
                    {
                        MessageBox.Show("Sorry, but you're too low. The number was " + numbGen + "!", "Please try again.");
                        txtGuess.Clear();
                        txtGuess.Focus();
                        guesses++;
                        break;
                    }
                }
            }



            private static int RandMake(int min, int max)
            {
                Random mkRnd = new Random();
                return mkRnd.Next(min, max);
            }
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:29:23+00:00Added an answer on May 26, 2026 at 1:29 am

    numbGen must be a class member.

    Change

            private void rndNum_Load(object sender, EventArgs e)
            {
                int numbGen = RandMake(0, 100);
            }
    

    to

            private int numbGen;
            private void rndNum_Load(object sender, EventArgs e)
            {
                numbGen = RandMake(0, 100);
            }
    

    actually you don’t need to put the initialization in Form.Load, you can initialize a class member directly.

        public partial class rndNum : Form
        {
            private int numbGen = RandMake(0, 100);
            public rndNum()
            {
            }
    

    and to further refine: if you want to make sure the values is not changed you can make it readonly

        public partial class rndNum : Form
        {
            private readonly int numbGen = RandMake(0, 100);
            public rndNum()
            {
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: Bear in mind that while this code works on
I have the following code that works on PHP5 to send a HTTP POST
I have the following code that works on Firefox and Chrome $("#adicionarItem").click(function(){ $.ajax({ type:
I have the following code that works fine in IE: <HTML> <BODY> <script language=JavaScript>
hey all, Im stumped. I have the following code..works fine in all browsers except
I have the following code which works just fine when the method is POST,
I have the following code which works fine. However, I only want to return
I have the following code which works perfectly, but I want to allow access
I have the following code which works when i put it in any blank
At the moment I have the following code which works fine. label = new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.