Reputation: 67
I have an Account class that contain a readonly variable balance. Constructor will assign default value as 0.
But i want to update that value when I call CashIn
method.
I tried this:
class Account
{
private string name;
private readonly double balance;
public Account(string n, double b)
{
name = n;
balance = b;
}
public void CashIn(double amount)
{
balance = balance+amount;
}
}
But as I know, readonly variable could be assigned through constructor or field.
Is it possible to update balance when i call CashIn
method?
If yes then Please describe me how?
Upvotes: 5
Views: 13244
Reputation: 1
The readonly value can be changed at constructor level. But it can be initialised at the time of declaration and can also be changed at constructor level
Upvotes: 0
Reputation: 10818
There is a big difference between the readonly
modifier and the concept of a "read only" property.
The readonly
modifier means your field can only be set in the constructor. You will not be able to set that variable to anything else outside of the constructor. Given the context of your code (a bank account simulator) it seems weird that you would have a balance that never, ever changes. Which leads me to believe you want a read only property.
A property is the preferred way of storing simple data that has get/set methods. For a bank account simulator it would be really bad to just have the balance exposed with a public setter right? So I think in that context it makes more sense to have a property with a public read, and a private set:
public decimal Balance { get; private set; }
This allows it to be read publicly but its only allowed to be modified inside the class (i.e from a custom method you write like CashIn()
)
public void CashIn(decimal amount)
{
Balance += amount;
}
Please note how I use decimal
instead of double
. Monetary values should always be represented as decimal
, this is why it exists
Upvotes: 16
Reputation: 37313
Create a private variable, and a Public Readonly Property that return its value, so you can edit the variable from the same class, but it is read only to other classes.
private double balance_;
public double Balance
{
get
{
return balance_;
}
}
Or you can use private set
public double Balance { get; private set; }
Check this link for more info: understanding private setters
Upvotes: 0
Reputation: 41
A simple solution is to remove a read-only tag for a variable. you can not change a value of read-only variables once it's initialized. you can initialize it while creating the object and thereafter it will be treated as a constant for the lifetime of that object.
Upvotes: 0
Reputation: 1062600
The entire point of readonly
is that you cannot do this, except in the constructor. Since readonly
is a language level feature, not a strict runtime guarantee: you can cheat (via reflection or unsafe
or various other evils), but it seems to me that the most pragmatic thing is to: remove the readonly
modifier. Since you don't seem to want it to be read-only.
Upvotes: 4