Reputation: 196679
I have a variable to show RAG Status (Red, Amber, Green) on a project
var previousStatus = "R"
var currentStatus = "A"
and i am trying to calculate "Trend" so sometihng like
var trend = CalculateTrend(previous, current)
I am trying to find a more elegant solution than
if (prev == current)
return "Stable";
if (prev == "R" && (current == "G" ||current == "A"))
return "Improving";
if (prev == "G" && (current == "R" ||current == "A"))
return "Declining";
if (prev == "A" && current == "G")
return "Improving";
if (prev == "A" && current == "R")
return "Declining";
any suggestion on a "cleaner" solution.
Upvotes: 1
Views: 1184
Reputation: 3615
Create an enum
with an integer value for each status.
public enum Status
{
Red = 1,
Amber = 2,
Green = 3
}
Then use int.CompareTo
method.
switch(previous.CompareTo(current))
{
case -1:
return "Improving";
case 0:
return "Stable";
case 1:
return "Declining";
}
Upvotes: 5
Reputation: 7117
If you just want to avoid the if statements you can create a simple lookup dictionary:
private static string GetTrend(string prev, string cur)
{
var trends = new Dictionary<string, string[]>()
{
{"Stable", new[] {"AA", "RR", "GG"}},
{"Improving", new[] {"RG", "RA", "AG"}},
{"Declining", new[] {"GA", "GR", "AR"}},
};
return trends.Single(x => x.Value.Contains(prev + cur)).Key;
}
Upvotes: 0
Reputation: 1286
Use a numeric system. Rate the values with equivalent numeric values. Then you can use math.
Upvotes: 0