Three-value logic class in C #
The actual application value of the three-value logic has not been ignored. In the vast majority of books that introduce relational database knowledge, the discussion of NULL values is related, and it is less than three-value logic. In the MSDN, a three-value logical structure (STRUCT) implemented with C # is given, and the application layer provides a three-value logical operation function. Related article reposted as follows:
C # language specification
11.4.2 Database Boolean Type
The DBBool struct below implements a three-valued logical type. The possible values of this type are DBBool.True, DBBool.False, and DBBool.Null, where the Null member indicates an unknown value. Such three-valued logical types are commonly used IN DATABASES.
Using system;
Public struct dbbool
{
// The Three Possible Dbbool Values.
Public Static Readonly dbbool null = new dbbool (0);
Public Static Readonly Dbbool False = New DbboOL (-1);
Public Static Readonly Dbbool True = New DbboOL (1);
// Private Field That Stores -1, 0, 1 for False, Null, True.
Sbyte value;
// private instance constructor. The value parameter must be -1, 0, or 1.
Dbbool (int value) {
THIS.VALUE = (sbyte) Value;
}
// Properties to Examine The Value of a dbbool. Return True IF this
// Dbbool Has The Given Value, False Otherwise.
Public Bool Isnull {Get {Return Value == 0;}}
Public Bool IsFalse {Get {Return Value <0;}}
Public Bool Istrue {Get {Return Value> 0;}}
// Implicit Conversion from Bool To Dbbo 市 马ps true to dbbool.true and
// false to dbbool.false.
Public static implicit operator dbubool (bool x) {
RETURN X? TRUE: FALSE;
}
// Explicit Conversion from dbbool to bool. Throws an exception if The
// Given Dbbool is Null, OtherWise Returns True Or False.
Public Static Explicit Operator Bool (dbbool x) {
IF (x.value == 0) throw new invalidopertyleException ();
Return X.Value> 0;
}
// Equality Operator. Returns Null if Either Operand is Null, Otherwise // Returns True OR FALSE.
Public Static Dbbool Operator == (dbbool x, dbbool y) {
IF (x.value == 0 || y.value == 0) Return NULL;
Return x.value == y.value? True: false;
}
// inequality operator. Returns Null if Either Operand Is Null, Otherwise
// Returns True or false.
Public Static Dbbool Operator! = (dbbool x, dbbool y) {
IF (x.value == 0 || y.value == 0) Return NULL;
Return X.Value! = y.value? True: false;
}
// Logical Negation Operator. Returns True if The Operand Is False, NULL
// if the operand is null, or false if the operand is true.
Public Static Dbbool Operator! (dbbool x) {
Return new dbbool (-x.value);
}
// logical and operator. Returns False if Either Operand is False,
// OtherWise Null IF Either Operand is Null, OtherWise True.
Public Static Dbbool Operator & (DBBool X, DBBOOL Y) {
Return New Dbbool (X.Value } // logical or operator. Returns True if Either Operand Is True, Otherwise // Null if Either Operand is Null, OtherWise False. Public Static Dbbool Operator | (dbbool x, dbbool y) { Return new dbbool (x.value> y.value? x.Value: y.value); } // Definitely True Operator. Returns True If the Operand Is True, False // Otherwise. Public Static Bool Operator True (dbbool x) { Return X.Value> 0; } // definitely false operator. Returns True If the Operand Is False, False // Otherwise. Public Static Bool Operator False (dbbool x) { Return X.Value <0; } Public Override Bool Equals (Object Obj) { IF (! (OBJ Is DbboOL) Return False; Return Value == (DbBool) .value; } Public override int getHashcode () {Return Value; } Public Override string toString () { IF (Value> 0) Return "dbbool.true"; IF (Value <0) Return "DBBOOL.FALSE"; Return "dbubool.null"; } } Send Feedback on this Topic to Microsoft © Microsoft Corporation. All Rights Reserved. From the article content we can see that it uses the third algorithm we described earlier. This example has built a nice framework, in addition to, or the operation, also includes the necessary type of conversion, comparison, and the necessary GetHashCode and Tositring methods in .NET CLR. When we face this simple code with the mentality of beginners, there are several places worth learning: Inside the structure, three different logic states are denoted by -1, 0, and 1. And through the three constants of false, null, true, all possible values that may be used. This value and logic correspond to people's conventional thinking habits and mathematical aesthetics. It is also convenient to implement the GetHashCode method. Using internal values, concise and beautiful implementation with / or / non-computational. If you are in the three logical algorithms we mentioned in front, it is not so beautiful. Maybe this is the reason why there are many relational databases to choose this algorithm. Aesthetics, a very important thing in the mathematical system. It provides istrue, isfalse, isnull judgment, which is very convenient to use. When the three-value logic is converted to the two-value logic and DBNULL, the transplicit must be displayed, and vice versa only implicit conversions. Implement True and False operators. The GetHashCode and Tostring methods required for .NET CLR are overloaded. When we work in a specific environment, we should follow the requirements and conventions of the environment, which is often ignored when actually developed. In order to meet the needs of actual use, I have been extended to this class. Mainly as follows: Transformation with DBNULL types (to consider the type conversion exception). The parsing method PARSE from the string to the tri-value logic (accordingly, the toString () method changes). Added new constructor. Added and / or operations supporting the other two logical arithmetic systems. Added the conversion function Todbbolean used to assign values to the database logic field. The new code is as follows: Using system; Namespace march.vboolean {/// /// Bool with Three Support System.dbnull. /// public struct Boolw3 {// The three possible Boolw3 values public static readonly Boolw3 Null = new Boolw3 (0);. public static readonly Boolw3 False = new Boolw3 (-1); public static readonly Boolw3 True = new Boolw3 (1); / / Private Field That Stores -1, 0, 1 for False, Null, True. Sbyte Value; // Private Instance Construction. The Value Parameter Must Be -1, 0, OR 1. Boolw3 (int value) {this.Value = (sbyte) value;} public boolw3 {this.value = value? (sbyte) 1: (sbyte) -1;} public boolw3 (dbnull value) {this.Value = (sbyte) 0;} // / /// Construct instances from the logical field value of the database components /// can only be used for System.Boolean or DBNULL type. Public Boolw3 (Object Value) {if (null == value) throw new argumentException ("The value or dbnull!"); if (value.gettype () == typeof (bool) {THIS.VALUE = (bool) value? (sbyte) 1; return;}}} (value.gettype () == typeof (dbnull) {this.Value = (sbyte) 0; return;} throw new argumentException ("The Value Must In True, False Or DBNULL!");} // /// Analyzes the value from the string. // // / Optional value is likely to have "True", "false", "false", "null" public static boolw3 parse (Switch (Value) {CASE " Boolw3.true ": Case" true ": {re.Value = (sbyte) 1; break;} code" boolw3.false ": Case" false ": {r.Value = (sbyte) -1; break;} case "Boolw3.Null": Case "null": {Re.Value = (sbyte) 0; break;} default: throw new argumentException ("The value must in /"boolw3.true/", /"boolw3.false/ " , / "Boolw3.Null /", / "True /", / "False /" OR / "NULL /"! ");} Return Re; // Boolw3 Has The Given Value, False Otherwise. Public Bool Isnull {Get {Return Value == 0;}} PUBLIC BOOL ISFALSE {Get {Return Value <0;}} public bool istrue {get {return value> 0;} } // imp // false to Boolw3.false. Public static impLicit Operator Boolw3.false. Public static implicit operator boolw3 (bool x) {return X? True: false;} public static implicit Operator Boolw3 (DBNULL X) {Return NULL;} // Explicit conversion from Boolw3 to bool.Throws an exception if the // given Boolw3 is Null, otherwise returns true or false. Public static explicit operator bool (Boolw3 x) {if (x.value == 0) throw new InvalidOperationException ( Return X.Value> 0;} Public Static Explicit Operator DBNULL (Boolw3 x) {if (x.value! = 0) throw new invalidopertyleException (); return dbnull.value;} // Equality operator. Returns Null if Either Operand is Null, Otherwise // Returns True or false. Public static boolw3 operator == (Boolw3 x, boolw3 y) {if (x.value == 0 || y.Value == 0) return null; returnix x.value == y.Value? True: false;} // inequality operator. Returns null if Either operand is null, Otherwise // Returns True or false. Public static boolw3 operator! = (Boolw3 x Boolw3 y) {if (x.value == 0 || y.value == 0) Return null; return x.value! = Y.Value? True: false;} // logical negation operator. Returns True if The Operand is false, null // if the operand is null, or false if the operand is true. public static boolw3 operator! (boolw3 x) {return new boolw3 (-x.value);} // logical and operator. Returns False If Either OPERAND IS FALSE, // Otherwise Null IF Either OPERAND IS NULL, OTHERWISE TRUE. Public Static Boolw3 Operator & (Boolw3 x, Boolw3 Y) {RETURN New Boolw3 (x.value Return New Boolw3 (X.Value> Y.Value? x.value: y.value); /// /// verifying is actually a logical and operation with a NULL value as the lowest priority. Usually used to verify data validity. /// There is at least one of the two operands to return false, otherwise, at least one True is TRUE, no // returns NULL. ////// left operation number /// Right Operation //// The result of the operation is BOOLW3 type Public Static Boolw3 Verify (Boolw3 x, Boolw3 Y) {if (x.value == -1 || y.value == -1) Return False; if (x.value == 1 || y.value == 1 Return True; Return Null; /// /// verify actually a logical or operation with a NULL value as the lowest priority. Usually used to verify data validity. The /// There is at least one of the two operands to return True, otherwise, at least one of the FALSE is returned to false, no // returns NULL. /// /// Left operation number /// Right Operation /// The result of the operation is BOOLW3 type Public Static Boolw3 Verifyor (Boolw3 x, Boolw3 Y) {if (x.value == 1 || y.Value == 1) Return true; if (x.value == -1 & y.value == -1) Return false;} ///// DBAND is logical and operation with a NULL value as the highest value, common in some database platforms. When there is /// null in the operand, the return value is NULL, and the other is the same as the binary logic. // /// Left operation number /// Right Operation ///// The result of the operation is BOOLW3 type Public Static Boolw3 DBAND (Boolw3 x, Boolw3 Y) {IF (x.value == 0 || y.Value == 0) Return Null; Return New Boolw3 (X.Value /// /// DBOR is a logic or operation with a NULL value as the highest value, common in some database platforms. When there is /// null in the operand, the return value is NULL, and the other is the same as the binary logic. /// /// Left operation number /// Right Operation /// The result of the operation is BOOLW3 type Public Static Boolw3 Dbor (Boolw3 x, Boolw3 Y) {if (x.value == 0 || y.value == 0) Return Null; Return New Boolw3 (x.value> y.value? x.Value: y. Value);} // definitely true operator. Returns true if the operand is true, false // Otherwise. public static bool operator true (boolw3 x) {return x.Value> 0;} // definitely false operator. Returns True IF The operand is false, false // Otherwise. public static bool Operator false (boolw3 x) {returnx x.Value <0;} public override bool equals (Object obj) {if (! (ipj is boolw3)) Return False; Return Value == ((Boolw3) obj) .value;} public override int getHashcode () {return value;} public override string toString () {if (value> 0) Return "Boolw3.True"; if (Value <0) Return "Boolw3.false"; return "boxw3.null";} /// // Used to assign a value of the Boolean type (such as a sqldbtype.bit) field to the database access component. /// Returns an Object object that may be True, False or DBNULL.VALUE Public Object Todbbolean () {return (value == 0)? (object) dbnull.value: (value> 0);}}} The above code can meet the needs, but there is still a further transformation as follows: Three logical systems should be placed in the same type, which is not coordinated, affecting code consistency and our routine usage habits. The universal code should be unified to be a base class, and the implementation of / or operation is specified in the form of an abstract method or interface. Implemented in three different subclasses. The display type conversion between different subclasses is implemented in subclasses. In order to appear unnecessary strong coupling, the implicit type conversion of the subclass can be implemented (due to the characteristics of the OO language, this is valid) and the explicit type conversion of the base class to subclats (this You can directly inherit the realization by the subclass by defining a fumble function in the base class.