7 Replies - 448 Views - Last Post: 30 April 2012 - 02:17 PM Rate Topic: -----

#1 jbarcus81  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 30-April 12

Error message with decimal to float

Posted 30 April 2012 - 10:04 AM

I am working on a project and I think I'm about done.. BUT, I am having a problem with the actual calculation of the answer... The error I am getting is 'Cannon implicityly convert type 'decimal' to 'float'. An explicit conversion exists (are you missing a cast?) ... Now here is my code that I am working with...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WorkerPieceCalculation
{
    public partial class Form1 : Form
    {
        int totalPcs = 0;
        float totalWages = 0;
        float avgPay = 0;
        int numEmp = 0;
  
        
        
        public Form1()
        {
            InitializeComponent();
        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnCalcPay_Click(object sender, EventArgs e)
        {
            int pcs = 0;
            pcs = int.Parse(tbPcs.Text.Trim());
            float pay = CalcPay(pcs);
            lblEarn.Text = string.Format("Pay: ${0:f2}", pay);
            lblEarn.Visible = true;
            numEmp = numEmp + 1;
            totalPcs = totalPcs + pcs;
            totalWages += pay;
            btnSummary.Enabled = true;

        }

        private void btnSummary_Click(object sender, EventArgs e)
        {
            lblTotalPcs.Text = string.Format("{0}", totalPcs);
            lblTotalWages.Text = string.Format("${0:f2}", totalWages);
            lblAvgPay.Text = string.Format("${0:f2}", totalWages / numEmp);
            gbxSummary.Visible = true;

        }

        private void btnClr_Click(object sender, EventArgs e)
        {
            tbEmp.Text = string.Empty;
            tbPcs.Text = string.Empty;
            lblEarn.Visible = false;
        }

        private void lblEarn_Click(object sender, EventArgs e)
        {

        }

        private void btnClrAll_Click(object sender, EventArgs e)
        {
            if ((MessageBox.Show("Are you sure?", "Confim", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes))
            {
                btnClr_Click(sender, e);
                totalPcs = 0;
                totalWages = 0;
                avgPay = 0;
                gbxSummary.Visible = false;
                btnSummary.Enabled = false;
            }

        }

        private void tbEmp_TextChanged(object sender, EventArgs e)
        {
            SetCalcButtonState();
        }

        private void tbPcs_TextChanged(object sender, EventArgs e)
        {
            SetCalcButtonState();
        }
        private void SetCalcButtonState()
        {
            btnCalcPay.Enabled = false;
            if (((tbEmp.Text.Trim().Length > 0) & (tbPcs.Text.Trim().Length > 0)))
            {
                int pcs = 0;
                if ((int.TryParse(tbPcs.Text.Trim(), out pcs)))
                {
                    btnCalcPay.Enabled = true;
                }
            }
        }
        private float CalcPay(int pcs)
        {
            float pay = 0;
            switch (pcs)
            {
                case 0:
                    pay = 0;
                    break;
                case 1: // Up to 199
                    pay = pcs * 0.5M;
                    break;
                case 200: // Up to 399
                    pay = pcs * 0.55M;
                    break;
                case 400: // Up to 599
                    pay = pcs * 0.6M;
                    break;
                default:
                    pay = pcs * 0.65M;
                    break;
            }
            return pay;
        }


    }
}



The error is pointing to that last block of code under private float CalcPay(int pcs) .... Any and all help is appreciated!! Am I confusing VB with C# here?? I need C# here. Thanks!!

Is This A Good Question/Topic? 0
  • +

Replies To: Error message with decimal to float

#2 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2533
  • View blog
  • Posts: 4,633
  • Joined: 24-September 10

Re: Error message with decimal to float

Posted 30 April 2012 - 10:12 AM

the M in those numbers type that number as a the Decimal type. A Decimal multiplied by int results in Decimal. 'pay' is a float... so you're setting a Decimal to a float.

You need to convert it.

...
case 200: // Up to 399
   pay = (float)(pcs * 0.55M);
...



...
case 200: // Up to 399
   pay = pcs * 0.55f;
...



...
case 200: // Up to 399
   pay = Convert.ToSingle(pcs * 0.55M);
...



There's all sorts of directions you can approach getting the result in the type you want. Each with there own overflow and accuracy issues.

This post has been edited by lordofduct: 30 April 2012 - 10:16 AM

Was This Post Helpful? 1
  • +
  • -

#3 jbarcus81  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 30-April 12

Re: Error message with decimal to float

Posted 30 April 2012 - 10:20 AM

Perfect!!! It's always something small that I miss... But now I am not getting my summary to calculate... specifically block:
private void btnSummary_Click(object sender, EventArgs e)
        {
            lblTotalPcs.Text = string.Format("{0}", totalPcs);
            lblTotalWages.Text = string.Format("${0:f2}", totalWages);
            lblAvgPay.Text = string.Format("${0:f2}", totalWages / numEmp);
            gbxSummary.Visible = true;

        }


I don't get an error.. just no output..
Was This Post Helpful? 0
  • +
  • -

#4 jbarcus81  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 30-April 12

Re: Error message with decimal to float

Posted 30 April 2012 - 10:28 AM

Actually wait.. I think I'm missing something now... I have declared
float avgPay = 0;
but my compiler says it's being used... Wait a second, where did it go? I don't think I removed it..
Was This Post Helpful? 0
  • +
  • -

#5 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2533
  • View blog
  • Posts: 4,633
  • Joined: 24-September 10

Re: Error message with decimal to float

Posted 30 April 2012 - 01:22 PM

I'm unsure what you're getting at. What I can say though is that you don't really ever use the value 'avgPay' for anything. You create it, you set it to 0 in one place, and that's it. You don't use it in the Summary method... I see a lblAvgPay, but that's not avgPay.
Was This Post Helpful? 0
  • +
  • -

#6 jbarcus81  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 30-April 12

Re: Error message with decimal to float

Posted 30 April 2012 - 01:31 PM

View Postlordofduct, on 30 April 2012 - 01:22 PM, said:

I'm unsure what you're getting at. What I can say though is that you don't really ever use the value 'avgPay' for anything. You create it, you set it to 0 in one place, and that's it. You don't use it in the Summary method... I see a lblAvgPay, but that's not avgPay.


I see.. that makes sense.. but with reguards to the Summary .. I click the button and nothing calculates.. no errors, just no calculation ... Or at least no output.. maybe seeing the GUI will help?? Here is a screen shot of the section I am talking about ...

Posted Image
Was This Post Helpful? 0
  • +
  • -

#7 jbarcus81  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 30-April 12

Re: Error message with decimal to float

Posted 30 April 2012 - 01:37 PM

And also.. the previous image is in design mode obviously .. here is what it looks like when running.. When the Summary button is clicked it should display a Summary of the sales and pay in the box.. but I get no response from the button ...



Posted Image


And this is how it SHOULD look .. BUT, this is in VB that someone did so I could see it properly..


Posted Image
Was This Post Helpful? 0
  • +
  • -

#8 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2533
  • View blog
  • Posts: 4,633
  • Joined: 24-September 10

Re: Error message with decimal to float

Posted 30 April 2012 - 02:17 PM

Is the event actually firing?

Put a 'breakpoint' in the event handler and make sure it's actually running... there's a chance the designer removed the code that adds the method to the event.





(personally it's one thing that bugs me about Visual Studio and its handling of C# and events in WinForms. They hide the actual code in the designer file and it's hard to tell if it's connected or not. For instance, double click on a button in the designer, and it'll create a click event handler for you. Now delete it, because say you accidently did this. And watch the error show up saying it can't find the function you just deleted. VB.Net and its 'Handles' keyword is a god-sent in this case)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1