Link to home
Start Free TrialLog in
Avatar of brandon-shelton
brandon-sheltonFlag for United States of America

asked on

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

Avatar of Pratima
Pratima
Flag of India image

 public Account(string inName, string inAddress, decimal inAmount)
    { }// This is where it says " } expected"
ASKER CERTIFIED SOLUTION
Avatar of Pratima
Pratima
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of brandon-shelton

ASKER

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?
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.
Right..

constructor is called only one time when object is created
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.
Awesome help!  Thanks for the quick response
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.
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