Reputation: 33
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.
Upvotes: 2
Views: 36459
Reputation: 216353
Probably is a typo. Don't know if yours or in the book.
The call to PrintAccount
should 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
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);
}
}
Upvotes: 0
Reputation: 48600
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.
Problem 2: Static methods are called inside static methods so your PrintAccount
method should also be static.
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
}
}
Upvotes: 4
Reputation: 362
There's two things wrong here;
Try to change the following two things:
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!
Upvotes: 0
Reputation: 68440
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.
Upvotes: 8
Reputation: 124790
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 Bankprogram
class (why?) and everything is static. Do you only intend to allow for one account where every property of said account is hard-coded? Doesn't seem very useful.
Upvotes: 1
Reputation: 700800
The problem is that you have code in the class, that isn't within a method.
Move that line into the Main
method:
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 struct
for a data object, where a class
would be appropriate.
Upvotes: 0
Reputation: 18126
First of all, you are trying to call PrintAccount()
method outside any other method! It seems it is a typo.
Second, PrintAccount()
method must be static (it belongs to the Bankprogram
class) which is called from static Main()
method.
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);
}
}
Upvotes: 0