• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 337
  • Last Modified:

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

0
brandon-shelton
Asked:
brandon-shelton
  • 4
  • 3
  • 2
1 Solution
 
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
 
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now