Question

I am using Visual Studio 2008.  I literally started learning C#  3 days ago and I'm kind of stuck trying to figure out why I can't get this to compile.

I get this error:
Error1           } expected  Line: 17  Column: 6
Error2      Type or namespace definition, or end-of-file expected   Line: 50      Column 1

From what I can gather it is wanting me to close the constructor as soon as I open it...which isn't very helpful.
Like I said I just started learning this 3 days ago so feel free to give me any tips or tricks that might be handy.  

Thanks in advance,

Brandon
using System;
 
interface IAccount
{
    bool WithdrawFunds(decimal amount);
    decimal GetBalance();
    void PayInFunds(decimal amount);
    void SetName(string name);
    void SetAddress(string address);
}
 
public class Account: IAccount{
    
    private decimal balance = 0;
 
    public Account(string inName, string inAddress, decimal inAmount) 
    { // This is where it says " } expected"
        public virtual bool WithdrawFunds(decimal amount){
            if(balance<amount){
                return false;
            }
            balance -= amount;
            return true;
        }
 
        public void PayInFunds(decimal amount){
            balance += amount;
        }
 
        public decimal GetBalance(){
            return balance;
        }
        
        public void SetName(string Name){
            Console.Write("Enter your name: ");
            Name=Console.ReadLine();
        }
 
        public void SetAddress(string Address){
            Console.Write("Enter your Address: ");
            Address = Console.ReadLine();
        } 
    
        public Account(string inName,string inAddress):this (inName,inAddress, 0){ 
        }
 
        public Account (string inName):this (inName, "No address",0){
        } 
    }
} // Here is where it says "Type or namespace definition, or end-of-file expected"
    
public class Bank{
    public static void Main(){
        IAccount B = new Account("This is my name","This is my address", 12345);
        B.GetBalance();
        B.PayInFunds(50);
        B.GetBalance();
        Console.ReadLine();
    }
}

Open in new window

LVL 1
brandon-sheltonAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pratima PharandeCommented:
 public Account(string inName, string inAddress, decimal inAmount)
    { }// This is where it says " } expected"
0
Pratima PharandeCommented:
you can initalize values in consructor

public Account(string inName, string inAddress, decimal inAmount)
    {
inName = "test";
}// This is where it says " } expected"




0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
brandon-sheltonAuthor Commented:
Is it that I can't have methods inside of the constructor?

If so then I could create members of the class to store the values passed to the constructor when it was called...correct?  Then I could take the methods that were in the constructor and move them out of the constructor and just make them methods of the class.
Right?
0
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

CuteBugCommented:
This is because you have not completed the definition of the constructor in Line 17 and in Line 18 you started the declaration of another method called WithdrawFunds.

This will be solved if you add a '}' at line 18. This will complete the constructor.
0
Pratima PharandeCommented:
Right..

constructor is called only one time when object is created
0
CuteBugCommented:
You cannot have methods inside the constructor.
The constructor is a method itself.

Yeah you can have class members to store the values passed in the constructor. Then you can use them in the other methods of the class.
0
brandon-sheltonAuthor Commented:
Awesome help!  Thanks for the quick response
0
brandon-sheltonAuthor Commented:
CuteBug.  OK, thank you. I would have shared the points but I didn't get your post until I was done.
Thank you for the info though.  I guess that I must have missed something about the syntax of constructors when I was reading through the book.
0
brandon-sheltonAuthor Commented:
WOW.  Going through my code I realized that I had a lot of  useless stuff.  I also had my name and address methods way off...but it's much better now. And it compiles!
 
Thank both of you for all of your help!

using System;
 
interface IAccount
{
    bool WithdrawFunds(decimal amount);
    decimal GetBalance();
    void PayInFunds(decimal amount);
    void SetName(string name);
    void SetAddress(string address);
    string GetName();
    string GetAddress();
}
 
public class Account: IAccount{
    
    private decimal balance = 0;
    private string name, address;
 
    public Account(string inName, string inAddress, decimal inAmount) 
    {
        name = inName;
        address = inAddress;
        balance = inAmount;
    }
        
    public virtual bool WithdrawFunds(decimal amount){
        if(balance<amount){
            return false;            
        }          
        balance -= amount;
        return true;
        
    }
            
    public void PayInFunds(decimal amount){            
        balance += amount;
        
    }
 
    public decimal GetBalance(){            
        return balance;        
    }
           
    public void SetName(string Name){
        name = Name;     
    }
 
    public string GetName()
    {
        return name;
    }
            
    public void SetAddress(string Address){
        address = Address;      
    }
 
    public string GetAddress()
    {
        return address;
    }
        
    public Account(string inName,string inAddress):this (inName,inAddress, 0){        
    }
        
    public Account (string inName):this (inName, "No address",0){        
    }    
}
    
public class Bank{
    public static void Main(){
        IAccount B = new Account("This is my name","This is my address", 12345);
        Console.WriteLine(B.GetBalance());
        B.PayInFunds(50);
        Console.WriteLine(B.GetBalance());
        Console.WriteLine(B.GetName() + "\n");
        B.SetName("Brandon");
        Console.WriteLine(B.GetName());
        Console.WriteLine(B.GetAddress());
        Console.ReadLine();
    }
}
 
    

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.