2013-11-28 90 views
0

我正在制作一个代数方程求解器,其中输入2个值并以x + (value1)= (value2)的格式给出x值。在函数外部使用变量

我想出了如何将value1value2中给出的数据转换为一个整数,但是值被卡在private void中,我无法在该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); 
     }  
    } 
} 
+0

这两个事件处理程序是否有原因?不会有一个按钮,然后从文本框中读取值就足够了吗? – Haedrian

+0

将来,请为您的问题使用更具描述性的标题。如果我没有用我的标题表达您的问题,请编辑您的帖子。 – gunr2171

+0

顺便提一句,'y = Convert.ToInt32(textBox1.Text); y = int.Parse(textBox1.Text);'执行两次完全相同的操作。没有使用两者的目的;两个调用的结果都是int。 –

回答

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帕蒂),您使用的Convert.ToInt32int.Parse转换文本框值int,都将有同样的效果,你可以使用他们,只是不要同时使用两者。您还可以查看int.TryParse解析哪些更安全的选项,因为如果解析失败,它不会引发异常。

+0

@RahulTripathi,其中:P?只是误读它:) – Habib

+0

哈哈哈..我刚刚删除了评论。我知道你会接受+1;) –

+1

你也可以提到,y = Convert.ToInt32(textBox1.Text); y = int.Parse(textBox1.Text);是同样的东西! –

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方法将字符串转换为整数,因为没有保证该用户要输入有效的整数值。

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