B
Breck
When I began work tonight, my class looked like this:
public class Plane3D
{
Vector3D _normal;
double _constant;
public Vector3D Normal { get { return _normal; } }
public double Constant { get { return _constant; } }
public Plane3D(Vector3D normal, double constant)
{
_normal = normal;
_normal.Normalize();
this._constant = constant;
}
public Plane3D(Vector3D p0, Vector3D p1, Vector3D p2)
{
_normal = Vector3D.CrossProduct(p1 - p0, p2 - p0);
_normal.Normalize();
_constant = Vector3D.DotProduct(_normal, p0);
}
public double DistanceTo(Vector3D p)
{
return Math.Abs(Vector3D.DotProduct(_normal, p) - _constant);
}
}
Then I Installed ReSharper (JetBrain's Visual Studio plugin), and its
refactoring tools suggested I change the fields to automatic
properties with private setters:
public class Plane3D
{
public Vector3D Normal { get; private set; }
public double Constant { get; private set; }
public Plane3D(Vector3D normal, double constant)
{
Normal = normal;
Normal.Normalize();
this.Constant = constant;
}
public Plane3D(Vector3D p0, Vector3D p1, Vector3D p2)
{
Normal = Vector3D.CrossProduct(p1 - p0, p2 - p0);
Normalize();
Constant = Vector3D.DotProduct(Normal, p0);
}
public double DistanceTo(Vector3D p)
{
return Math.Abs(Vector3D.DotProduct(Normal, p) - Constant);
}
}
After this change, though, the calls to Normal.Normalize() leave the
field Normal unchanged. My ignorance of private set before tonight
should indicate I'm a C# nublet, but this struck me as egregiously
dangerous because it compiled without warnings. Is there a best
practice regarding private set I need to know about, or do I have a
more fundamental misunderstanding of how properties work?
Thanks in advance,
-- Breck
public class Plane3D
{
Vector3D _normal;
double _constant;
public Vector3D Normal { get { return _normal; } }
public double Constant { get { return _constant; } }
public Plane3D(Vector3D normal, double constant)
{
_normal = normal;
_normal.Normalize();
this._constant = constant;
}
public Plane3D(Vector3D p0, Vector3D p1, Vector3D p2)
{
_normal = Vector3D.CrossProduct(p1 - p0, p2 - p0);
_normal.Normalize();
_constant = Vector3D.DotProduct(_normal, p0);
}
public double DistanceTo(Vector3D p)
{
return Math.Abs(Vector3D.DotProduct(_normal, p) - _constant);
}
}
Then I Installed ReSharper (JetBrain's Visual Studio plugin), and its
refactoring tools suggested I change the fields to automatic
properties with private setters:
public class Plane3D
{
public Vector3D Normal { get; private set; }
public double Constant { get; private set; }
public Plane3D(Vector3D normal, double constant)
{
Normal = normal;
Normal.Normalize();
this.Constant = constant;
}
public Plane3D(Vector3D p0, Vector3D p1, Vector3D p2)
{
Normal = Vector3D.CrossProduct(p1 - p0, p2 - p0);
Normalize();
Constant = Vector3D.DotProduct(Normal, p0);
}
public double DistanceTo(Vector3D p)
{
return Math.Abs(Vector3D.DotProduct(Normal, p) - Constant);
}
}
After this change, though, the calls to Normal.Normalize() leave the
field Normal unchanged. My ignorance of private set before tonight
should indicate I'm a C# nublet, but this struck me as egregiously
dangerous because it compiled without warnings. Is there a best
practice regarding private set I need to know about, or do I have a
more fundamental misunderstanding of how properties work?
Thanks in advance,
-- Breck