方法必须有返回类型(C#)

时间:2012-08-16 19:15:45

标签: c# methods return

所以我一直在用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);”

我知道之前已经问过这个问题,但没有一个问题与我的问题相似。

7 个答案:

答案 0 :(得分:8)

问题是编译器认为PrintAccount(RobsAccount);是一个方法定义,这就是为什么需要返回类型。

你必须在另一个方法中调用该方法,你不能在void上调用它。

答案 1 :(得分:4)

有2个问题

问题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
    }  
}

答案 2 :(得分:1)

PrintAccount(RobsAccount);

您何时期望完全调用该函数?你已经把它放在了类定义的中间(这就是你得到错误的原因)。你期望在程序启动时调用它吗?创建类的第一个实例时?它没有意义;类是对象的模板。

如果要调用方法,则必须使用其他方法(暂时不进行静态初始化)。所以,删除该行,然后在主...

static void Main(...) 
{
    PrintAccount();
}

另请注意,您的设计非常奇怪。你已经定义了Bankprogram类的主要内部(为什么?)和所有都是静态的。您是否只打算允许一个帐户,其中所述帐户的每个属性都是硬编码的?似乎没什么用处。

答案 3 :(得分:0)

首先,您尝试在任何其他方法之外调用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);
    }
}

答案 4 :(得分:0)

可能是拼写错误。不知道你的是否或书中。

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); 
    } 
} 

答案 5 :(得分:0)

问题是你在类中有代码,这不在方法中。

将该行移至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是合适的。

答案 6 :(得分:0)

这里有两件事是错的;

  • 您正在尝试从类中调用PrintAccount方法,而不是从任何方法调用。这显然不会奏效。
  • 您的PrintAccount方法不是静态方法,因此只能在实例化类时才能访问。这意味着您将无法从主功能中调用它。

尝试更改以下两项内容:

  • 将PrintAccount设为静态方法
  • 从主方法调用PrintAccount方法。

您的代码将如下所示:

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);
}
祝你好运!