Genius Boy here just spent ten minutes debugging a class because I used the private field directly inside the class, instead of the property accessor method.
Here's what the property looked like:
public string FileSpec { get { return _fileSpec; } } private string _fileSpec;
Notice there's no set method. The file spec is set only in the constructor, and is immutable at runtime. That means that throughout the class I had code like this:
if (File.Exists(_fileSpec)) { _cache.Load(_fileSpec); }
The problem? Well, the private data could contain any number of tokens representing disk or remote folders, which wasn't a requirement when the class was first built.
The solution? C# 2.0 gives you the power to create an accessors of different visibilities, like this:
public string FileSpec { get { return _fileSpec; } private set { _fileSpec = DeTokenize(value); } }
The constructor now looks like this:
public MyClass(string fileSpec) { FileSpec = fileSpec; }
And then the code that uses the value can do this:
if (File.Exists(FileSpec)) { _cache.Load(FileSpec); }
All better.
dasBlog theme by Mads Kristensen
All content Copyright ©2010 David Braverman. The Daily Parker by David Braverman is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 United States License, excluding photographs, which may not be republished unless otherwise noted.
Disclaimer David Braverman is the author of this blog and is solely responsible for its content.
Avanade does not control or endorse the content, messages or information found in any public Weblog, and therefore specifically disclaims any liability with regard to this Weblog and any actions resulting from the author's participation in any Weblog.
The ParkerCam may return, but until it does, it's still a good photo of him.