Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/forums/c-sharp/)
-   -   Need Help with Properties in C# (http://www.go4expert.com/forums/help-properties-c-sharp-t2712/)

chobo2 27Jan2007 09:44

Need Help with Properties in C#
 
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

shabbir 27Jan2007 11:49

Re: Need Help with Properties in C#
 
Quote:

Originally Posted by chobo2
So now all these are suppost to be properties so the first 4 are only readonly

You aim at not inputing the firstname, lastname .... Thats not what can be expected from any design.
Quote:

Originally Posted by chobo2
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 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.

Quote:

Originally Posted by chobo2
But of course this does not work so how do I do it now?

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


All times are GMT +5.5. The time now is 09:41.