Why does VS think this statement is always true?

I have this if statement -

if (!((main.property == 1)||(main.property == 2))) { ... } 

main.property is a byte that is 0, 1, 2, or 98.

Visual studios say this statement is always true, but I don’t understand why?

If the value 1 or 2 should not be false.

Thanks in advance.

Edit: code added

file1.cs

 private void Upload(DataSet ds) { Main main = CreateMain(ds); //This is tested and works correctly if(ValidateDate(main)) { ... } } 

file2.cs

 internal static bool ValidateData(Main main, ...) { if (!((main.property == 1)||(main.property == 2))) { ... } } 

Edit: code added

If I do this, the error will disappear -

 internal static bool ValidateData(Main main, ...) { main.property = 0; //Or = any number if (!((main.property == 1)||(main.property == 2))) { ... } } 

I assume that VS thinks it is not initialized, but I am 100% sure of that.

+4
source share
4 answers

I wrote this little program to check it out:

  class Program { static void Main(string[] args) { test(0); test(1); test(2); test(3); Console.ReadLine(); } private static void test(int p) { bool b1 = (!((p == 1) || (p == 2))); bool b2 = (p != 1 && p != 2); Console.Out.WriteLine("{0} {1} {2}", b1, b2, b1 == b2); } } 

It seems that Konstantin is right in his transcription, but not one expression is always true or always false.

+2
source

Pre-PS: Inspired by Jon Skeet's comment above, I suggest you do the following:

  • (Optional: back up the current code.)
  • Simplify your code by gradually removing parts of it until the warning disappears. *
  • As soon as the warning disappears, you will find out which parts of your code caused the error (namely, the part that you deleted last), and then you can understand it.

( Where did this advice come from? → You already saw that you can make the warning go away by adding code. I think your understanding of the problem will be even better when you do the opposite, that is, delete the code to get to the bottom of the problem.)


main.property can only have values ​​0, 1, 2, and 98, and you said that your code ( CreateMain ) works correctly in this regard.

Thus, I assume that VS assumes that main.property will always be 0. Why this should come to this conclusion, I do not know. Perhaps he thinks he was not initialized.

I wrote code similar to yours and I don’t get this warning - I don’t even get a warning when I declare main.property as readonly and initialize it to 0. Thus, you probably main.property up in one of these situations when the crash code analysis / debugger VS.

Possible (though unlikely) reasons:

  • Is there any code path where main.property not initialized?

  • Since main.property is of type byte (at your discretion), maybe the warning is somehow caused by an overflow or overflow? (What happens if you change the type to int ?)

  • Could it be that main obscures another main variable?

+1
source

Go through your code to find out what the value of main.property is.

I had a similar problem, and that was because the value was null by default.

0
source

Since you are claiming that main.property is a byte that is 0, 1, 2, or 98, you should state this condition at the beginning of this method as such:

 internal static bool ValidateData(Main main, ...) { System.Diagnostics.Debug.Assert(main.property == 0 || main.property == 1 || main.property ==2 || main.property == 98); if (!((main.property == 1)||(main.property == 2))) { ... } } 
0
source

All Articles