三值逻辑运算(C#)

一般来说,在C#对数据库程序设计时变量的类型要和数据库中的字段类型相对应,即需要做变量的数据类型与字段的数据类型映射(数据库中的数据类型和c#的数据类型的映射表),例如string类型对应与Varchar(char)类型。

      但是对于数据库中的Bit类型,就无法使用C#中的Boolean值进行对应,原因是Bit型会有三种状态(0,1,null)而Boolean型只能是(True,False),非真即假的状态让Boolean类型无法与数据库中的null对应起来,所以在使用Bit类型转换成C#类型是就需要使用自定义的“三值逻辑”,也就是自己设定一个struct类型。

      所以,以下是笔者在晚上找到并略微修改的实现“三值逻辑运算”的C#代码,该代码实现了三种状态(DBNull,False,True),和网上相关代码不同是将DBNull的值设为-1,而将False的值设置成0.笔者认为这样更符合数据库中Bit类型存储的规律。在数据库操作语句的时候:0代表False,1代表True,这更符合Bit类型在数据库中的值的存储方式。

      以下是代码部分:

 

Code
  1using System;
  2using System.Collections.Generic;
  3using System.Text;
  4
  5namespace Chemmu.Dev.BaseTools
  6{
  7    public struct dbbool
  8    {
  9        //与数据库中Bit类型对应的三种状态
 10        public static readonly dbbool Null = new dbbool(-1);
 11        public static readonly dbbool False = new dbbool(0);
 12        public static readonly dbbool True = new dbbool(1);
 13        public sbyte value;
 14
 15        初始化#region 初始化
 16        private dbbool(int value)
 17        {
 18            this.value = (sbyte)value;
 19        }
 20
 21        public dbbool(bool value)
 22        {
 23            this.value = (value ? (sbyte)1 : (sbyte)0);
 24        }
 25
 26        public dbbool(DBNull value)
 27        {
 28            this.value = (sbyte)-1;
 29        }
 30
 31        public dbbool(object value)
 32        {
 33            if (value == null)
 34            {
 35                throw new ArgumentException("the value must in true, false or dbnull!");
 36            }
 37            if (value.GetType() == typeof(bool))
 38            {
 39                this.value = ((bool)value ? (sbyte)1 : (sbyte)0);
 40                return;
 41            }
 42            if (value.GetType() == typeof(DBNull))
 43            {
 44                this.value = (sbyte)-1;
 45                return;
 46            }
 47            throw new ArgumentException("the value must in true, false or dbnull!");
 48        }
 49        #endregion
 50
 51        /**//// <summary>
 52        /// 显示类型转换
 53        /// </summary>
 54        /// <param name="value"></param>
 55        /// <returns></returns>
 56        public static dbbool Prase(string value)
 57        {
 58            switch (value)
 59            {
 60                case "True":
 61                    return true;
 62                case "False":
 63                    return false;
 64                case "Null":
 65                    return null;
 66                default:
 67                    throw new ArgumentException("The value must in \"True\", \"False\" or \"Null\"!");
 68            }
 69        }
 70
 71        /**//// <summary>
 72        /// 判断是否为空
 73        /// </summary>
 74        public bool IsNull { get { return value == -1; } }
 75        /**//// <summary>
 76        /// 判断是否为True
 77        /// </summary>
 78        public bool IsTrue { get { return value == 1; } }
 79        /**//// <summary>
 80        /// 判断是否为False
 81        /// </summary>
 82        public bool IsFalse { get { return value == 0; } }
 83
 84        dbbool类型与bool、DBNull类型之间的转换#region dbbool类型与bool、DBNull类型之间的转换
 85        public static implicit operator dbbool(bool x)
 86        {
 87            return x ? True : False;
 88        }
 89
 90        public static implicit operator dbbool(DBNull x)
 91        {
 92            return Null;
 93        }
 94
 95        public static explicit operator bool(dbbool x)
 96        {
 97            if (x.value == -1) throw new InvalidOperationException();
 98            return x.value > 0;
 99        }
100
101        public static explicit operator DBNull(dbbool x)
102        {
103            if (x.value != -1) throw new InvalidOperationException();
104            return DBNull.Value;
105        }
106        #endregion
107
108        逻辑运算#region 逻辑运算
109        public static dbbool operator ==(dbbool x, dbbool y)
110        {
111            if (x.value == 0 || y.value == 0) return Null;
112            return x.value == y.value ? True : False;
113        }
114
115        public static dbbool operator !=(dbbool x, dbbool y)
116        {
117            if (x.value == 0 || y.value == 0) return Null;
118            return x.value != y.value ? True : False;
119        }
120
121        public static dbbool operator !(dbbool x)
122        {
123            if (x.value == 0)
124                return new dbbool(x.value + 1);
125            if (x.value == 1)
126                return new dbbool(x.value - 1);
127            return new dbbool(x.value);
128        }
129        #endregion
130
131        public override bool Equals(object obj)
132        {
133            return base.Equals(obj);
134        }
135
136        public override int GetHashCode()
137        {
138            return this.value;
139        }
140
141        public override string ToString()
142        {
143            if (value > 0) return "True";
144            if (value < 0) return "False";
145            return "Null";
146        }
147    }
148} 

当然,可以根据该算法的基础上增加工厂模式,并扩展出比如“四值逻辑元素”等。

 

posted @ 2012-08-24 10:49  Star★  阅读(693)  评论(0编辑  收藏  举报