[英]'double' does not contain a definition for 'Text' and no extension method
Me and my teacher cannot figure this out. 我和我的老师无法弄清楚。 It's a windows forms app with all the appropriate fields linked, this is my only issue. 这是一个带有所有适当字段链接的Windows窗体应用程序,这是我唯一的问题。
Error 1 'double' does not contain a definition for 'Text' and no extension method 错误1'double'不包含'Text'的定义,也没有扩展方法
here's my code: 这是我的代码:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void calculate_Click(object sender, EventArgs e)
{
double total = 0; //total amount
double tip = 0; //tip percentage
double meal = 0; //meal amount
tip = Convert.ToDouble(tip.Text) / 100;
meal = Convert.ToDouble(meal.Text);
total = (meal * tip);
total.Text = "$ " + Convert.ToString(total);
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
I think you are trying to display the result into a Textbox; 我认为您正在尝试将结果显示到文本框中; but Here in the code you declared total
as double. 但是在这里,您在代码中将total
声明为double。 please use the textbox name here ( total.Text
) in place of the variable total
; 请在这里使用文本框名称( total.Text
)代替变量total
; And also use .ToString()
instead of .Text
inside your Convert.ToDouble(
并在Convert.ToDouble(
.ToString()
使用.ToString()
代替.Text
Few more tips for you: 为您提供更多提示:
total.ToString("$#00.00");
使用total.ToString("$#00.00");
to get the the number prefixed with a $
symbol and rounded to 2
digits after the decimal 获取以$
符号为前缀的数字,并四舍五入到小数点后2
Let me assume the UI elements are : 让我假设UI元素是:
//txtTip be the Textbox for tip percentage
//txtMeal be the Textbox for meal amount
//txtTotal be the Textbox for Total Amount
Then your code will be like the following: 然后,您的代码将如下所示:
double total = 0; //total amount
double tip = 0; //tip percentage
double meal = 0; //meal amount
if(! double.TryParse(txtTip.Text,out tip))
{
// show conversion failed error
}
if(! double.TryParse(txtMeal.Text,out meal))
{
// show conversion failed error
}
tip = tip / 100;
total = (meal * tip);
txtTotal.Text =total.ToString("$#00.00") ;
It looks like you named your class-level Textboxes
the exact same as your method-scoped variables. 看起来您为类级Textboxes
命名的方式与方法范围的变量完全相同。 Or at least, that's my best assumption without knowing what your textboxes are actually called. 或至少,这是我的最佳假设,却不知道您的文本框实际被调用了什么。
Your problem is you are trying to find a property Text
on a double
, which certainly does not exist. 您的问题是,您正在尝试在double
上查找属性Text
,该属性肯定不存在。 If you actually did name your textboxes the same (which is legal in C#), you'll want to reference them by using this.total
and this.tip
when trying to set the Text
property. 如果您确实使用相同的名称命名了文本框(在C#中是合法的), this.tip
在尝试设置Text
属性时将要使用this.total
和this.tip
来引用它们。
It's worth noting that I'm concerned for your education if your teacher is unable to debug this. 值得一提的是,如果您的老师无法对此进行调试,我将对您的教育感到担忧。
I think this is a mistake of use local variable and global variable. 我认为这是使用局部变量和全局变量的错误。
class Test
{
// global variable.
int va = 1;
int vb = 2;
public void local()
{
bool va = false; // local variable
Console.WriteLine(va); // va is bool here.use local value.
Console.WriteLine(vb); // vb is int, use global value.
}
}
in your code, you declared local variables. 在代码中,您声明了局部变量。
public partial class Form1 : Form
{
private void calculate_Click(object sender, EventArgs e)
{
// declare local variables...
// overwrite global varables.
double total = 0; //total amount
double tip = 0; //tip percentage
double meal = 0; //meal amount
// maybe, you want to invoke textbox.Text.
// but, tip(TextBox) object is overwrote by double.
// double has not the Text property, throw exception.
tip = Convert.ToDouble(tip.Text) / 100;
meal = Convert.ToDouble(meal.Text);
total = (meal * tip);
total.Text = "$ " + Convert.ToString(total);
}
}
how to fix it? 如何解决? just declare different variable name, like this: 只需声明不同的变量名,像这样:
private void calculate_Click(object sender, EventArgs e)
{
// make the local variable name is different with global.
double d_total = 0; //total amount
double d_tip = 0; //tip percentage
double d_meal = 0; //meal amount
d_tip = Convert.ToDouble(tip.Text) / 100; // tip is TextBox here
d_meal = Convert.ToDouble(meal.Text);
d_total = (d_meal * d_tip);
total.Text = "$ " + Convert.ToString(d_total);
}
or use this
like this: 或使用this
这样的:
private void calculate_Click(object sender, EventArgs e)
{
double total = 0; //total amount
double tip = 0; //tip percentage
double meal = 0; //meal amount
tip = Convert.ToDouble(this.tip.Text) / 100;
meal = Convert.ToDouble(this.meal.Text);
total = (meal * tip);
this.total.Text = "$ " + Convert.ToString(total);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.