2008-12-12 7 views
10

Dieser Fehler sollte einfach sein, aber ich kann es nicht funktionieren. Das Problem liegt in der Tatsache, dass der gleiche Code früher im Programm funktioniert. Ich sehe keinen Grund dafür, dass bei dieser Instanz ein Fehler gesendet wird und nicht bei den vier vorherigen. Verweisen Sie auf den unten stehenden Code, und zögern Sie nicht, Ihre Kritik zu äußern, denn sie sollte mich besser machen. Wenn es darauf ankommt, verwende ich Sharp Develop 2.2. HierC#, Operator '*' kann nicht auf Operanden vom Typ 'double' und 'decimal' angewendet werden

ist ein Beispiel für den Code, der funktioniert:

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(); 

    } 

Hier ist das Beispiel für den Code, der den Fehler in der Betreffzeile dieser Nachricht sendet:

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(); 
     } 

    } 

ich jede schätzen Hilfe, die ich bekommen kann. Danke.

Antwort

27
.8m instead of .8 
+0

Vielen Dank, dass Sie darauf hingewiesen haben. Ich habe insgeheim gehofft, dass es nicht so einfach ist. :) –

+0

Wofür steht das m? – Alex

+1

m erzwingt, dass das Literal als Dezimalzahl interpretiert wird. – Broam

3

In dieser Zeile hier:

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

Sie geben .8 als Literal an, aber ohne das Suffix 'm' gibt das Literal ein Double an.

z = (x y) (0,8m * 1,732050808m);

wird es beheben.

4

Sie hat nicht gesagt, welche Linie es war, aber ich wette, auf diese beiden:

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

Und:

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

Beachten Sie, dass Ihre .8 nicht die ‚m hat "Qualifier. Jeder andere Ort, an dem ich dich sehe, hat das geliefert.

Verwandte Themen