Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#, Operator '*' cannot be applied to operands of type 'double' and 'decimal'

This error should be a simple one but I cant seem to make it work. The problem lies in the fact that this very same code works earlier in the program. I don's see any reason for it to be sending an error on this instance and not the four previous ones. Reference the code below, and feel free to provide any criticism you may have as it should make me better. If it matters, I am using Sharp Develop 2.2.

Here is an example of the code that works:

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

    }

Here is the example of the code that sends the error in the subject line of this post:

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

    }

I appreciate any help that I can get. Thank you.

like image 919
tejas_grande Avatar asked Dec 12 '08 18:12

tejas_grande


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


1 Answers

.8m instead of .8
like image 162
Jimmy Avatar answered Oct 23 '22 04:10

Jimmy