The problem is here: (bool) (flag2 ? 1 : 0)
. You’re trying to convert an integer to a boolean using an explicit cast.
Use Convert.ToBoolean
instead: Convert.ToBoolean(flag2 ? 1 : 0)
. BTW, it seems like it’s so useless: why do you want to convert 1
to true
and 0
to false
if flag2
is already bool
?
BTW, now you should have already realized that bool
has no implicit or explicit conversion to int
and vice versa and you need to use Convert.ToBoolean
/Convert.ToInt32
…