Is it possible to type casting an object to bool to return something depends on the state?

Viewed 159

I want to write a class to contain a bool value and a message. The message is used to explain why the class contains a false value. When I use this class, I would like to cast it to bool and it will return the bool value instead of get the property. Is this possible ?

public class ReturnResult
{
    public ReturnResult(bool state, string message)
    {
        IsSuccess = state;
        ErrorMessage = message;
    }

    public bool IsSuccess
    {
        get;
        private set;
    }

    public string ErrorMessage
    {
        get;
        private set;
    }
}

I want to do the following

ReturnResult rr = CallSomeFunction(a,b,c);

if ((bool) rr) {
  // it is good
}
else {
  // it is bad
}
2 Answers

Yup, you can override the true operator.

public class ReturnResult
{
    public ReturnResult(bool state, string message)
    {
        IsSuccess = state;
        ErrorMessage = message;
    }
    public bool IsSuccess
    {
        get;
        private set;
    }
    public string ErrorMessage
    {
        get;
        private set;
    }

    public static bool operator true(ReturnResult returnResult) => 
        returnResult.IsSuccess;

    public static bool operator false(ReturnResult returnResult) => 
        !returnResult.IsSuccess;    // Alternatively, implement as
                                    // returnResult ? false : true,
                                    // avoiding duplication.

}

You also have to define a matching false operator. Now these lines will work:

ReturnResult rr = CallSomeFunction(a,b,c);
if (rr) // Succeeds if the operator returns true, so if rr.IsSuccess is true.
{
    // If it's good.
}
else 
{
    // If it's bad.
}

EDIT: As Dmitry suggested, it's probably worth mentioning that you can also override the implicit conversion operator to bool:

public static implicit operator bool(ReturnResult returnResult) => 
    returnResult.IsSuccess;

While true and false are used in Boolean Expressions[^1], which at the time of this writing are limited to control statements and the ?: ternary operator, the implicit conversion operator will also allow assignments like this:

ReturnResult rr = CallSomeFunction(a,b,c);
bool b = rr;

You might wonder which one is used in an if statement if all three of them are overloaded - the answer is that the implicit conversion takes precedence, as per the spec.

[^1]: And also during && and || operators evaluation, if there are user-defined & and | operators defined on the type. For more info, the spec is your friend.

it should work if you write it like this (i wouldn't cast):

ReturnResult rr = CallSomeFunction(a,b,c);
if (rr.IsSuccesss) {
  // it is good
}
else {
  // it is bad
}
Related