views:

108

answers:

7

When referencing class properties from a function within the class do you use the value from the actual property or the private variable value?

Which way is best? Why?

public class

  private m_Foo as double

  public property Foo() as double
    get
      return m_Foo
    end get
    set(byval value as double)
      m_Foo = value
    end set
  end property

  public function bar() as double
    Dim x as double = 5 * m_Foo
    Dim y as double = 3 * Foo
  end function

end class
+7  A: 

Personally, I try to use the get/set accessor whenever possible, to avoid surprising myself when I change their logic and suddenly places where I access the private field don't work as expected.

Aviad P.
+3  A: 

The property code may contain -- now or in the future -- logic that returns a different value or sets a different value depending on the state of the object. It makes sense to use the property.

Adam Crossland
And, if you are in the habit of using automatic properties, you don't have a choice.
John Kraft
+1  A: 

I prefer the latter. If your property simply returns the field, the compiler will optimize your calls to it away. And you need to protect yourself against your own changes just like everyone else. What if the property later does something to the field before returning it, you'd have to update all your internal code to accommodate.

Matt Greer
+1 for mentioning the inlining optimization. When you take that into consideration, the question really becomes "why would you not use the property?" (aside from the obvious case when you use a ref param)
Josh Einstein
+1  A: 

It's probably safer (but not better) to use the property just in case you add additional get logic later on. Of course, adding logic to get for something that only yesterday was a 'plain' property probably isn't a good idea.

Dmitri Nesteruk
Exactly what I was thinking. Changing the behavior of a property could be a bad idea, which, of course, brings up another question, how much logic should be in a property?
The change may come during refactoring, during the prototyping, during any phase prior to production.
Aviad P.
A: 

Often the property contains simple logic, which you may miss out on accessing the variable directly.

public class MyClass 
{

   private List<string> _someList;

   public List<string> SomeString
   {
      get
      {
         if(_someList == null)
            _someList = new List<string>();
         return _someList;
      }
   }

}

if you then access _someList directly, you may run into a null reference. Accessing the property directly ensures it's been initialized.

Some may argue the private variable should have simply been declared as new in the first place, or initialized in the constructor rather than the property - but this should at least highlight a possible issue as this is a common approach.

KP
A: 

Use the property name. That way, the rest of the class code can function independently of how you choose to implement that property.

Maybe you will want to change the private variable tomorrow to, say, half it's value. Then you can change your Get to 'return 2 * m_HalfFoo and your old code keeps on working.

With the Set, it's even more obvious: you could validate the value you set your variable to at one point. Use the Set Property everywhere and don't worry about validating this property value.

Ralf MC
+1  A: 

Two things.

First, this is a duplicate of

http://stackoverflow.com/questions/586087/when-should-a-class-use-its-own-getters-setters-vs-accessing-the-members-directly

Second, here's my article from earlier this year on a closely related subject:

http://blogs.msdn.com/ericlippert/archive/2009/01/14/automatic-vs-explicit-properties.aspx

Eric Lippert