0

So I'm trying to return a decimal value for a total cost of a bill into a textbox from a class method.

this class method is as shown:

public decimal calcCost(decimal totalCost, bool specialistRepair, decimal labourHours,      decimal standardRate, decimal partSaleCost)
    {

        foreach (Job j in m_jobs)
        {
            if (specialistRepair == true)
            {
                totalCost = totalCost + 45;
            }

            totalCost = totalCost + partSaleCost + (labourHours * standardRate);
        }

        return totalCost;
    }

the problem I'm having is that it's saying 'Cannot implicitly comvert type 'decimal to 'string'.

I have tried parsing the whole line after the '=' sign but that's not possible.

txtTotal.Text = //Here// Program.AuspexDo.calcCost(0, checkSpecialist, jobHours, standardRate, partSaleCost); 

The code for main is shown here:

      bool checkSpecialist = true;
        if (chkSpecialistRepairs.Checked == true)
        {
            checkSpecialist = true;

        }

        decimal jobHours = decimal.Parse(txtHours.Text); 
        decimal standardRate = Program.AuspexDo.StandardRate;
        decimal partSaleCost = 5; //to be configured



        txtTotal.Text = Program.AuspexDo.calcCost(0, checkSpecialist, jobHours, standardRate, partSaleCost); 

If anyone could help it would be greatly appreciated. Thanks.

2
  • I have 'Cannot implicitly comvert type 'decimal to 'string'.' I'll make it clearer Commented Oct 22, 2012 at 13:51
  • I would pass "m_jobs" to the method and not modify a parameter "totalCost" and return that modified value but rather use a local decimal for the the calculation - but the .ToString() is the real issue (you might need to format the decimal as well. Commented Oct 22, 2012 at 14:06

2 Answers 2

2

Change it to

txtTotal.Text = Program.AuspexDo.calcCost(0, checkSpecialist, jobHours, standardRate, partSaleCost).ToString();  
Sign up to request clarification or add additional context in comments.

1 Comment

Oh wow, I wouldn't have thought to have done that, it's not showing an error, I'll check it does what I want it too.
1

calcCost returns a decimal and you are trying to assign the return value to txtTotal.Text, which is of type string.

As the error says, there is no conversion between the two - you need to convert the return value to a string first - the way to do that is to call ToString() on the return value:

txtTotal.Text = Program.AuspexDo.calcCost(0, 
                                          checkSpecialist, 
                                          jobHours, 
                                          standardRate, 
                                          partSaleCost)
                                          .ToString();

Take a look at the custom and standard numeric format strings, as well as the information about composite format strings, as you may want to output to a specific format.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.