You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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();
}
}
The text was updated successfully, but these errors were encountered:
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
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
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 ?
The text was updated successfully, but these errors were encountered: