I've a little messy method, which generates a name out of three strings. The three strings can be null. I've to check each possible combination and create a string, based on the given available values. There isn't a fix position / layout for each variable in the resulting name, it depends on the availability of all strings.
My method looks like this right now:
private String GetName(string variable, string label, string dimension)
{
string result = String.Empty;
if (!String.IsNullOrEmpty(label) && !String.IsNullOrEmpty(variable) && !String.IsNullOrEmpty(dimension))
{
result = String.Format("{0} [{1}] ({2})", label, dimension, variable);
}
else if (!String.IsNullOrEmpty(label) && !String.IsNullOrEmpty(dimension))
{
result = String.Format("{0} [{1}]", label, dimension);
}
else if (!String.IsNullOrEmpty(label) && !String.IsNullOrEmpty(variable))
{
result = String.Format("{0} ({1})", label, variable);
}
else if (!String.IsNullOrEmpty(label))
{
result = String.Format("{0}", label);
}
else if (!String.IsNullOrEmpty(variable) && !String.IsNullOrEmpty(dimension))
{
result = String.Format("{0} [{1}]", variable, dimension);
}
else if (!String.IsNullOrEmpty(variable))
{
result = String.Format("{0}", variable);
}
return result;
}
Any suggestions on how to improve this method (Get rid of the if / else construct)?
else if (!String.IsNullOrEmpty(dimension))