C# 带有字符串开关的类

C# 带有字符串开关的类,c#,C#,我得到错误“并非所有代码路径都返回值”。它来自myFunction(字符串值)。我不知道如何返回这个,或者在参数中输入什么来让它工作。我也需要下面的东西吗?我是新来上课的。请告诉我我是否做得都错了,或者这是switch语句应该做的地方 class AddItemOption { //Fields that contain class data string input = Console.ReadLine(); //Methods that define class f

我得到错误“并非所有代码路径都返回值”。它来自myFunction(字符串值)。我不知道如何返回这个,或者在参数中输入什么来让它工作。我也需要下面的东西吗?我是新来上课的。请告诉我我是否做得都错了,或者这是switch语句应该做的地方

 class AddItemOption
{
    //Fields that contain class data
    string input = Console.ReadLine(); 
   //Methods that define class functionality
    public string myFunction(string Value)
    {
        switch (input)
        {
            case "Umbrella":
               Console.WriteLine("Umbrella is selected");
               break;
            case "Rain Coat":
               Console.WriteLine("Raincoat is selected");
               break;
            case "Boots":
                Console.WriteLine("Boots is selected"); 
                break;
            case "Hood":
                Console.WriteLine("Hood is selected");
                break;
            default:
                Console.WriteLine("Input not reconized, please choose another item");
                break;


        }

    }
从Shyju我将其更改为:

   public AddItemOption(string input) 

        {


        }

但是,它无法识别中断。“检测到无法访问的代码”

您的方法的返回类型为string。这意味着您的方法应该总是返回一些字符串。但您没有返回字符串。而是使用
WriteLine
方法将其写入控制台

所以改变

class AddItemOptionOne
{
    //Fields that contain class data
    string input = Console.ReadLine(); 
   //Methods that define class functionality
    public string myFunction(string Value)
    {
        switch (input)
        {
            case "Key Chain":
               return "Key Chain is selected";
               break;
            case "Leather Armor":
               return "Leather Armor is selected";
               break;
            case "Boots":
                return "Boots is selected"; 
                break;
            case "Sword":
                return "Sword is selected";
                break;
            default:
                return "Input not reconized, please choose another item";
                break;


        }
    }

您可以将方法的返回类型更改为
void

return "Umbrella is selected";
编辑:根据问题编辑

return
语句将从方法中取出控件。这意味着下一行(
break
)将不会执行。所以删除break语句

 public void myFunction(string Value)
 {
   //your current fancy stuff here
   Console.WriteLine("My method dont have a return type");
 }

您的方法的返回类型为
string
。这意味着您的方法应该总是返回一些字符串。但您没有返回字符串。而是使用
WriteLine
方法将其写入控制台

所以改变

class AddItemOptionOne
{
    //Fields that contain class data
    string input = Console.ReadLine(); 
   //Methods that define class functionality
    public string myFunction(string Value)
    {
        switch (input)
        {
            case "Key Chain":
               return "Key Chain is selected";
               break;
            case "Leather Armor":
               return "Leather Armor is selected";
               break;
            case "Boots":
                return "Boots is selected"; 
                break;
            case "Sword":
                return "Sword is selected";
                break;
            default:
                return "Input not reconized, please choose another item";
                break;


        }
    }

您可以将方法的返回类型更改为
void

return "Umbrella is selected";
编辑:根据问题编辑

return
语句将从方法中取出控件。这意味着下一行(
break
)将不会执行。所以删除break语句

 public void myFunction(string Value)
 {
   //your current fancy stuff here
   Console.WriteLine("My method dont have a return type");
 }

嗯。。。函数中没有返回任何内容,因此将其更改为

 case "Key Chain":
           return "Key Chain is selected";

嗯。。。函数中没有返回任何内容,因此将其更改为

 case "Key Chain":
           return "Key Chain is selected";
试试这个:

public void myFunction(string Value)
试试这个:

public void myFunction(string Value)


或者,如果您不希望函数输出,请将签名更改为void,这也应该有效。@QuintinRobinson:也添加了该项。@Shyju它消除了错误,但现在我有了“无法访问的代码检测到错误”@Sarah:您的参数名是
,您正在开关中使用
输入
statement@Shyju对每一个案件都进行审查是不好的做法。如果可能,一个方法应该返回一次。或者,如果您不希望函数输出,请将签名更改为void,这也应该有效。@QuintinRobinson:也添加了它。@Shyju它消除了错误,但现在我有了“无法访问的代码检测到错误”@Sarah:您的参数名是
,您正在开关中使用
输入
statement@Shyju对每一个案件都进行审查是不好的做法。如果可能,一个方法应该返回一次。另外,一个有效的选项,取决于您尝试执行的操作。我昨天听说过void,我希望它返回一些内容。另外,一个有效的选项,取决于您尝试执行的操作。我昨天听说过void,我希望它返回一些内容。旁注:
Console.ReadLine()字段定义中?这是一种非常不寻常的初始化字段的方法。。。近乎“你真的能做到吗?”。。。考虑更直接的初始化(即在“RealIdPotoF控制台”方法中,或者将值传递给构造函数。边注:<代码>控制台。Read Load();字段定义中的这是一种非常不寻常的初始化字段的方法……与“你真的能做到吗?”……考虑更直接的初始化(也就是说,在“RealIdPoNoTo控制台”)中。方法或将值传递给构造函数。尽可能采用+1方法…我不会保持奇怪的
string input=Console.ReadLine()
行回答…是的,那是一个复制粘贴。根据建议更新。我从来都不知道你可以这样做,知道会很有用。谢谢你的帮助。+1尽可能的方法…我不会保持奇怪的
string input=Console.ReadLine()
回答中的一行…是的,那是一份拷贝和粘贴。根据建议更新。我从来不知道你能做到这一点,知道这一点会很有用。谢谢你的帮助。