Need Help with Properties in C#

Discussion in 'C#' started by chobo2, Jan 27, 2007.

  1. chobo2

    chobo2 New Member

    Joined:
    Jan 27, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hi

    I been doing my assignment and thought I was finished but I found out when my teacher keep going a read-only string property ( I thought property was C# name for variable - we never talked about it in lec and was not in my notes)

    So anyways I got to make changes to my program this is what I had before

    4 classes

    BankAccount - base class. This class has some variable and methods.

    SavingAccount - inherits stuff from BankAccount but has a couple formating methods and an addinterest method

    CheckingAccount - inherits stuff from BankAccount has a couple formating methods and also overrides a withdraw method.

    In my bankAccount class I have these variables


    protected readonly string firstName;
    protected readonly string lastName;
    protected readonly string accountNo;
    protected readonly string id;

    protected decimal balance;

    So now all these are suppost to be properties so the first 4 are only readonly

    and I am guessing the balance should be read and write(he had only readonly what i don't think can be done otherwise you can't change the balance).

    So my question is that my checkingAccount/SavingAccount needs to use these variables and of course now that they are properites they have to be private so now I need to use the properties everytime I need to get these.

    So in my inherited classes do I have to go BankAccount acct1 = new BankAccount?

    or is there some other way I can do it.

    Here is an example what my property looks like.

    // this is in the bankAccount class
    public string FirstName
    {
    get
    {
    return this.firstName;
    }

    }

    but say in my checking class I will have something like a toString method to help format the output the way I want it too look like

    example

    public string toString()
    {
    // this is how I would do it the old way when I had protected and it was inherited.
    return("firstName" + this.firstName);
    }

    But of course this does not work so how do I do it now?

    Thanks
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,374
    Likes Received:
    388
    Trophy Points:
    83
    You aim at not inputing the firstname, lastname .... Thats not what can be expected from any design.
    So whats the harm in using the props. Also the vars can be protected and then the derived class can use the protected members in the class.

    I am not sure what you are trying to do??
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice