hi
im trying to make a simple guess a number game for an assignment and i dont get why im getting a does not exist in the current context error here is my code i put the part that gets an error in red any help appreciated thx

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

namespace GuessANumber
{

    public partial class GuessANumber : Form
    {



        public GuessANumber()
        {
            InitializeComponent();

            Random RandomClass = new Random();
            int randomNumber;
            randomNumber = RandomClass.Next(1, 5);

            Random RandomClassB = new Random();
            int randomNumberB;
            randomNumberB = RandomClassB.Next(1, 5);
            while (randomNumberB == randomNumber)
                randomNumberB = RandomClassB.Next(1, 5);
  
        }
        private void Hint_MouseHover(object sender, EventArgs e)
        {
            Hint.Text = "The correct button is not #" + randomNumberB.ToString("C");  
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void GuessANumber_Load(object sender, EventArgs e)
        {

        }


    }
}

randomNumberB currently only exists in the function called GuessANumber(), for this number to work in your hint_mouseover function you would need to declare int randomNumberB outside of any function thus making it a class attribute.

just write: private int randomNumberB; on the line before GuessANumber and remove the declaration from the GuessANumber() function.

thx that did it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.