I've currently got a class that has multiple string properties. One or more of them may have a value. What I need to do is get the first non-empty property, based off some priority. Here is an example of what I envision the class would look like:
public class MyClass
{
[Priority(1)]
public string HighestPriority { get; set; }
[Priority(2)]
public string MiddlePriority { get; set; }
[Priority(3)]
public string LowestPriority { get; set; }
}
Or, maybe use another enum property that can be used to determine the highest one that is set?
public enum Priority
{
HighestPriority,
MiddlePriority,
LowestPriority
}
public class MyClass
{
private string highestPriority;
public string HighestPriority
{
get;
set
{
highestPriority = value;
HighestSetProperty = Priority.HighestPriority;
}
}
private string middlePriority;
public string MiddlePriority
{
get;
set
{
middlePriority = value;
if (HighestSetProperty != Priority.HighestPriority)
HighestSetProperty = Priority.MiddlePriority;
}
}
private string lowestPriority;
public string LowestPriority
{
get;
set
{
lowestPriority = value;
if (HighestSetProperty != Priority.HighestPriority || HighestSetProperty != Priority.MiddlePriority)
HighestSetProperty = Priority.LowestPriority;
}
}
public Priority HighestSetProperty { get; set; }
}
Then, use HighestSetProperty to set the string in a switch statement?
So far though, the only way I know of to find the first non-empty property, without using some form of priority attribute like above, is something like this:
string highestPriorityProp = String.IsNullOrWhitespace(HighestPriority) ? (String.IsNullOrWhitespace(MiddlePriority) ? LowestPriority : MiddlePriority) : HighestPriority;
Which is obviously messy and doesn't scale well if more properties are added. So, what is the best way (in terms of readability, speed and scalability) of doing this? Thanks in advance.
EDIT: Let's go for cleanliness and scalability over speed.
EDIT: The duplicate question doesn't actually answer my question. As the properties may not be in the order of priority. Hence, the first one that is looped through may be a lower priority than the highest one set. For instance:
public class MyClass
{
public string MiddlePriority { get; set; }
public string HighPriority { get; set; }
}
EDIT: Thanks for the help everyone. As mjwills and I have discussed in the comments, The accepted answer would suit my needs as I have only around 6 properties in my class. But, if there were more, the duplicate answer is probably the best way to go.