方法必须有返回类型 (C#)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11993964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Method must have return type (C#)
提问by cookiepuss
so I've been following along C# with this book.
所以我一直在关注这本书的 C#。
http://www.robmiles.com/c-yellow-book/Rob%20Miles%20CSharp%20Yellow%20Book%202011.pdfon page 81-82 I get this code from there and add another method from page 82 resulting in:
http://www.robmiles.com/c-yellow-book/Rob%20Miles%20CSharp%20Yellow%20Book%202011.pdf第 81-82 页我从那里获取此代码并从第 82 页添加另一种方法导致:
using System;
enum AccountState
{
New,
Active,
UnderAudit,
Frozen,
Closed
};
struct Account
{
public AccountState State;
public string Name;
public string Address;
public int AccountNumber;
public int Balance;
public int Overdraft;
};
class Bankprogram
{
public static void Main()
{
Account RobsAccount;
RobsAccount.State = AccountState.Active;
RobsAccount.Name = "Rob Miles";
RobsAccount.AccountNumber = 1234;
RobsAccount.Address = "his home";
RobsAccount.Balance = 0;
RobsAccount.Overdraft = -1;
Console.WriteLine("name is " + RobsAccount.Name);
Console.WriteLine("balance is : " + RobsAccount.Balance );
}
public void PrintAccount(Account a)
{
Console.WriteLine ("Name" + a.Name);
Console.WriteLine ("Address :" + a.Address);
Console.WriteLine ("Balance:" + a.Balance);
}
PrintAccount(RobsAccount);
}
but I get an error: Method Must have return type. referring to the "PrintAccount(RobAccount);"
但我收到一个错误:方法必须有返回类型。指的是“PrintAccount(RobAccount);”
I know this question has been asked before but none of them looked similar to my problem.
我知道以前有人问过这个问题,但没有一个看起来与我的问题相似。
回答by Sergey Brunov
First of all, you are trying to call PrintAccount()method outside any other method! It seems it is a typo.
首先,您试图PrintAccount()在任何其他方法之外调用方法!它似乎是一个错字。
Second, PrintAccount()method must be static (it belongs to the Bankprogramclass) which is called from static Main()method.
其次,PrintAccount()方法必须Bankprogram是从静态Main()方法调用的静态(它属于类)。
class Bankprogram
{
public static void Main()
{
Account RobsAccount;
RobsAccount.State = AccountState.Active;
RobsAccount.Name = "Rob Miles";
RobsAccount.AccountNumber = 1234;
RobsAccount.Address = "his home";
RobsAccount.Balance = 0;
RobsAccount.Overdraft = -1;
Console.WriteLine("name is " + RobsAccount.Name);
Console.WriteLine("balance is : " + RobsAccount.Balance );
PrintAccount(RobsAccount); // This line has been moved.
}
// This method has become STATIC!
public static void PrintAccount(Account a)
{
Console.WriteLine ("Name" + a.Name);
Console.WriteLine ("Address :" + a.Address);
Console.WriteLine ("Balance:" + a.Balance);
}
}
回答by Claudio Redi
The problem is that the compiler thinks that PrintAccount(RobsAccount);is a method definition and that's why is requiring for a return type.
问题是编译器认为这PrintAccount(RobsAccount);是一个方法定义,这就是为什么需要返回类型。
You have to call that method inside another method, you can't call it on the void.
您必须在另一个方法中调用该方法,不能在 void 上调用它。
回答by Nikhil Agrawal
There are 2 problems
有2个问题
Problem 1:Your method calling is directly inside a class. It has to be inside a method. We declare methods in class, not call them.
问题 1:您的方法调用直接在类中。它必须在方法内部。我们在类中声明方法,而不是调用它们。
Problem 2:Static methods are called inside static methods so your PrintAccountmethod should also be static.
问题 2:在静态方法内部调用静态方法,因此您的PrintAccount方法也应该是静态的。
Solution:This should be the class structure.
解决方案:这应该是类结构。
//Previous code as it is
class Bankprogram
{
public static void Main()
{
//Previous code as it is
PrintAccount(RobsAccount);
}
public static void PrintAccount(Account a)
{
//Method code as it is
}
}
回答by Steve
Probably is a typo. Don't know if yours or in the book.
大概是笔误。不知道是你的还是书上的。
The call to PrintAccountshould be made inside a method of the class BankProgram.
As written, the call is outside from any method and this is an error.
More, the PrintAccount method, if called from inside the Main, should be also static because to call an instance member method you need to create an instance of the BankProgram class
要将呼叫PrintAccount应类BankProgram的方法中进行。正如所写,调用是在任何方法之外,这是一个错误。
此外,PrintAccount 方法,如果从 Main 内部调用,也应该是静态的,因为要调用实例成员方法,您需要创建 BankProgram 类的实例
class Bankprogram
{
public static void Main()
{
Account RobsAccount;
RobsAccount.State = AccountState.Active;
RobsAccount.Name = "Rob Miles";
RobsAccount.AccountNumber = 1234;
RobsAccount.Address = "his home";
RobsAccount.Balance = 0;
RobsAccount.Overdraft = -1;
Console.WriteLine("name is " + RobsAccount.Name);
Console.WriteLine("balance is : " + RobsAccount.Balance );
PrintAccount(RobsAccount);
// OR - if you really want to keep NON STATIC the method PrintAccount
// BankProgram bp = new BankProgram();
// bp.PrintAccount(RobsAccount);
}
public static void PrintAccount(Account a)
{
Console.WriteLine ("Name" + a.Name);
Console.WriteLine ("Address :" + a.Address);
Console.WriteLine ("Balance:" + a.Balance);
}
}
回答by Guffa
The problem is that you have code in the class, that isn't within a method.
问题是您在类中有代码,而不是在方法中。
Move that line into the Mainmethod:
将该行移动到Main方法中:
using System;
enum AccountState
{
New,
Active,
UnderAudit,
Frozen,
Closed
};
struct Account
{
public AccountState State;
public string Name;
public string Address;
public int AccountNumber;
public int Balance;
public int Overdraft;
};
class Bankprogram
{
public static void Main()
{
Account RobsAccount;
RobsAccount.State = AccountState.Active;
RobsAccount.Name = "Rob Miles";
RobsAccount.AccountNumber = 1234;
RobsAccount.Address = "his home";
RobsAccount.Balance = 0;
RobsAccount.Overdraft = -1;
Console.WriteLine("name is " + RobsAccount.Name);
Console.WriteLine("balance is : " + RobsAccount.Balance );
PrintAccount(RobsAccount);
}
public void PrintAccount(Account a)
{
Console.WriteLine ("Name" + a.Name);
Console.WriteLine ("Address :" + a.Address);
Console.WriteLine ("Balance:" + a.Balance);
}
}
Side note: The code is using a structfor a data object, where a classwould be appropriate.
旁注:代码使用 astruct作为数据对象,其中 aclass是合适的。
回答by Ed S.
PrintAccount(RobsAccount);
When do you expect that function to be called exactly? You have plopped it in the middle of a class definition (which is why you get the error). Do you expect it to be called when the program starts? When the first instance of your class is created? It doesn't make sense; classes are templates for objects.
您预计该函数何时被准确调用?您已将它放在类定义的中间(这就是您收到错误的原因)。你希望它在程序启动时被调用吗?你的类的第一个实例是什么时候创建的?这没有意义;类是对象的模板。
If you want to call a method you must do so from another method (static initialization aside for now). So, remove that line, and then in main...
如果你想调用一个方法,你必须从另一个方法调用(现在静态初始化除外)。因此,删除该行,然后在主...
static void Main(...)
{
PrintAccount();
}
Also note that your design is very strange. You have defined main inside of your Bankprogramclass (why?) and everythingis static. Do you only intend to allow for one account where every property of said account is hard-coded? Doesn't seem very useful.
另请注意,您的设计很奇怪。您已经在Bankprogram类中定义了 main (为什么?)并且一切都是静态的。您是否只打算允许一个帐户,其中该帐户的每个属性都进行了硬编码?似乎不是很有用。
回答by Chris Beemster
There's two things wrong here;
这里有两件事不对;
- You are trying to call your PrintAccount method from the class, not from any method. That will obviously not work.
- Your PrintAccount method is not a static method, thus can only be reached when your class is instantiated. That means that you will not be able to call it from your main function.
- 您正在尝试从类中调用 PrintAccount 方法,而不是从任何方法中调用。那显然行不通。
- 您的 PrintAccount 方法不是静态方法,因此只能在您的类被实例化时访问。这意味着您将无法从主函数中调用它。
Try to change the following two things:
尝试改变以下两点:
- Make your PrintAccount a static method
- Call the PrintAccount method from your main method.
- 使您的 PrintAccount 成为静态方法
- 从您的主方法调用 PrintAccount 方法。
Your code will then look like this:
您的代码将如下所示:
public static void Main()
{
Account RobsAccount;
RobsAccount.State = AccountState.Active;
RobsAccount.Name = "Rob Miles";
RobsAccount.AccountNumber = 1234;
RobsAccount.Address = "his home";
RobsAccount.Balance = 0;
RobsAccount.Overdraft = -1;
Console.WriteLine("name is " + RobsAccount.Name);
Console.WriteLine("balance is : " + RobsAccount.Balance );
PrintAccount(RobsAccount);
}
public static void PrintAccount(Account a)
{
Console.WriteLine ("Name" + a.Name);
Console.WriteLine ("Address :" + a.Address);
Console.WriteLine ("Balance:" + a.Balance);
}
Good luck!
祝你好运!

