Click here to Skip to main content
15,893,337 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So were making a program that involves doing math with radio buttons. The part I'm lost on is we have 2 sets of radio buttons, a label, a text box, and 2 buttons. the first radio buttons set up wether the problem will be addition, subtraction, or multiplication. The second set chooses a range of either 1-12 or 1-24. Based on those 2 choices the first button generates a problem with 2 random numbers within the proper range, and creates a math equation of the correct format. The user then solves the problem, placing the answer in the text box. The last button then checks the answer and tells the user if their right or wrong.

I started with doing an if statement for the addition radio button and nested an if else statement inside of it for the range. I then created the variables x, y, and z as integers in this class. x and y are random numbers within the range and z is the addition of them. I then copied, pasted, and edited for multiplication and subtraction. So my random variables are set and the computer has solved the problem. But i create a new class when i start coding for the "Check Answer" Button. My problem is i can't reference those random numbers to check with their input. Any tips on how to do this?

A copy of what the final project is supposed to look like can be found here: http://jbwyatt.com/202/a/a4/a4Math.html[^]

Also i can put up the code if someone tells me how. Sorry for any confusion, this is my first post here.

Edit:

Here is an example of the code
C#
private void btnNewProblem_Click(object sender, EventArgs e)
       {
           int x, y, z;
           Random rnd = new Random();
           if (radAddition.Checked == true)
           {
               if (radSmallRange.Checked == true)
               {
                   x = rnd.Next(0, 13);
                   y = rnd.Next(0, 13);
               }
               else
               {
                   x = rnd.Next(0, 25);
                   y = rnd.Next(0, 25);
               }
               z = x + y;
               lblProblem.Text = x + " + " + y + " = ";
           }
private void btnCheckAnswer_Click(object sender, EventArgs e)
       {
           try
           {
               int Answer = int.Parse(txtAnswer.Text);
           }
           catch
           {
               MessageBox.Show("Please Enter a Whole Number", "Invalid Entry");
           }


       }


Im trying to use the "z" variable in both button events. And since its dealing with random numbers, I need to reference the variable in both btnNewProblem and btnCheckAnswer. I need to Fetch the value for z created in btnNewProblem and compare it to the users input when btnCheckAnswer is clicked
Posted
Updated 28-Mar-15 1:17am
v3
Comments
Sergey Alexandrovich Kryukov 28-Mar-15 0:48am    
Not clear. What does it mean, "I cannot reference..."? And what can you? Now, if you want to post code... Not quite. Create a sample code which focuses only to one problem you are asking about, remove all irrelevant. Make sure the code is comprehensive, desirably in one file. Use "Improve question", paste it make sure you format it correctly. Explain the problem. Add some comments to the code related to the problem and reference those comment in your description outside code. Imagine that you are the one who reads you post for the first time; make sure the nothing is missing.
—SA
Member 11561872 28-Mar-15 7:18am    
I attemped to clarify it and added a snippet of code from my project. All editing was done after the "Edit" line.
BillWoodruff 28-Mar-15 3:27am    
"But i create a new class when i start coding for the "Check Answer" Button. My problem is i can't reference those random numbers to check with their input. Any tips on how to do this?" So why do you create a new class ? Without the details of why your "new class" cannot access the variables, I don't think we can assist you.
Member 11561872 28-Mar-15 7:20am    
It forces me to create a new class when i create a new event handler. Im using the variable in the event created for the btnNewProblem, and then i need to fetch it and use it in the event for the btnCheckAnswer. I updated with an example of my code.

1 solution

First off, don;t create a new Random instance each time they press the button - move that to a private variable and use that instance each time - you will get a better random distribution of numbers that way.
C#
private Random rnd = new Random();
private void btnNewProblem_Click(object sender, EventArgs e)
       {
           int x, y, z;
           if (radAddition.Checked == true)
           {
               if (radSmallRange.Checked == true)
               {
                   x = rnd.Next(0, 13);
...
           }

Then, move x, y, and z to also be class level private variables:
C#
private Random rnd = new Random();
private int x = -1;
private int y = -1;
private int z = -1;
private void btnNewProblem_Click(object sender, EventArgs e)
       {
           if (radAddition.Checked == true)
           {
               if (radSmallRange.Checked == true)
               {
                   x = rnd.Next(0, 13);
...
           }
Now your numbers are preserved as part of the class - so you can use them in both methods and you will reference the same values. (I set them to -1 both so they have a value, and so that you can check if a problem has been set when the use clicks the "answer" button)

Make sense?
 
Share this answer
 
Comments
Member 11561872 28-Mar-15 8:21am    
Thanks OriginalGriff. I can still set x, y, and z to the random variables right? they don't need to be -1? i.e. private int x = rnd.Next();?
OriginalGriff 28-Mar-15 8:26am    
You can, and they don't - but you don't want to do it like that.
Only set the values to a random number when the user presses the button - the syntax is exactly the same as in your example code, only you don't declare any variables inside the event handler methods.

(You shouldn't try to use rnd in the declaration line of the variables anyway, because you don't know that rnd has been initialised yet - I'm pretty sure Visual Studio will complain if you try! :laugh: )
Member 11561872 28-Mar-15 8:35am    
Ok, First I'm sorry I'm not picking this up right away. This is actually for a introduction class so I'm just a beginner with coding.
Second, if i set x = rnd.Next(0,13); under btnNewProblem like in your example, it will change private x = -1; so that when i write z = x+y; in btnNewProblem, if x=5 and y=6 z should equal 11. So when i write under btnAnswerCheck to compare z to int Answer it should compare 11 to the user input correct? Was that followable?
OriginalGriff 28-Mar-15 8:49am    
Yes - that's the whole idea.
By declaring them as part of the class instead of as part of the method, all the methods in the class "share" the same variable - so if you set it in one click event, you can access it in another. It's a bit like putting you mobile in your shirt pocket instead of in the glove box of your car: it doesn't matter which car you are in, the shirt pocket remains "part of you" and so you can always get your phone. The glove box is "part of the car" so you have to be in the right car to access it.

Give it a try - you'll see what I mean!

[edit]Typo: "But" for "By" - OriginalGriff[/edit]
Member 11561872 28-Mar-15 8:26am    
Actually looking closer, I have noticed some things that may not work. I need it to be under the btnNewProblem method. This is because it needs to create a new set of random variables each time the button is clicked.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900