[英]Method must have return type (C#)
所以我一直在用C#跟随这本书。
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);
}
但是我收到一个错误:方法必须有返回类型。 指的是“PrintAccount(RobAccount);”
我知道之前已经问过这个问题,但没有一个问题与我的问题类似。
问题是编译器认为是PrintAccount(RobsAccount);
是一个方法定义,这就是为什么需要返回类型。
你必须在另一个方法中调用该方法,你不能在void上调用它。
问题1:您的方法调用直接在类中。 它必须在一个方法内。 我们在类中声明方法,而不是调用它们。
问题2:在静态方法中调用静态方法,因此PrintAccount
方法也应该是静态的。
解决方案:这应该是类结构。
//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
}
}
PrintAccount(RobsAccount);
你什么时候期望完全调用这个函数? 你已经把它放在了类定义的中间(这就是你得到错误的原因)。 你期望在程序启动时调用它吗? 创建类的第一个实例时? 它没有意义; 类是对象的模板。
如果要调用方法,则必须使用其他方法(暂时不进行静态初始化)。 所以,删除该行,然后在主...
static void Main(...)
{
PrintAccount();
}
另请注意,您的设计非常奇怪。 你已经定义了Bankprogram
类的主要内部(为什么?), 一切都是静态的。 您是否只打算允许一个帐户,其中所述帐户的每个属性都是硬编码的? 似乎没有用。
首先,您尝试在任何其他方法之外调用PrintAccount()
方法! 这似乎是一个错字。
其次, 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);
}
}
可能是拼写错误。 不知道你的书还是书。
对PrintAccount
的调用应该在PrintAccount
类的方法中进行。 如上所述,调用在任何方法之外,这是一个错误。
更多,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);
}
}
问题是你在类中有代码,不在方法中。
将该行移动到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);
}
}
旁注:代码使用struct
作为数据对象,其中一个class
是合适的。
这里有两件事是错的;
尝试更改以下两件事:
您的代码将如下所示:
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);
}
祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.