gpt4 book ai didi

c# - 并非所有代码路径都在方法中返回带有输出参数的值

转载 作者:行者123 更新时间:2023-11-30 20:58:38 24 4
gpt4 key购买 nike

我正在练习构建一个虚拟应用程序,我正在尝试将枚举作为参数传入,并具有一个整数输出参数,该参数将用于设置成员变量 (BillsOwed) .我有两个具体问题:为什么 ComputeRetirementBenefitCost 告诉我必须在控制离开当前方法之前分配我的变量 以及为什么不是 ComputeRetirementBenefitCost 可访问我在评论中标记的位置?欢迎任何和所有审慎的 future 设计建议:)

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;



}


}

}
}


}

最佳答案

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

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

  • 为什么无法访问 ComputeRetirementBenefitCost

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

关于c# - 并非所有代码路径都在方法中返回带有输出参数的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15954518/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com