0

I am making an algebra equation solver in which you enter 2 values and gives you the x value in a format of x + (value1)= (value2).

I figured out how to convert the data given in value1 and value2 to an integer but value one gets stuck in a private void and I can't use it outside of that void. How am I able to use value1 and value2 outside their respective private voids?

If you figured that out, how am I able to output the value of x into the program windows ( I'm creating a windows form application)?

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 equation_solver
{
    public partial class EquationSolver : Form
    {
        public EquationSolver()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {
            int z;
            z = Convert.ToInt32(textBox1.Text);
            z = int.Parse(textBox1.Text);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        { 
            int y;
            y = Convert.ToInt32(textBox1.Text);
            y = int.Parse(textBox1.Text);
        }    
    }
}
3
  • Is there a reason for those two event handlers? Wouldn't a single button which then reads the values from the textbox suffice? Commented Nov 28, 2013 at 20:07
  • In the future, please use a more descriptive title for your question. If I have not represented your question with my title, please edit your post. Commented Nov 28, 2013 at 20:08
  • By the way, y = Convert.ToInt32(textBox1.Text); y = int.Parse(textBox1.Text); is doing exactly the same operation twice. There is no purpose to using both; the result of both calls is an int. Commented Nov 28, 2013 at 20:08

3 Answers 3

2

Define y and z at class level and then use that in different events.

public partial class EquationSolver : Form
{
    int z = 0; //class level
    int y;

    public EquationSolver()
    {
        InitializeComponent();
    }

    private void label1_Click(object sender, EventArgs e)
    {
        z = Convert.ToInt32(textBox1.Text);
        z = int.Parse(textBox1.Text);
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        y = Convert.ToInt32(textBox1.Text);
        y = int.Parse(textBox1.Text);
    }
}

In your current code since you are defining them inside a method they are limited to their scope and not visible outside of their scope.

EDIT: Just noticed one thing in your code (thanks to @Rahul Tripathi), you are converting TextBox values to int using Convert.ToInt32 and int.Parse, both would have the same effect, you can use either of them, just don't use both. You can also look at int.TryParse for parsing which is safer options since it will not raise the exception if parsing fails.

Sign up to request clarification or add additional context in comments.

2 Comments

Ha ha ha..I just deleted the comment. I knew you will catch that +1;)
Also you can mention that y = Convert.ToInt32(textBox1.Text); y = int.Parse(textBox1.Text); are same things!
0

well the answer is as Habib said, as the variable is created inside the method, they are limited to that scope. if you want it to be available outside the method, create them outside the method.

second point i do not get why are you using those two even handlers. You could have used two text boxes for y and z and a third text box for x and a button for the result.

public partial class EquationSolver : Form
{
    int x, y, z;

    //Button in the form named btnSolveForX
    private void btnSolveForX_Click(object sender, EventArgs e)
    {
        y = Int.Parse(txtY.Text);
        z = Int.Parse(txtZ.Text);
        x = z - y; // As x + y = z -> x = z -y
        txtX.Text = z.ToString();
        PrintResultInConsole();
    }

    //This function is to show that the x is still available
    //outside the scope of the functions as it is created
    //in the scope of class which means it is available anywhere
    //inside the class.
    private void PrintResultInConsole()
    {
        Console.WriteLine("Value of x is {0}",x);
    }
}

Obviously it is better to use int.TryParse method instead of Int.Parse method to convert string to integer as there is no guarantee that the user is going to enter valid integer values.

Comments

0

Don't use a void method. Use a function to return the values you need. Then call that function when your event is fired. Here's an example that uses a generic collection:

//Here is the code that defines the method:
private double Sum(List<double> dataEntries)
    {
        double total = 0;
        foreach (double d in dataEntries)
        {
            total += d;
        }
        return total;
    }

// Here is the code that calls the method:
List<double> myList = new List<double>();
myList.Add(5.3);
myList.Add(3.34);
myList.Add(3.453);
double mySum = Sum(myList);
MessageBox.Show(mySum.ToString());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.