簡體   English   中英

C#輸入系統-由於'{method}'返回void,因此return關鍵字后不能包含對象表達式

[英]C# Input system - Since '{method}' returns void, a return keyword must not be followed by an object expression

在這里嘗試進行一些高級文本冒險時,我有一個清單類。 (這不是錯誤),並且一切正常! 我正在嘗試實現輸入功能。 它只是導致輸入,然后將參數返回給該類。 我以為會很容易。 原來,“無效”方法無法返回任何內容。 那我不知道該用什么。
我在Google上進行了一些搜索,但找不到google,這里的答案都是XML或更有經驗的程序員。 還有一些更簡單的方法,但是沒有答案。

這是我的程序課

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Inventory_system_test
{
    class Program
    {
        //Objects
        static private Inventory inv = new Inventory();

        //strings
        static private string args;
        //variables



        static void Main(string[] args)
        {
            Write("Do you want to kill dave?");
            input();
        }

        static public void input()
        {

            bool done = false;

            Writen("Enter a command: ");
            args = Console.ReadLine();
            while (!done)
            {
                if (args.Contains("add inv "))
                {
                    args = args.Split()[2];
                    inv.additem(args);

                }
                else if (args.Contains("remove inv "))
                {
                    args = args.Split()[2];
                    inv.removeitem(args);

                }
                else if (args.Contains("see inv"))
                {
                    Write("INVENTORY:");
                    inv.getinv();
                }
                else if (args == "close")
                {
                    Environment.Exit(0);
                }
                else
                {
                    done = true;
                    return args; ///**Here is the error ofcourse.**
                }

            }
        } //Input files things :)


        #region Easy Commands (Write, Sleep)
        //Write to console
        public static void Write(string writev)
        {
            Console.WriteLine(writev);
        }

        //Sleep for 'int sleeptime' in milliseconds
        public static void Sleep(int sleeptime)
        {
            System.Threading.Thread.Sleep(sleeptime);
        }

        public static void Writen(string writen)
        {
            Console.Write(writen);
        }
        #endregion
    }
}

我越來越了解腳本,這只是通過提問和搜索Google來實現,我真的很喜歡Stackoverflow上的人! 謝謝大家的幫助!

恩..我該怎么做呢? 方法不多..我也不知道該怎么辦。

原來,“無效”方法無法返回任何內容。 那我不知道該用什么。

您應該使用一種聲明為要返回的信息種類的方法! 當方法為void ,這具體意味着它不意味着返回任何內容。

在這種情況下,您似乎正在嘗試返回args的值(這是一個string變量),因此您需要:

public static string input()

另外:

  • 您應該遵循.NET命名約定
  • 沒有必要讓args變量為靜態-最好將其作為方法中的局部變量
  • 在我看來, args始終是此變量的一個奇怪名稱。 考慮到您要輸入命令,為什么不使用command作為變量名呢?

我建議您閱讀有關方法MSDN頁面,或者看一本有關C#的好書,以了解有關返回類型,參數等的更多信息。

void (C# Reference)‎

當用作方法的返回類型時,void指定該方法不返回值。

但是您的input法返回的值是..

Console.ReadLine()方法返回一個string因此您的args看起來像一個string 這就是為什么您應該將返回類型更改為類似string

public static string input()
{

}

您將args聲明為string類型,因此應返回以下內容:

static public string input()
{
    ...
    return args;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM