2008-12-12 577 views
10

這個錯誤應該是一個簡單的錯誤,但我似乎無法使它工作。問題在於這個非常相同的代碼在程序的早期工作。我不認爲它有任何理由在這個實例上發送錯誤,而不是以前的四個錯誤。參考下面的代碼,並隨時提供您可能有的任何批評,因爲它會讓我變得更好。如果有關係,我正在使用Sharp Develop 2.2。C#,運算符'*'不能應用於'double'和'decimal'類型的操作數

下面是工作的代碼的一個例子:

void calc2Click(object sender, EventArgs e) 
{ 
    if (!String.IsNullOrEmpty(tb2_fla.Text) & String.IsNullOrEmpty(tb2_e.Text) | String.IsNullOrEmpty(tb2_fla.Text) & String.IsNullOrEmpty(tb2_e.Text) | String.IsNullOrEmpty(tb2_e.Text)) 
    { 
     MessageBox.Show("Enter either kVA and Voltage or FLA and Voltage", "Invalid Data Entry", MessageBoxButtons.OK); 
    }  

     if (!String.IsNullOrEmpty(tb2_kva.Text) & !String.IsNullOrEmpty(tb2_e.Text)) 
    { 
      decimal x, y, z; 
      x = decimal.Parse(tb2_kva.Text);  
      y = decimal.Parse(tb2_e.Text); 
      z = (x * 1000)/(1.732050808m * y); //the m at the end of the decimal allows for the multiplication of decimals  
      tb2_fla.Text = z.ToString(); 
      tb2_fla.Text = Math.Round(z,2).ToString(); 
    } 
     else 
    { 
     if (!String.IsNullOrEmpty(tb2_fla.Text) & !String.IsNullOrEmpty(tb2_e.Text)) 
    { 
      decimal x, y, z; 
      x = decimal.Parse(tb2_fla.Text);  
      y = decimal.Parse(tb2_e.Text); 
      z = (x * y * 1.732050808m)/1000; //the m at the end of the decimal allows for the multiplication of decimals 
      tb2_kva.Text = Math.Round(z,2).ToString(); 

    } 

這裏是發送該錯誤在這篇文章的主題行的代碼的示例:

void Calc4Click(object sender, EventArgs e) 
{ 
     if (!String.IsNullOrEmpty(tb4_fla.Text) && String.IsNullOrEmpty(tb4_e.Text) || String.IsNullOrEmpty(tb4_kw.Text) & String.IsNullOrEmpty(tb4_e.Text) || String.IsNullOrEmpty(tb4_e.Text)) 
     { //If values are entered improperly, the following message box will appear 
     MessageBox.Show("Enter either FLA and Voltage or kW and Voltage", "Invalid Data Entry", MessageBoxButtons.OK); 
     } 


     if (!String.IsNullOrEmpty(tb4_fla.Text)&& !String.IsNullOrEmpty(tb4_e.Text)&& String.IsNullOrEmpty(tb4_kw.Text)) 
     {//If the user eneters FLA and Voltage calculate for kW 

      decimal x, y, z; 
      x = decimal.Parse(tb4_fla.Text); 
      y = decimal.Parse(tb4_e.Text); 
      z = (x*y)*(.8 * 1.732050808m); 
      tb4_kw.Text = Math.Round(z,0).ToString(); 

     }    

     if (!String.IsNullOrEmpty(tb4_kw.Text) && !String.IsNullOrEmpty(tb4_e.Text) && String.IsNullOrEmpty(tb4_fla.Text)) 
     {;//If the user enters kW and Voltage calculate for FLA 
      decimal x, y, z; 
      x = decimal.Parse(tb4_kw.Text); 
      y = decimal.Parse(tb4_e.Text); 
      z = (1000 * x)/(y * 1.732050808m)* .8; 
      tb4_fla.Text = Math.Round(z,0).ToString(); 
     } 

    } 

我明白任何幫助,我可以得到。 謝謝。

回答

27
.8m instead of .8 
+0

謝謝你指出。我暗中希望這不是那麼簡單。 :) – 2008-12-12 18:33:11

3

在該線的位置:

Z =(X Y)(0.8 *1.732050808米);

您將.8指定爲一個文字,但沒有'm'後綴,該文字指定一個double。

z =(x y)(.8m * 1.732050808m);

會解決它。

4

你沒有說哪條線路是的,但我打賭這兩個:

z = (x*y)*(.8 * 1.732050808m); 

和:

z = (1000 * x)/(y * 1.732050808m)* .8; 

請注意,你的0.8不具備「M '限定詞。我看到你的每一個地方都提供了這個。

相關問題