我正在练习构建一个虚拟应用程序,我试图将一个枚举作为参数传入,并有一个整数输出参数用于设置成员变量 ( BillsOwed
)。我有两个具体问题:为什么它ComputeRetirementBenefitCost
告诉我我的变量must be assigned to before control leaves the current method
以及为什么ComputeRetirementBenefitCost
在我在评论中标记的地方无法访问?欢迎任何和所有审慎的未来设计建议:)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GivePromotions
{
//Different Employees in the EmpType enum extend this class
public abstract class Employee
{
public int EmployeeId { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public DateTime DateHired { get; set; }
public RetirementPackage.RetirementPackageType PackageType { get; set; }
public EmpType Employeetype { get; set; }
public int NetSalary { get; set; }
//I'd like to set the output of ComputeRetirementBenefitCost
//in the member below but Intellisense doesn't pick it up
//on two lines for space, commented because it doesn't work
//int billsOwed = Employee.RetirementPackage
//.ComputeRetirementBenefitCost(Employee.EmpType,out benefitCost)
public virtual void DisplayInformation()
{
Console.WriteLine("Employee information: ");
Console.WriteLine("EmployeeId = {0} ", EmployeeId);
Console.WriteLine("Last name = {0} ", LastName);
Console.WriteLine("First name = {0} ", FirstName);
Console.WriteLine("Date hired = {0} ", DateHired);
Console.WriteLine("Salary = {0} ", NetSalary);
}
public enum EmpType
{
Janitor,
President,
Manager
}
//an Employee 'has-a' Retirement package
public class RetirementPackage
{
public enum RetirementPackageType
{
Basic,
Gold,
Silver,
Platinum,
Black
}
//method of choice here
//ERROR: Output parameter 'benefitCost' must be assigned to before control
//leaves the current method
public void ComputeRetirementBenefitCost(Employee.EmpType e, out int benefitCost)
{
switch (e)
{
case Employee.EmpType.President:
benefitCost = 100000;
break;
case Employee.EmpType.Manager:
benefitCost = 5000;
break;
case Employee.EmpType.Janitor:
benefitCost = 1000;
break;
}
}
}
}
}