2012-12-02 31 views
0

label1是好的,但 是label2不正常而得到错误,计算数值的标签

private void button1_Click(object sender, EventArgs e) 
{ 
    double a = Convert.ToDouble(textBox1.Text); 
    double b = Convert.ToDouble(textBox2.Text); 
    double z = Convert.ToDouble(label1.Text); 

    label1.Text = (a + b).ToString(); // it is ok 
    label2.Text = (z + a).ToString(); // not ok and get error  
} 
+1

什么是你所得到的错误 – Rotem

+0

'双ž? - 转换...' - 你的代码 –

+0

'textBox1.Text','textBox2.Text',特别是'label1.Text'中的值是什么? –

回答

0

是的,有一些语法错误。不仅如此,但是一旦你让你的代码编译并运行,你将会遇到一个运行时错误。我决定纠正代码并评论问题是什么以及如何以及在某些情况下为什么应该以不同的方式进行评论是最简单的。

记住,即使我提供的代码,你可以仍然接收运行时(notcompile时间),根据最终用户输入的错误。使用模式提供我做出调整产生额外的(读旁边的新老代码中的注释

 double a = Convert.ToDouble(textBox1.Text); // Tip: I would recomment using Double.TryParse() instead of what you have here unless you know a number will always be in this box (I will provide an example with your 'z') 
     double b = Convert.ToDouble(textBox2.Text); // Tip: I would recomment using Double.TryParse() instead of what you have here unless you know a number will always be in this box (I will provide an example with your 'z') 
     /* double z = Convert.ToDouble(label1.Text); /* First off, you need to correct your syntax errors: 
                * You had: double z - Convert.toDouble(label1.Text); Notice the minus (-) and not equal (=) sign 
                * You also need to capatilize the 'T' in what you have 'toDouble' and change it to 'ToDouble' 
                * Second off, you need to correct your runtime errors: 
                * In this line, you are trying to convert something in this label (lbl1) to a double. This will not work because at 
                * runtime, there is nothing in this label because it has no text. Incase you don't know, if you are using Visual Studio, you should 
                * put a break point on this line by pressing F9. Then when you run your application, Visual Studio will halt/pause here so you can see 
                * value of label1.Text by moving your mouse over it. You will notice it is "" which cannot be converted 
                * There are a few possible solutions that you can emply to make this work but I will show you my prefered method below: */ 
     double z; 
     bool didItParseDouble = Double.TryParse(label1.Text, out z); /* The first paramater is what you want to try and parse.. in this case, you are trying to parse a label. 
               * If it can parse it, it will assign the value to the out parameter which is 'z' You must preceed this with the out keyword! 
               * If it can't parse, it will */ 
     if (didItParseDouble) 
     { 
      // It parsed/convered (although parse and convert are technically different) 
      // When you first run the application, you will NOT hit this block of code because ditItParse is false because label1.Text is "". (Put a break point here and you will see) 
      // This is where you should put your calcuations at. Don't forget, though, that if the user enters nothing for the first or second textbox, your code as is will fail so you should do a try parse there as well! 
      // There are many ways to handle this, various patters, this is just one. You can even put the Double.TryParse(...) in the if conditional statement! 
     } 
     else 
     { 
      // It couldn't parse so you could put some error handling here if you would like. 
     } 


     label1.Text = (a + b).ToString(); // C# is case sensitive so your code won't compile! You had: label1.TexT = (a + b).ToString(); 

     label2.Text = (z + a).ToString(); // Now that z is a valid double it should work. //not ok and get error 
+0

另外,我只有comme讨论了什么导致程序不能编译,以及运行时会发生什么。我不知道你的逻辑是什么,所以如果你有问题(逻辑与编码错误),请让我们知道你试图达到的逻辑。此外,如果您有任何其他问题,为什么我这样做,让我们知道。再次,有几种方法可以做到这一点,我这样做是为了让它容易理解。 – John

+0

谢谢约翰 它工作完美,没有错误,这是我想要的。 非常感谢 – user1867155

+0

很高兴它帮助你出去! – John