accessing local properties

  • Thread starter Thread starter DKode
  • Start date Start date
D

DKode

Ok,

kind of a silly question I just want to know what the proper method is,

When I have local Properties, ie:

private bool _someVal;
public bool SomeVal {
get { return _someVal; }
}

When I accessing this property within the class it exists in, should I
use _someVal or always use the Property to retrieve the value? I guess
it really doesn't make a different but I noticed this is one part of my
code that isn't consistent, sometimes I use the property sometimes I
don't is there any OOP rules here to follow? THank you
 
Dkode,

Personally, I use the property, not the field that is backing it. The
reason for this is that with properties, there can be code that affects the
return value. More often than not, it is this kind of encapsulation that
you want when using properties, so it is best to use them everywhere that
you expose them (otherwise, it would be pointless to have the property in
the first place).

I say only in places where there is an explicit reason to not use the
property, you should use the field.

Hope this helps.
 
I don't see why that is a reason to use the field over the property.
Just because the backing field is read only, or the property is read only,
doesn't mean that the the relationship between the backing field and the
return result from the property is always going to return the same value.

It's ^because^ of this that you should use the property instead of the
backing field.
 
Nicholas Paldino said:
I don't see why that is a reason to use the field over the property.
Just because the backing field is read only, or the property is read only,
doesn't mean that the the relationship between the backing field and the
return result from the property is always going to return the same value.

It's ^because^ of this that you should use the property instead of the
backing field.

I was referring to a developer attempting to "set" the value of a
property...you can't if it's ReadOnly, in which case, you'd set the member
variable instead :)

Mythran
 
DKode said:
Ok,

kind of a silly question I just want to know what the proper method is,

When I have local Properties, ie:

private bool _someVal;
public bool SomeVal {
get { return _someVal; }
}

When I accessing this property within the class it exists in, should I
use _someVal or always use the Property to retrieve the value?

Not a silly question at all. In fact, it's the subject of much debate.

There are 3 situations to consider:

1. You want the functionality of the property. Obviously, use the property.
2. You explicitly DO NOT want the functionality of the property. Obviously,
do not use the property.
3. Using the property and the field is functionally equivalent. Here you've
got to see into the future to make a determination. If you use the property,
then your code is dependent on the property getter/setter code. If you use
the field, it is not. If someone should change the getter/setter in the
future, what should happen to the piece of code you are writing? I think you
have to take it on a case by case basis.
 
thank you very much for your responses.

I am going along, attempting to improve my code to a more normalized
foundation and this is one thing I was still struggling with.

thank you!

DKode
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top