Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method must have return type (C#)

Tags:

methods

c#

return

so I've been following along C# with this book.

http://www.robmiles.com/c-yellow-book/Rob%20Miles%20CSharp%20Yellow%20Book%202011.pdf on page 81-82 I get this code from there and add another method from page 82 resulting in:

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

I know this question has been asked before but none of them looked similar to my problem.

like image 497
cookiepuss Avatar asked Dec 20 '25 11:12

cookiepuss


1 Answers

The problem is that the compiler thinks that PrintAccount(RobsAccount); is a method definition and that's why is requiring for a return type.

You have to call that method inside another method, you can't call it on the void.

like image 179
Claudio Redi Avatar answered Dec 22 '25 23:12

Claudio Redi