How can I return null or bool from a function

I have this function:

public bool GetBoolSetting(Settings setting)
{
    return int.Parse(GetStringSetting(setting)) == 0 ? false : true;
}

      

The problem is what GetStringSetting(setting)

can return null

, "0"

or "1"

. When it returns a null

, I get an exception.

Is it possible for the function to GetBoolSetting

return null if this happens?

+3


source to share


6 answers


You can change the type of your method to Nullable<bool>

and continue with your linear operator as shown below:

public bool? GetBoolSetting(Settings setting)
{
    return GetStringSetting(setting) != null ? 
           int.Parse(GetStringSetting(setting)) != 0 : new bool?();//Or default(bool?)
}

      



You can also use default(bool?)

instead new bool?()

, which returns the default Nullable bool, which is null.

+2


source


In C #, as in most languages, regular booleans are not nullable. However, you can use nullable types to get the desired result.

For example, if you want a method to be able to return true

, false

or null

, you specify a nullable bool as the return type:

public bool? MyMethod()
{
    // Stuff
}

      

Pay attention to ?

after bool

. This indicates that the boolean value is null.

So, in your case, you can do:

public bool? GetBoolSetting(Settings setting)
{
    string myString = GetStringSetting(setting));
    if (string.IsNullOrEmpty(myString))
        return null;
    return int.Parse(myString) != 0;
}

      

Note that working with types like nullable bools is tricky. For example, if you try to pass this method result to another method that takes a bool parameter, you won't be able to immediately.

public void MyBoolMethod(bool parameter)
{
}

MyBoolMethod(MyMethod()); // This doesn't compile

      



You will need to cast a boolean zero back to normal after it is sure it is not . null:

MyBoolMethod((bool)MyMethod());

      

.. or alternatively choose a default for this case (which should be bool?

if it is null so that it can be used elsewhere) and convert it.

Example with false

default:

bool newBool = MyMethod() ?? false;

      

Additional advice that has little to do with your question, but I recommend that you do it. Instead of the unsafe int.Parse

one that throws an exception if the provided string is invalid, switch to Int32.TryParse

:

public bool? GetBoolSetting(Settings setting)
{
    string myString = GetStringSetting(setting));
    int output = -1;
    if (string.IsNullOrEmpty(myString) || !int.TryParse(myString, out output))
        return null;
    return output != 0;
}

      

+3


source


You probably want to use nullable bool like:

public bool? GetBoolSetting(Settings setting)
{
    var s = GetStringSetting(setting);
    if (string.IsNullOrEmpty(s))
        return null;
    return int.Parse(s) == 0 ? false : true;
}

      

Or, if you prefer to treat null as false:

public bool GetBoolSetting(Settings setting)
{
    var s = GetStringSetting(setting);
    if (string.IsNullOrEmpty(s))
        return false;
    return int.Parse(s) == 0 ? false : true;
}

      

Also, consider using TryParse

, not Parse.

+2


source


You are getting an exception because you int.Parse

cannot handle the input null

. You should check this before.

If you want to return null if it GetStringSetting

returns null, you need to change the return type to Nullable<bool>

(shorthand by adding a question mark:) bool?

, for example:

public bool? GetBoolSetting(Settings setting)
{
  var strSetting = GetStringSetting(setting);
  if (string.IsNullOrEmpty(strSetting))
    return null;
  return int.Parse(strSetting) == 0 ? false : true;
}

      

When using the return value, you have to check if it has a value that is more difficult to handle with:

var boolSetting = GetBoolSetting(...);
if (boolSetting.HasValue)
{
    // Check boolSetting.Value
}
else
{
     // Handle null value
}

      

See this link for an overview of nullable data types .

+2


source


public bool? GetBoolSetting(Settings setting)
{
    int result = 0;
    if (!int.TryParse(GetStringSetting(setting), out result))
        return null;
    return result == 0 ? false: true;
}

      

+2


source


I would recommend you revert true

or false

and get rid of the weird logic null

.

Using C # 7 syntax, this can be done in one line:

public bool GetBoolSetting(Settings setting) => int.TryParse(GetStringSetting(setting), out int i) && i == 1;

      

+2


source







All Articles