I have two different data inputs for the same value, I want to use the non empty one if one of them is, to return the empty string if both are empty, and to flag an error if both are not empty but differ and return one of them.
Empty for my criteria matches the meaning of string.IsNullOrEmpty()
.
And it's okay that it's written as a property (don't worry about that part.)
This works but feels overly verbose. Can you think of any alternatives?
public string Callkey
{
get
{
if (string.IsNullOrEmpty(AS_CALL_KEY) && !string.IsNullOrEmpty(CALLKEY))
{
return CALLKEY.Trim();
}
if (!string.IsNullOrEmpty(AS_CALL_KEY) && string.IsNullOrEmpty(CALLKEY))
{
return AS_CALL_KEY.Trim();
}
if (!string.IsNullOrEmpty(AS_CALL_KEY) && !string.IsNullOrEmpty(CALLKEY))
{
if (!AS_CALL_KEY.Trim().Equals(CALLKEY.Trim()))
{
_error = true;
}
return AS_CALL_KEY.Trim();
}
if (string.IsNullOrEmpty(AS_CALL_KEY) && string.IsNullOrEmpty(CALLKEY))
{
return "";
}
_error = true;
return "THISNEVERHAPPENS";
}
}