C# 代表。铸造由于某种原因不工作
C# Delegates. Casting not working for some reason
2 当我尝试构建此代码时出现错误。
第一个:"Argument 2 : cannot convert from double to int."
第二个:“CalculatePay(double, int, Calculate) 的最佳重载方法有一些无效参数。
我不明白为什么我在 BankHolidayShift 和 NormalShift 方法中应用的转换不起作用。谢谢
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();
}
}
}
你有int TotalPay = CalculatePay(HourlyPay, HourlyPay, calc);
,显然是拼写,你应该有:
int TotalPay = CalculatePay(HourlyPay, HoursPerShift, calc);
顺便说一句,局部变量,以及方法参数,应该是 CamelCased。
2 当我尝试构建此代码时出现错误。 第一个:"Argument 2 : cannot convert from double to int." 第二个:“CalculatePay(double, int, Calculate) 的最佳重载方法有一些无效参数。 我不明白为什么我在 BankHolidayShift 和 NormalShift 方法中应用的转换不起作用。谢谢
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();
}
}
}
你有int TotalPay = CalculatePay(HourlyPay, HourlyPay, calc);
,显然是拼写,你应该有:
int TotalPay = CalculatePay(HourlyPay, HoursPerShift, calc);
顺便说一句,局部变量,以及方法参数,应该是 CamelCased。