I made a class with a code contract in the constructor CodeTypes has a list of string properties.
Contract.Requires(type == CodeTypes.AdmitDx1 || type == CodeTypes.AdmitDx2
|| type == CodeTypes.AdmitDx3 || type == CodeTypes.DX
|| type == CodeTypes.CPT || type == CodeTypes.HCPCS
|| type == CodeTypes.PX);
After that warnings appeared where I was using this class because different values were put in. So far so good.
Then I went to a piece of the code where there was a complaint and added the opposite of the if statement and threw an exception.
The warning remained.
I then added a return statement on the opposite of the if statement.
The warning remained.
I then put a big if/else where the class was only called if the condition in the contract was meant.
The warning remained.
I then surrounded the contract with an if statement with that condition.
The warning disappeared.
What is going on here? I expected the 1st 3 options to work and I really wanted to use option 1. What is the best way to go about this?
Edit the calling Code the contract exists in the constructor of CreateCodeFromType:
//Type is an XElement that is passed into the method as a parameter
if (type == null || (type.Value != CodeTypes.AdmitDx1 && type.Value != CodeTypes.AdmitDx2
&& type.Value != CodeTypes.AdmitDx3 && type.Value != CodeTypes.DX
&& type.Value != CodeTypes.CPT && type.Value != CodeTypes.HCPCS
&& type.Value != CodeTypes.PX))
{
throw new ValidationException("Type is invalid.");
}
else
{
var newCode = (new CreateCodeFromType(type.Value).CreateCode(term.Value));
}