onzymorg
onzymorg

Reputation: 19

C# Delegates. Casting not working for some reason

2 Errors appear when I try to build this code. First one: "Argument 2 : cannot convert from double to int." Second one: "The best overloaded method for CalculatePay(double, int, Calculate) has some invalid arguments. I don't understand why the casting I've applied in the BankHolidayShift and NormalShift methods isn't working. Thanks.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication8
{

    public delegate int Calculate(double val1, int val2);

    class PayCalculator
    {

        static double HourlyPay = 10;
        static int HoursPerShift = 8;

        static int NormalShift(double HourlyPay, int HoursPerShift)
        {
            return (int) HourlyPay * HoursPerShift;
        }

        static int BankHolidayShift(double HourlyPay, int HoursPerShift)
        {
            return (int)(HourlyPay * HoursPerShift) + 50;
        }

        public static int CalculatePay(double a, int b, Calculate calc)
        {

            int TotalPay = calc(a, b);
            return TotalPay;
        }

        static void Main()
        {
            Calculate calc = new Calculate(BankHolidayShift);

            int TotalPay =  CalculatePay(HourlyPay, HourlyPay, calc);
            Console.WriteLine("Total Pay for this shift is : {0}", TotalPay);
            Console.ReadLine();
        }


    }
}

Upvotes: 1

Views: 43

Answers (1)

Alex Sikilinda
Alex Sikilinda

Reputation: 3013

You have int TotalPay = CalculatePay(HourlyPay, HourlyPay, calc);, obviously it's a spelling, and you should have:

int TotalPay =  CalculatePay(HourlyPay, HoursPerShift, calc);

BTW, local variables, as well as methods parameters, should be CamelCased.

Upvotes: 1

Related Questions