Overloading true and false


The keywords true and false can also be used as unary operators for the purposes of overloading. Overloaded versions of these operators provide custom determinations of true and false relative to classes that you create. Once true and false are implemented for a class, you can use objects of that class to control the if, while, for, and do-while statements, or in a ? expression. You can even use them to implement special types of logic, such as fuzzy logic.

The true and false operators must be overloaded as a pair. You cannot overload just one. Both are unary operators and they have this general form:

 public static bool operator true(param-type operand) {    // return true or false } public static bool operator false(param-type operand) {    // return true or false }

Notice that each returns a bool result.

The following example shows how true and false can be implemented for the ThreeD class. Each assumes that a ThreeD object is true if at least one coordinate is non-zero. If all three coordinates are zero, then the object is false. The decrement operator is also implemented for the purpose of illustration.

 // Overload true and false for ThreeD. using System; // A three-dimensional coordinate class. class ThreeD {   int x, y, z; // 3-D coordinates   public ThreeD() { x = y = z = 0; }   public ThreeD(int i, int j, int k) { x = i; y = j; z = k; }   // Overload true.   public static bool operator true(ThreeD op) {     if((op.x != 0) || (op.y != 0) || (op.z != 0))       return true; // at least one coordinate is non-zero     else       return false;   }   // Overload false.   public static bool operator false(ThreeD op) {     if((op.x == 0) && (op.y == 0) && (op.z == 0))       return true; // all coordinates are zero     else       return false;   }   // Overload unary --.   public static ThreeD operator --(ThreeD op)   {     // for ++, modify argument     op.x--;     op.y--;     op.z--;     return op;   }   // Show X, Y, Z coordinates.   public void show()   {     Console.WriteLine(x + ", " + y + ", " + z);   } } class TrueFalseDemo {   public static void Main() {     ThreeD a = new ThreeD(5, 6, 7);     ThreeD b = new ThreeD(10, 10, 10);     ThreeD c = new ThreeD(0, 0, 0);     Console.Write("Here is a: ");     a.show();     Console.Write("Here is b: ");     b.show();     Console.Write("Here is c: ");     c.show();     Console.WriteLine();     if(a) Console.WriteLine("a is true.");     else Console.WriteLine("a is false.");     if(b) Console.WriteLine("b is true.");     else Console.WriteLine("b is false.");     if(c) Console.WriteLine("c is true.");     else Console.WriteLine("c is false.");     Console.WriteLine();     Console.WriteLine("Control a loop using a ThreeD object.");     do {       b.show();       b--;     } while(b);   } }

The output is shown here:

 Here is a: 5, 6, 7 Here is b: 10, 10, 10 Here is c: 0, 0, 0 a is true. b is true. c is false. Control a loop using a ThreeD object. 10, 10, 10 9, 9, 9 8, 8, 8 7, 7, 7 6, 6, 6 5, 5, 5 4, 4, 4 3, 3, 3 2, 2, 2 1, 1, 1

Notice how the ThreeD objects are used to control if statements and a do-while loop. In the case of the if statements, the ThreeD object is evaluated using true. If the result of this operation is true, then the if statement succeeds. In the case of the do-while loop, each iteration of the loop decrements b. The loop repeats as long as b evaluates as true (that is, it contains at least one non-zero coordinate). When b contains all zero coordinates, it evaluates as false when the true operator is applied and the loop stops.




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net