I have this method called "DoStuff()" and what it does is that it checks if the checkboxes are checked and if they are then does something.
private void DoStuff()
{
if(Checkbox1.isChecked == true)
{
DoSomething();
}
if(Checkbox2.isChecked == true)
{
DoSomething();
}
if(Checkbox3.isChecked == true)
{
DoSomething();
}
}
How do I correctly set a bool so I don't have to do "== true" for every if statement?
As checkboxes can be tri-state (checked, unchecked & undetermined) then you'll have to leave the == true
in place as Checkbox.IsChecked
is a nullable boolean which can't be tested simply like a standard boolean.
Even if the checkbox is bi-state and can't be set into it's indeterminate state by the user IsChecked
remains a nullable boolean as it can be set to null
programmatically.
In this case there is absolutely no harm in leave the code as has you have it. Anything you do to try to remove the == true
will more than likely make the code less readable, less maintainable and potentially less stable. You can, for example, just do this:
if ((bool)Checkbox1.IsChecked)
{
}
But this will raise a NullReferenceException if the checkbox is ever in the indeterminate state, and so is not good practice at all.
bool? is a nullable type and can be 'true', 'false', or 'null'.
If you know the value won't be null then you can do:
if(Checkbox1.IsChecked.Value)
or if you didn't know whether it was null:
if(Checkbox1.IsChecked ?? false)
but there is nothing wrong with keeping == true
either...
Anything you place in the brackets after the if must be a boolean value, the code will not compile otherwise. Essentially it only understands if(false) and if(true)
IsChecked is a nullable bool, it can be true, false or null. The ==True is needed so that you are left with a "predicate" that can be resolved as either true or false.
if ischecked is null then .IsChecked == true will be false, if IsChecked is false the .IsChecked == true will also be false. Only when .IsChecked == true, only then will it return true and move into the if block
I'm a little confused by your question, but reading your comments it sounds like you are dealing with a nullable boolean. In which case it can be null, true or false. Nullable objects have a property .HasValue that should be checked prior to accessing. If HasValue is true, then it is not an null object.
Your code would look like this:
if(checkbox.IsChecked.HasValue == true && checkbox.IsChecked.Value == true)
{
DoStuff();
}
A slightly shorter version:
if(checkbox.IsChecked.HasValue && checkbox.IsChecked.Value)
{
DoStuff();
}
The bool is "hidden" behind a nullable object, so you access the bool using Value
. First, you need to check HasValue
and if that is true, access the bool via Value
. If HasValue
is false then Value
is null.
You can create an extension method as below
public static class BoolExtensions
{
public static bool GetBoolOrDefault(this bool? val, bool defaultval)
{
return val.HasValue ? val.Value : defaultval;
}
}
And use like this way
private void DoStuff()
{
if(Checkbox1.isChecked.GetBoolOrDefault(false))
{
DoSomething();
}
if(Checkbox2.isChecked.GetBoolOrDefault(false))
{
DoSomething();
}
if(Checkbox3.isChecked.GetBoolOrDefault(false))
{
DoSomething();
}
}
CheckBox.IsChecked is a Nullable<bool>
property
if statement requires a straight bool
by == true
you are getting an implicit cast
option
Debug.WriteLine(cb1.IsChecked.ToString());
//Debug.WriteLine(((bool)cb1.IsChecked).ToString()); // this fails on null
if (cb1.IsChecked ?? false)
Debug.WriteLine("cb1.IsChecked ?? false");
if (cb1.IsChecked ?? true)
Debug.WriteLine("cb1.IsChecked ?? true");
if (cb1.IsChecked == true)
Debug.WriteLine("cb1.IsChecked == true");
what you are getting with == true is cb1.IsChecked ?? false
你可以做 :
if (Checkbox1.Checked)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.