Re: OOP question about properties & methods
- From: Barry Kelly <barry.j.kelly@xxxxxxxxx>
- Date: Mon, 14 Aug 2006 16:15:50 +0100
Bruce One <raul@xxxxxxxxxxxxxxxxxxxxxx> wrote:
Kelly,
My name is Barry!
Could your explain better points 3 and 4?
3) If reading doesn't modify the observable state of the class
Just reading a property shouldn't change the property. For example:
Button b = new Button();
// ...
string text = b.Text;
This action (reading b.Text, which is a property) shouldn't change the
property. Another example:
IEnumerator x = new List().GetEnumerator();
bool b = x.MoveNext();
This action, MoveNext(), changes the enumerator - it moves it forward
one. Therefore it shouldn't be a property, it should be a method (which
it is).
4) If writeable, then writing and subsequently reading gives the same
value (i.e. { a.Prop = 42; Debug.Assert(a.Prop == 42); }), unless
writing the property throws an exception because of a problem with the
value.
When there is a property, people expect this:
b.Name = "foo";
if (b.Name != "foo")
throw new Exception("Something went wrong!");
If this code would throw an exception for your property, then it
shouldn't be a property - it should be a method.
This only matters if the property is writeable - that is, it has a 'set'
method.
-- Barry
--
http://barrkel.blogspot.com/
.
- References:
- Re: OOP question about properties & methods
- From: Cor Ligthert [MVP]
- Re: OOP question about properties & methods
- From: Bruce One
- Re: OOP question about properties & methods
- From: Barry Kelly
- Re: OOP question about properties & methods
- Prev by Date: silly questions for an expert in OOP
- Next by Date: Re: Are there any INTRINSIC advantages of the C# language over VB.NET?
- Previous by thread: Re: OOP question about properties & methods
- Next by thread: Re: OOP question about properties & methods
- Index(es):