简体   繁体   English

C#; 并非所有代码路径都返回值

[英]C#; Not all code paths return a value

I have been looking for a post that can actually help me figure this out, the thing is I am very new, as in within my first 30 days of seriously learning to code. 我一直在寻找可以实际上帮助我弄清楚这个问题的帖子,因为我很新,就像在认真学习编码的前30天内一样。 Here is my code; 这是我的代码;

class Program
{
    static void Main(string[] args)
    {
        string name;             //Employee Name added to nameList
        double payRate;          //Employee Pay Rate added to rateList
        double hoursWorked;      //Integer associated with how many hours the employee worked, multiplied by payRate
        bool addMore;

        List<string> nameList = new List<string>();
        List<double> rateList = new List<double>();
        List<double> hWorkedList = new List<double>();
    }

    public static object PayRollMethod(string name, double payRate, double hoursWorked, bool addMore, List<string> nameList, List<double> rateList, List<double> hWorkedList)
    {
        Console.Write("Enter employee name: ");
        name = Console.ReadLine();
        nameList.Add(name);      //adds the name entered to the namesList

        Console.Write("What is the pay rate for this employee: ");
        payRate = Convert.ToDouble(Console.ReadLine());
        rateList.Add(payRate);  //adds the payRate to the rateList, as a double

        Console.Write("How many hours did the employee work? ");
        hoursWorked = Convert.ToDouble(Console.ReadLine());
        hWorkedList.Add(hoursWorked);

        Console.Write("Do you need to add any other employees? Y or N");
        addMore = Convert.ToBoolean(Console.ReadLine());

        Console.ReadKey();  //holds the console open after the program finishes

    }


}

When I try and run it I get an error with the PayRollMethod giving me the error listed. 当我尝试运行它时,PayRollMethod出现错误,并给我列出的错误。

I think that the issue is the boolean at the bottom (addMore), I have that as it will signal the program to either restart the user input section, or prompt the user to either exit or edit an entry. 我认为问题是底部的布尔值(addMore),我认为这是因为它将向程序发出信号,要求重新启动用户输入部分,或提示用户退出或编辑条目。 I am still trying to figure out the best way to implement the loop, so I haven't typed up that part yet. 我仍在尝试找出实现循环的最佳方法,因此我还没有输入该部分。

Thanks in advance for the help. 先谢谢您的帮助。

You declared PayRollMethod with an object return type, that means you must return an object instance before you leave the method. 您使用object返回类型声明了PayRollMethod ,这意味着您必须在离开方法之前返回对象实例。 If you don't want to return any value, then you can declare the method with void return type. 如果您不想返回任何值,则可以使用void return type声明该方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM