在函数外部使用变量

时间:2013-11-28 20:04:12

标签: c#

我正在制作一个代数方程求解器,您可以在其中输入2个值,并以x + (value1)= (value2)的格式为您提供x值。

我想出了如何将value1value2中给出的数据转换为整数,但是值{1}卡在private void中,我无法在该空格之外使用它。我如何在他们各自的私人空间之外使用value1value2

如果你想出来了,我如何能够将x的值输出到程序窗口中(我正在创建一个Windows窗体应用程序)?

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 个答案:

答案 0 :(得分:2)

在课程级别定义yz,然后在不同的事件中使用它。

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);
    }
}

在您当前的代码中,因为您在方法中定义它们,所以它们仅限于它们的范围,并且在其范围之外不可见。

编辑:刚刚注意到代码中的一件事(感谢@Rahul Tripathi),您正在使用intConvert.ToInt32将TextBox值转换为int.Parse ,两者都会产生相同的效果,你可以使用其中任何一种,只是不要同时使用它们。您还可以查看int.TryParse来解析更安全的选项,因为如果解析失败,它不会引发异常。

答案 1 :(得分:0)

答案正如Habib所说,因为变量是在方法内部创建的,所以它们仅限于该范围。如果您希望它在方法之外可用,请在方法之外创建它们。

第二点我不明白为什么你使用这两个偶数处理程序。您可以为y和z使用两个文本框,为x使用第三个文本框,为结果使用一个按钮。

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);
    }
}

显然最好使用int.TryParse方法而不是Int.Parse方法将字符串转换为整数,因为无法保证用户将输入有效的整数值。

答案 2 :(得分:0)

不要使用void方法。使用函数返回所需的值。然后在触发事件时调用该函数。这是一个使用泛型集合的示例:

//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());