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 DBBool. Maps true to DBBool.True and
// false to DBBool.False.
public static implicit operator DBBool(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 InvalidOperationException();
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 < y.value? x.value: y.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)obj).value;
}
public override int GetHashCode() {
return value;
}
public override string ToString() {
if (value > 0) return "DBBool.True";
if (value < 0) return "DBBool.False";
return "DBBool.Null";
}
}
namespace March.VBoolean
{
/// <summary>
/// 三值邏輯類(Bool with three),支援System.DBNull。
/// </summary>
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 constructor. The value parameter must be –1, 0, or 1.
Boolw3(int value)
{
this.value = (sbyte)value;
}
public Boolw3(bool value)
{
this.value = value? (sbyte)1:(sbyte)-1;
}
public Boolw3(DBNull value)
{
this.value = (sbyte)0;
}
/// <summary>
/// 從資料庫組件的邏輯欄位值中構造執行個體
/// </summary>
/// <param name="?">只能為System.Boolean或DBNull類型。</param>
public Boolw3(object value)
{
if(null == value)
throw new ArgumentException("The value must in true, false or DBNull!");
if(value.GetType() == typeof(bool))
{
this.value = (bool)value?(sbyte)1:(sbyte)-1;
return;
}
if(value.GetType() == typeof(DBNull))
{
this.value = (sbyte)0;
return;
}
throw new ArgumentException("The value must in true, false or DBNull!");
}
/// <summary>
/// 從字串解析值。
/// </summary>
/// <param name="value">可選值為可能帶有限定名"Boolw3"的"True"、"False"、"Null"</param>
public static Boolw3 Parse(string value)
{
Boolw3 Re = Null;
switch(value)
{
case "Boolw3.True":
case "True" :
{
Re.value = (sbyte)1;
break;
}
case "Boolw3.False":
case "False":
{
Re.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;
}
// Properties to examine the value of a Boolw3. Return true if this
// 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; } }
// Implicit conversion from bool to Boolw3. Maps true to Boolw3.True and
// false to 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 InvalidOperationException();
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;
return 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 < y.value? x.value: y.value);
}
// Logical OR operator. Returns True if either operand is True, otherwise
// Null if either operand is Null, otherwise False.
public static Boolw3 operator |(Boolw3 x, Boolw3 y)
{
return new Boolw3(x.value > y.value? x.value: y.value);
}