When I run this bit of code, Equation(10, 20)
is output to the console:
public class Equation
{
public int a;
public int b;
public override string ToString()
{ return "Equation(" + a + ", " + b + ")"; }
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(new Equation() { a = 10, b = 20 });
Console.ReadLine();
}
}
I'd like to support Equation
instances being used in the test of an if
so I allowed for implicit conversion to Boolean
:
public class Equation
{
public int a;
public int b;
public override string ToString()
{ return "Equation(" + a + ", " + b + ")"; }
public static implicit operator Boolean(Equation eq)
{ return eq.a == eq.b; }
}
class Program
{
static void Main(string[] args)
{
if (new Equation() { a = 10, b = 10 })
Console.WriteLine("equal");
Console.WriteLine(new Equation() { a = 10, b = 20 });
Console.ReadLine();
}
}
However, the trouble is, now when I use WriteLine
on an Equation
, it get's converted to a Boolean
instead of printing using ToString
.
How can I allow for implicit conversion to Boolean
and still have WriteLine
display using ToString
?
update
This question is inspired by the Equation
class in SymbolicC++. The code below illustrates that an Equation
can be displayed via cout
as well as used in the test of an if
:
auto eq = x == y;
cout << eq << endl;
if (eq)
cout << "equal" << endl;
else
cout << "not equal" << endl;
So this is somehow possible in C++.
ToString() Method. This method is used to convert the value of this instance to its equivalent string representation i.e. either “True” or “False”.
You can include that bit in an If clause. If(**** = 1,True,False) or If(**** = 0,True,False).
ToBoolean(Object) Converts the value of a specified object to an equivalent Boolean value. ToBoolean(Decimal) Converts the value of the specified decimal number to an equivalent Boolean value.
You can't, as far as I can tell. You could also provide a conversion to string... but that would then make the call ambiguous between WriteLine(string)
and WriteLine(
bool`).
Personally I'd strongly recommend that you ditch the implicit conversion to Boolean
. Implicit conversions are almost always a bad idea. They make the code far more confusing, as well as leading to unintended overload changes as you've found.
(I'd also change your bracing style, but that's a different matter.)
You can't do it with a bool
conversion but you can overload the true
and false
operators for Equation
. Of course Equation
won't be implicitly convertible to bool
any more but you can still use it in if
, while
, do
, and for
statements and conditional expressions (i.e. ?:
operator).
public class Equation
{
public int a;
public int b;
public override string ToString()
{ return "Equation(" + a + ", " + b + ")"; }
public static bool operator true(Equation eq)
{
return eq.a == eq.b;
}
public static bool operator false(Equation eq)
{
return eq.a != eq.b;
}
}
From your example:
if (new Equation() { a = 10, b = 10 })
Console.WriteLine("equal"); // prints "equal"
Console.WriteLine(new Equation() { a = 10, b = 20 }); // prints Equation(10, 20)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With