使用类-C#创建计算器,但无法解决错误
我正在尝试创建一个带有类的计算器。但是,使用来自互联网的参考资料,特别是来自本网站的参考资料() 它没有提到申报“信息”或任何东西 当我输入代码时,返回的错误列表信息在当前上下文中不存在 有没有办法修改下面的代码?非常感谢你使用类-C#创建计算器,但无法解决错误,c#,C#,我正在尝试创建一个带有类的计算器。但是,使用来自互联网的参考资料,特别是来自本网站的参考资料() 它没有提到申报“信息”或任何东西 当我输入代码时,返回的错误列表信息在当前上下文中不存在 有没有办法修改下面的代码?非常感谢你 public partial class Form4 : Form { public Form4() { InitializeComponent(); } private
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();
}
}
我快速查看了链接,他们似乎没有在任何地方声明信息,也没有表示他们已经覆盖了任何内容……我不知道 然而,这一行只是验证输入到两个文本框中的信息实际上是数字,而不是其他无法计算的信息 有很多方法可以用来检查这些数字。选项包括但不限于:
if(Int32.TryParse(txtNum1, out int temp1) && Int32.TryParse(txtNum2, out int temp2))
{
do stuff;
}
或
还有其他选择,但这两个可能值得研究。下载示例项目后,我了解了
信息
所指的内容。事实证明,这可能是为了向所有.NET语言公开VB核心库的某些方面。通过向项目中添加对Microsoft.VisualBasic的引用并添加以下内容,您可以在程序中使用它:
using Microsoft.VisualBasic;
到代码文件的顶部
(就我个人而言,我无法想象这种方法是非常有效的。它应该取一个
对象
,并确定它是否可以作为一个数字进行计算,我不知道它使用什么方法根据任何随机对象
进行推断。你可能最好使用Benny O尼尔建议。)有一个“下载代码”按钮,您应该下载代码,用文本光标在属性上点击visual stuido中的F12并找出它的定义位置。您的第一个选项不会很有用,因为第二个TryParse
将覆盖第一个TryParse
的结果。您将需要第二个临时变量。作为poi有趣的是,在C#7中,您甚至不需要temp
变量。您可以将解析写成Int32.TryParse(txtNum1,out int temp1)
和temp1
将可在if
块中访问。非常感谢!哦,你说得对!真丢脸。我会更新我的答案,以免误导他人。@Izzuan别忘了将给出的答案之一标记为解决方案。
using Microsoft.VisualBasic;