Question

Is there anyway to make is so that I can say something like

if(boolClass) {}

Where the boolClass is calling a contained function. Kinda like an overloaded bool operator or something.

Thanks for any help.

Was it helpful?

Solution

There is actually a 'true' operator you can use for this purpose, though it's a bit obscure. This is slightly more specific than a conversion to bool, as it is limited to use in expressions that check for true/false.

public class BoolClass
{
    public static bool operator true(BoolClass instance)
    {
        return true; //Logic goes here
    }

    public static bool operator false(BoolClass instance)
    {
        return true; //Logic goes here
    }

    public void Test()
    {
        BoolClass boolClass = new BoolClass();
        if (boolClass)
        {
            //Do something here
        }
    }
}

Note that MS actually recommends against using this operator,as it was originally intended to allow for a kind of nullable bool type (where a value could be neither true nor false). Since nullable bools are now natively supported, those are preferred. I'd recommend against using it in production code, mainly because most developers won't be familiar with the syntax, causing confusion.

OTHER TIPS

My first point would be to caution you against this, usually you want to use the bool or bool? classes available directly or indirectly.

If you are certain that is what you need, then you will need an implicit conversion operator to bool

//In the definition of boolClass
public static implicit operator bool(boolClass obj)
{
    //Return a bool in this method
}

You can use a implicit operator to convert your class to a Boolean.

This is a full and simple example :

Classe

using System;

namespace TestLogic
{
    internal class FuzzyLogic
    {
        public FuzzyLogic(Double init)
        {
            this.value = init;
        }

        public Double value { get; private set; }

        public static implicit operator Boolean(FuzzyLogic logic)
        {
            return logic.value < 0.1;
        }
    }
}

Using the convertion

using System;

namespace TestLogic
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            FuzzyLogic logic = new FuzzyLogic(0.2);

            if (logic)
            {
                Console.WriteLine("It's true !");
            }
            else
            {
                Console.WriteLine("It's not true !");
            }

            Console.ReadLine();
        }
    }
}

Sounds like a property:

public bool boolClass
{
    get { return false; } // or a calculated boolean value
}

You can invoke it exactly like you asked about from inside the same class:

if(boolClass) {}

Add a conversion operator to your class. Example (ideone):

using System;

public class A
{
    private int i;
    public int I { get { return i; } }
    public A(int i) { this.i = i; }
    public static implicit operator bool(A a) { return a.i != 0; }
}

public class Test
{
    public static void Main()
    {
        A a1 = new A(0);
        if (a1) 
          Console.WriteLine("a1 is true");
        else
          Console.WriteLine("a1 is false");

        A a2 = new A(42);
        if (a2) 
          Console.WriteLine("a2 is true");
        else
          Console.WriteLine("a2 is false");
    }
}

Output:

a1 is false
a2 is true
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top