2017-05-20 3 views
-1

私はクラスで電卓を作成しようとしています。しかし、特にこのウェブサイトからの参照を使用する(https://www.sourcecodester.com/tutorials/c/7548/simple-calculator-using-class-c.htmlクラスC#で電卓を作成してもエラーを解決できない

"情報"または何かを宣言することは言及していませんでした。

私がコードを入力したとき、エラーリストは情報が現在のコンテキストに存在しません。

以下のコードを変更する方法はありますか?どうもありがとうございます。

public partial class Form4 : Form 
    { 


     public Form4() 
     { 
      InitializeComponent(); 
     } 

     private void Form4_Load(object sender, EventArgs e) 
     { 

     } 

     public void RadioButton_Click(object sender, System.EventArgs e) 
     { 

      //call a constructor method and return to cal as an instance of a class 
      calculate cal = new calculate(); 

      //declaring the string variable represent as a textbox 
      string txtnum1 = TextBox1.Text; 
      string txtnum2 = TextBox2.Text; 
      //declaring the double variable 
      double dbl_val1 = default(double); 
      double dbl_val2 = default(double); 



      if (**Information**.IsNumeric(txtnum1) && **Information**.IsNumeric(txtnum2)) //check if the textbox has a numeric value 
      { 
       //convert the string to double 
       dbl_val1 = double.Parse(txtnum1); 
       dbl_val2 = double.Parse(txtnum2); 

       //get the value of the converted variable 
       //to pass it into the variable in the class 
       cal.num1 = dbl_val1; 
       cal.num2 = dbl_val2; 

       //the condition is, if the radiobutton is clicked, 
       //the operation of MDAS executes. 
       if (Radio_Multiplication.Checked) 
       { 
        //result: 
        cal.multiply(); //call a subname in a class for multiplying 
       } 

       else if (Radio_Addition.Checked) 
       { 
        //result: 
        cal.add(); //call a subname in a class for adding 
       } 
       else if (Radio_Subtraction.Checked) 
       { 
        //result: 
        cal.subtract(); //call a subname in a class for subtracting 
       } 
      } 
      else 
      { 
       //the result is: 
       //if the textbox is empty or has a string value 
       TextBox3.Text = "Enter a number"; 
       return; 
      } 
      //put the result of the MDAS to a textbox. 
      TextBox3.Text = cal.total.ToString(); 
     } 
    } 
+0

「コードをダウンロード」ボタンがあります。コードをダウンロードしてください。プロパティ上にカーソルを置き、定義されている場所を確認します。 –

答えて

0

私はリンクを簡単に見ていたし、彼らはどこでも情報を宣言しているように見えないも、彼らは、彼らがそう何かをオーバーライドしてきたことが示されている...私は知りません。

しかし、この行は、2つのテキストボックスに入力された情報が実際に数値であり、計算できないものではないことを検証するだけです。

これらの番号の確認にはさまざまな方法があります。オプションが含まれるが、これらに限定されない:

if(Int32.TryParse(txtNum1, out int temp1) && Int32.TryParse(txtNum2, out int temp2)) 
{ 
    do stuff; 
} 

または

if(txtNum1.All(char.IsDigit) && txtNum2.All(char.IsDigit)) 
{ 
    do stuff; 
} 

あり、他のオプションがありますが、これらの二つはに探して価値があるかもしれません。

+0

2番目の 'TryParse'が最初の' TryParse'の結果を上書きするので、最初のオプションはあまり役に立たないでしょう。 2番目の一時変数が必要になります。 – Abion47

+0

興味深いことに、C#7では、 'temp'変数は必要ありません。代わりに 'Int32.TryParse(txtNum1、out int temp1)'として解析を書くことができ、 'temp1'は' if'ブロック内でアクセス可能になります。 – Abion47

+0

ありがとう! – Izzuan

0

サンプルプロジェクトをダウンロードすると、Informationが参照するものがありました。おそらく、VBコアライブラリの特定の側面をすべての.NET言語に公開するためのa class from the Microsoft.VisualBasic namespaceです。

using Microsoft.VisualBasic; 

をコードファイルの先頭に:あなたは、あなたのプロジェクトにMicrosoft.VisualBasicへの参照を追加し、追加することによって、あなたのプログラムでそれを使用することができます。

(個人的には、このアプローチが非常に効率的であるとは想像もしませんが、objectを使用して数値として評価できるかどうかを判断する必要があります。任意のobjectをランダムに選択してください。ベニーオニールが示唆している選択肢の1つを使用する方がよいでしょう)

+0

ああ、ありがとう! – Izzuan

+0

@Izzuan解決策として指定された回答の1つをマークすることを忘れないでください。 – Abion47

関連する問題