Reputation: 517
I have the following coverage class:
public class Coverages
{
public double? Deductible1 { get; set; }
public double? Deductible2 { get; set; }
public double? Deductible3 { get; set; }
public bool IsCoverage { get; set; }
public int? Limit1 { get; set; }
public int? Limit2 { get; set; }
public int? Limit3 { get; set; }
public string DeductType1 { get; set; }
public string DeductType2 { get; set; }
public string DeductType3 { get; set; }
}
I also have the following logic:
if (coverage.Deductible1.HasValue)
{
var coverageValue = !string.IsNullOrEmpty(coverage.DeductType1)
? string.Format("{0}/{1}", coverage.DeductType1, coverage.Deductible1)
: coverage.Deductible1.Value.ToString();
coverages.Add(new CoverageDto
{
Name = string.Format("{0} - {1}", coverage.CoverageCode, "All Peril"),
Value = coverageValue
});
}
I would like to reuse this logic for Deductible2
, Deductible3
which will of course use its respective DeductTypes
(ie. DeductType1
for Deductible1
). How do I parameterize the properties of the class to reuse this logic or is there a better of doing this?
Thanks in advance.
Upvotes: 0
Views: 125
Reputation: 74625
If you're after a simple rule you could consider that "Any time you have a property name with a number on the end of it it's a candidate for backing it with an array so you can address it programmatically"
public class Coverages
{
private double?[3] deductibles = new double?[3];
public double? Deductible1 { get => deductibles[0]; set => deductibles[0] = value; }
public double? Deductible2 { get => deductibles[1]; set => deductibles[1] = value; }
...
Then you can refer to them programmatically, for example;
for(int x = 0; x<deductibles.Length; x++)
{
if (coverage.deductibles[x].HasValue)
{
var coverageValue = !string.IsNullOrEmpty(coverage.deductTypes[x])
? string.Format("{0}/{1}", coverage.deductTypes[x], coverage.deductibles[x])
: coverage.deductibles[x].Value.ToString();
coverages.Add(new CoverageDto
{
Name = string.Format("{0} - {1}", coverage.CoverageCode, "All Peril"),
Value = coverageValue
});
}
}
So if you ever find yourself wishing you could refer to some similar named properties that have some pattern to their name, you probably need a collection of them
Upvotes: 1
Reputation: 272035
Deductible
, Limit
and DeductType
seem very related, and should be grouped into a class/struct:
public class DeductibleInfo {
public double? Deductible { get; set; }
public int? Limit { get; set; }
public string DeductType { get; set; }
// Constructors...
// ...
}
Now Coverage
can have an array of DeductibleInfo
s:
public DeductibleInfo[] Deductibles { get; } = new DeductibleInfo[] {
new DeductibleInfo(),
new DeductibleInfo(),
new DeductibleInfo()
};
Now, you can easily extract a method for your logic:
public void SomeMethod(Coverage coverage, int deductibleIndex) {
var deductibleInfo = coverage.Deductibles[deductibleIndex];
if (deductibleInfo.Deductible.HasValue)
{
var coverageValue = !string.IsNullOrEmpty(deductibleInfo.DeductType)
? string.Format("{0}/{1}", deductibleInfo.DeductType, deductibleInfo.Deductible)
: deductibleInfo.Deductible.Value.ToString();
coverages.Add(new CoverageDto
{
Name = string.Format("{0} - {1}", coverage.CoverageCode, "All Peril"),
Value = coverageValue
});
}
}
Upvotes: 3