2

我正在练习构建一个虚拟应用程序,我试图将一个枚举作为参数传入,并有一个整数输出参数用于设置成员变量 ( 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; 



                }


            }

        }
    }


}
4

2 回答 2

3
  • 错误:必须在控制之前分配输出参数“benefitCost”:

您必须为您的开关添加一个“默认值:”或在您的开关之外设置一个值,因为这可能是您的三种情况都没有被击中。

  • 以及为什么 ComputeRetirementBenefitCost 不可访问

修复第一个将修复这个。使用 crtl+shif+B 构建和读取错误列表视图中的错误和警告。通常他们会准确地告诉你哪里出了问题。

于 2013-04-11T16:56:14.923 回答
1

在该方法ComputeRetirementBenefitCost中,您似乎处理了所有可能的代码流,因为enum Employee.EmpType具有三个不同的定义,并且您涵盖了所有三个定义。但是这是错误的。来自 MSDN枚举类型(C# 编程指南)

可以将任意整数值分配给 meetingDay。例如,这行代码不会产生错误: meetingDay = (Days) 42。

在你的情况下,我可以这样调用你的方法

int benefitCost = 0;
ComputeRetirementBenefitCost( (Employee.EmpType) 500, out benefitCost);

在这种情况下,我将脱离您的代码流,因此不会初始化 benefitCost。您可以像这样更改实现:

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; 
       default:
           throw new ArgumentOutOfRangeException("e");
    }
}

第二个问题 - 看起来您正在尝试ComputeRetirementBenefitCost作为静态方法调用,但它不是静态的,所以只需static为其添加一个关键字。

于 2013-04-11T17:08:28.853 回答