patterncsharpMinor
Validate the set accessors in a simple Triangle class
Viewed 0 times
validatesimpletheaccessorstriangleclassset
Problem
I have the following sample Triangle class and I want to validate the set accessors' values, obviously I cannot have a zero side or height Triangle. But while I am writing the validation rules for each property I found myself repeating the same code over and over again. Is there a more concise way to write these properties or to group them together? You can assume how newbie to C# I am.
```
// The Shape class
abstract class Shape
{
// Shape's Methods
abstract public double GetArea();
abstract public double GetPerimeter();
}
// The Triangle class
class Triangle : Shape
{
private double sideA;
private double sideB;
private double baseT;
private double height;
public double SideA
{
get { return sideA; }
set
{
if (value > 0)
{
sideA = value;
}
}
}
public double SideB
{
get { return sideB; }
set
{
if (value > 0)
{
sideB = value;
}
}
}
public double BaseT
{
get { return baseT; }
set
{
if (value > 0)
{
baseT = value;
}
}
}
public double Height
{
get { return height; }
set
{
if (value > 0)
{
height = value;
}
}
}
// Triangle's constructor.
public Triangle(double sideA, double sideB, double baseT, double height)
{
this.sideA = sideA;
this.sideB = sideB;
this.baseT = baseT;
this.height = height;
}
// Get Triangle's area.
public override double
```
// The Shape class
abstract class Shape
{
// Shape's Methods
abstract public double GetArea();
abstract public double GetPerimeter();
}
// The Triangle class
class Triangle : Shape
{
private double sideA;
private double sideB;
private double baseT;
private double height;
public double SideA
{
get { return sideA; }
set
{
if (value > 0)
{
sideA = value;
}
}
}
public double SideB
{
get { return sideB; }
set
{
if (value > 0)
{
sideB = value;
}
}
}
public double BaseT
{
get { return baseT; }
set
{
if (value > 0)
{
baseT = value;
}
}
}
public double Height
{
get { return height; }
set
{
if (value > 0)
{
height = value;
}
}
}
// Triangle's constructor.
public Triangle(double sideA, double sideB, double baseT, double height)
{
this.sideA = sideA;
this.sideB = sideB;
this.baseT = baseT;
this.height = height;
}
// Get Triangle's area.
public override double
Solution
Assuming you'll have other shapes that would have the same requirement, one option you have is to have a class Side inside the class Shape, and put the validation in there. Now every shape can declare as many sides as needed and each will be validated. Some thing like this should work:
In regards to the height, instead of having it passed to the constructor, calculate the height from the length of the sides, Heron's formula should work.
private class Side
{
double length;
public double Length
{
get{return length;}
set
{
if(value > 0)
{
length = value;
}
else
{
throw new ArgumentException("Value must be greater than 0");
}
}
}
Side(double _length)
{
Length = _length;
}
}In regards to the height, instead of having it passed to the constructor, calculate the height from the length of the sides, Heron's formula should work.
Code Snippets
private class Side
{
double length;
public double Length
{
get{return length;}
set
{
if(value > 0)
{
length = value;
}
else
{
throw new ArgumentException("Value must be greater than 0");
}
}
}
Side(double _length)
{
Length = _length;
}
}Context
StackExchange Code Review Q#159995, answer score: 3
Revisions (0)
No revisions yet.