2008-12-12 35 views
10

Este error debe ser simple pero no puedo hacer que funcione. El problema radica en el hecho de que este mismo código funciona antes en el programa. No veo ningún motivo para enviar un error en esta instancia y no las cuatro anteriores. Consulte el siguiente código y siéntase libre de proporcionar cualquier crítica que pueda tener, ya que debería mejorarme. Si es importante, estoy usando Sharp Develop 2.2.C#, el operador '*' no se puede aplicar a los operandos de tipo 'doble' y 'decimal'

Aquí se muestra un ejemplo del código que funciona:

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

    } 

Aquí es el ejemplo del código que envía el error en la línea de asunto de este mensaje:

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

    } 

Agradezco cualquier ayuda que puedo obtener. Gracias.

Respuesta

27
.8m instead of .8 
+0

Gracias por señalarlo. Esperaba en secreto que no fuera tan simple. :) –

+0

¿Qué significa m? – Alex

+1

m obliga a interpretar el literal como un decimal. – Broam

3

En esta línea aquí:

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

especificas .8 como un literal, pero sin el sufijo 'm', el literal especifica un doble.

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

lo arreglaré.

4

Usted no ha dicho qué línea era, pero estoy apostando por estos dos:

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

Y:

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

en cuenta que su 0.8 no tiene la 'm 'calificador. En cualquier otro lugar que veo, tú has provisto eso.

Cuestiones relacionadas