Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overloading 'AND' and 'OR' Operator in C# gives an Exception #54

Open
Peter4Git opened this issue Jan 22, 2019 · 2 comments
Open

Overloading 'AND' and 'OR' Operator in C# gives an Exception #54

Peter4Git opened this issue Jan 22, 2019 · 2 comments

Comments

@Peter4Git
Copy link

I treid to overload the 'And' Operator in MyClass (C#). Flee gives me the following Exception:

"AndOrElement: Operation 'And' is not defined for types 'MyClass' and 'MyClass'"

Overloading the '+' and'-' Operator works well.

how can I overload the 'AND' 'OR' Operators in an C# Class ?

class Program
{
    static void Main(string[] args)
    {
        ExpressionContext context = new ExpressionContext();
        VariableCollection variables = context.Variables;

        variables.Add("a", new MyClass("1"));
        variables.Add("b", new MyClass("2"));

        try
        {
            Console.WriteLine( context.CompileDynamic("a + b").Evaluate() ); // works well 
            Console.WriteLine( context.CompileDynamic("a - b").Evaluate() ); // this too

            // following gives me an exception:
            // "AndOrElement: Operation 'And' is not defined for types 'MyClass' and 'MyClass'" 
            Console.WriteLine(context.CompileDynamic("a and b").Evaluate()); 
            Console.WriteLine(context.CompileDynamic("a or b").Evaluate()  );
        }

        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }

    }
}

public class MyClass
{
    private string value;

    public MyClass(String v)
    {
        value = v;
    }

    public bool ToBool()
    {
        if (value == "0") return false;
        if (value == "") return false;
        return true;
    }

    public int ToInt()
    {
        return Convert.ToInt32(value);
    }

    public static bool operator &(MyClass c1, MyClass c2)
    {
        return c1.ToBool() && c2.ToBool();
    }

    public static bool operator |(MyClass c2, MyClass c1)
    {
        return c1.ToBool() || c2.ToBool();
    }

    public static int operator +(MyClass c2, MyClass c1)
    {
        return c1.ToInt() + c2.ToInt();
    }

    public static int operator -(MyClass c2, MyClass c1)
    {
        return c1.ToInt() - c2.ToInt();
    }
}
@Peter4Git Peter4Git changed the title Overloading 'AND' and 'OR' Oprator in C# gives an Exception Overloading 'AND' and 'OR' Operator in C# gives an Exception Jan 22, 2019
@Doc-Saintly
Copy link

I could also use an answer for this. I guess it is because of the same reason: I need to use Excel-like syntax. or(true,false) instead of true or false

@hunkydoryrepair
Copy link
Contributor

I believe somebody has added support for overloading AND/OR and there is an active pull request on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants