2013-11-28 92 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());