From: Michael D. <mik...@us...> - 2004-11-12 22:08:36
|
Update of /cvsroot/nhibernate/NHibernateContrib/src/Nullables In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21720/src/Nullables Added Files: .cvsignore AssemblyInfo.cs INullableType.cs NullableBoolean.cs NullableByte.cs NullableDateTime.cs NullableDecimal.cs NullableDouble.cs NullableGuid.cs NullableInt16.cs NullableInt32.cs NullableInt64.cs Nullables-1.1.csproj Nullables.build NullableSingle.cs Log Message: NH-15: created a NHibernateContrib folder in cvs --- NEW FILE: .cvsignore --- bin obj .#* *.user *.xsx --- NEW FILE: NullableInt16.cs --- using System; namespace Nullables { [System.ComponentModel.TypeConverter(typeof(Nullables.TypeConverters.NullableInt16Converter)), Serializable()] public struct NullableInt16 : INullableType { public static readonly NullableInt16 Default = new NullableInt16(); Int16 _value; bool hasValue; #region Constructors public NullableInt16(Int16 value) { _value = value; hasValue = true; } #endregion #region INullableType Members object INullableType.Value { get { return Value; } } public bool HasValue { get { return hasValue; } } #endregion public Int16 Value { get { return _value; } } #region Casts public static explicit operator Int16(NullableInt16 nullable) { if (!nullable.HasValue) throw new NullReferenceException(); return nullable.Value; } public static implicit operator NullableInt16(Int16 value) { return new NullableInt16(value); } public static implicit operator NullableInt16(DBNull value) { return NullableInt16.Default; } #endregion public override string ToString() { if (HasValue) return Value.ToString(); else return string.Empty; } public override bool Equals(object obj) { if (obj is DBNull && !HasValue) return true; else if (obj is NullableInt16) return Equals((NullableInt16)obj); else return false; //if this is reached, it is either some other type, or DBnull is compared with this and we have a Value. } public bool Equals(NullableInt16 x) { return Equals(this, x); } public static bool Equals(NullableInt16 x, NullableInt16 y) { if (x.HasValue != y.HasValue) //one is null return false; else if (x.HasValue) //therefor y also HasValue return x.Value == y.Value; else //both are null return true; } public static bool operator ==(NullableInt16 x, NullableInt16 y) { return x.Equals(y); } public static bool operator ==(NullableInt16 x, object y) { return x.Equals(y); } public static bool operator !=(NullableInt16 x, NullableInt16 y) { return !x.Equals(y); } public static bool operator !=(NullableInt16 x, object y) { return !x.Equals(y); } public static NullableInt32 operator +(NullableInt16 x, NullableInt16 y) { if (!x.HasValue || !y.HasValue) //one or both are null return NullableInt32.Default; return new NullableInt32(x.Value + y.Value); } public static NullableInt32 operator -(NullableInt16 x, NullableInt16 y) { if (!x.HasValue || !y.HasValue) //one or both are null return NullableInt32.Default; return new NullableInt32(x.Value - y.Value); } public static NullableInt32 operator *(NullableInt16 x, NullableInt16 y) { if (!x.HasValue || !y.HasValue) //one or both are null return NullableInt32.Default; return new NullableInt32(x.Value * y.Value); } public static NullableInt32 operator /(NullableInt16 x, NullableInt16 y) { if (!x.HasValue || !y.HasValue) //one or both are null return NullableInt32.Default; return new NullableInt32(x.Value / y.Value); } public override int GetHashCode() { if (HasValue) return Value.GetHashCode(); else return 0; //GetHashCode() doesn't garantee uniqueness, and neither do we. } } } --- NEW FILE: NullableDateTime.cs --- using System; namespace Nullables { [System.ComponentModel.TypeConverter(typeof(Nullables.TypeConverters.NullableDateTimeConverter)), Serializable()] public struct NullableDateTime : INullableType { public static readonly NullableDateTime Default = new NullableDateTime(); DateTime _value; bool hasValue; #region Constructors public NullableDateTime(DateTime value) { _value = value; hasValue = true; } #endregion #region INullable Members object INullableType.Value { get { return Value; } } public bool HasValue { get { return hasValue; } } #endregion public DateTime Value { get { return _value; } } #region Casts public static explicit operator DateTime(NullableDateTime nullable) { if (!nullable.HasValue) throw new NullReferenceException(); return nullable.Value; } public static implicit operator NullableDateTime(DateTime value) { return new NullableDateTime(value); } public static implicit operator NullableDateTime(DBNull value) { return NullableDateTime.Default; } #endregion public override string ToString() { if (HasValue) return Value.ToString(); else return string.Empty; } public override bool Equals(object obj) { if (obj is DBNull && !HasValue) return true; else if (obj is NullableDateTime) return Equals((NullableDateTime)obj); else return false; //if this is reached, it is either some other type, or DBnull is compared with this and we have a Value. } public bool Equals(NullableDateTime x) { return Equals(this, x); } public static bool Equals(NullableDateTime x, NullableDateTime y) { if (x.HasValue != y.HasValue) //one is null return false; else if (x.HasValue) //therefor y also HasValue return x.Value == y.Value; else //both are null return true; } public static bool operator ==(NullableDateTime x, NullableDateTime y) { return x.Equals(y); } public static bool operator ==(NullableDateTime x, object y) { return x.Equals(y); } public static bool operator !=(NullableDateTime x, NullableDateTime y) { return !x.Equals(y); } public static bool operator !=(NullableDateTime x, object y) { return !x.Equals(y); } public override int GetHashCode() { if (HasValue) return Value.GetHashCode(); else return 0; //GetHashCode() doesn't garantee uniqueness, and neither do we. } //TODO: Operators for DateTime (?) } } --- NEW FILE: NullableByte.cs --- using System; namespace Nullables { [System.ComponentModel.TypeConverter(typeof(Nullables.TypeConverters.NullableByteConverter)), Serializable()] public struct NullableByte : INullableType { public static readonly NullableByte Default = new NullableByte(); Byte _value; bool hasValue; #region Constructors public NullableByte(Byte value) { _value = value; hasValue = true; } #endregion #region INullable Members object INullableType.Value { get { return Value; } } public bool HasValue { get { return hasValue; } } #endregion public Byte Value { get { return _value; } } #region Casts public static explicit operator Byte(NullableByte nullable) { if (!nullable.HasValue) throw new NullReferenceException(); return nullable.Value; } public static implicit operator NullableByte(Byte value) { return new NullableByte(value); } public static implicit operator NullableByte(DBNull value) { return NullableByte.Default; } #endregion public override string ToString() { if (HasValue) return Value.ToString(); else return string.Empty; } public override bool Equals(object obj) { if (obj is DBNull && !HasValue) return true; else if (obj is NullableByte) return Equals((NullableByte)obj); else return false; //if this is reached, it is either some other type, or DBnull is compared with this and we have a Value. } public bool Equals(NullableByte x) { return Equals(this, x); } public static bool Equals(NullableByte x, NullableByte y) { if (x.HasValue != y.HasValue) //one is null return false; else if (x.HasValue) //therefor y also HasValue return x.Value == y.Value; else //both are null return true; } public static bool operator ==(NullableByte x, NullableByte y) { return x.Equals(y); } public static bool operator ==(NullableByte x, object y) { return x.Equals(y); } public static bool operator !=(NullableByte x, NullableByte y) { return !x.Equals(y); } public static bool operator !=(NullableByte x, object y) { return !x.Equals(y); } public static NullableInt32 operator +(NullableByte x, NullableByte y) { if (!x.HasValue || !y.HasValue) //one or both are null return NullableInt32.Default; return new NullableInt32(x.Value + y.Value); } public static NullableInt32 operator -(NullableByte x, NullableByte y) { if (!x.HasValue || !y.HasValue) //one or both are null return NullableInt32.Default; return new NullableInt32(x.Value - y.Value); } public static NullableInt32 operator *(NullableByte x, NullableByte y) { if (!x.HasValue || !y.HasValue) //one or both are null return NullableInt32.Default; return new NullableInt32(x.Value * y.Value); } public static NullableInt32 operator /(NullableByte x, NullableByte y) { if (!x.HasValue || !y.HasValue) //one or both are null return NullableInt32.Default; return new NullableInt32(x.Value / y.Value); } public override int GetHashCode() { if (HasValue) return Value.GetHashCode(); else return 0; //GetHashCode() doesn't garantee uniqueness, and neither do we. } } } --- NEW FILE: INullableType.cs --- using System; namespace Nullables { public interface INullableType { object Value { get; } bool HasValue { get; } } } --- NEW FILE: NullableDouble.cs --- using System; namespace Nullables { [System.ComponentModel.TypeConverter(typeof(Nullables.TypeConverters.NullableDoubleConverter)), Serializable()] public struct NullableDouble : INullableType { public static readonly NullableDouble Default = new NullableDouble(); Double _value; bool hasValue; #region Constructors public NullableDouble(Double value) { _value = value; hasValue = true; } #endregion #region INullable Members object INullableType.Value { get { return Value; } } public bool HasValue { get { return hasValue; } } #endregion public Double Value { get { return _value; } } #region Casts public static explicit operator Double(NullableDouble nullable) { if (!nullable.HasValue) throw new NullReferenceException(); return nullable.Value; } public static implicit operator NullableDouble(Double value) { return new NullableDouble(value); } public static implicit operator NullableDouble(DBNull value) { return NullableDouble.Default; } #endregion public override string ToString() { if (HasValue) return Value.ToString(); else return string.Empty; } public override bool Equals(object obj) { if (obj is DBNull && !HasValue) return true; else if (obj is NullableDouble) return Equals((NullableDouble)obj); else return false; //if this is reached, it is either some other type, or DBnull is compared with this and we have a Value. } public bool Equals(NullableDouble x) { return Equals(this, x); } public static bool Equals(NullableDouble x, NullableDouble y) { if (x.HasValue != y.HasValue) //one is null return false; else if (x.HasValue) //therefor y also HasValue return x.Value == y.Value; else //both are null return true; } public static bool operator ==(NullableDouble x, NullableDouble y) { return x.Equals(y); } public static bool operator ==(NullableDouble x, object y) { return x.Equals(y); } public static bool operator !=(NullableDouble x, NullableDouble y) { return !x.Equals(y); } public static bool operator !=(NullableDouble x, object y) { return !x.Equals(y); } public static NullableDouble operator +(NullableDouble x, NullableDouble y) { if (!x.HasValue || !y.HasValue) //one or both are null return NullableDouble.Default; return new NullableDouble(x.Value + y.Value); } public static NullableDouble operator -(NullableDouble x, NullableDouble y) { if (!x.HasValue || !y.HasValue) //one or both are null return NullableDouble.Default; return new NullableDouble(x.Value - y.Value); } public static NullableDouble operator *(NullableDouble x, NullableDouble y) { if (!x.HasValue || !y.HasValue) //one or both are null return NullableDouble.Default; return new NullableDouble(x.Value * y.Value); } public static NullableDouble operator /(NullableDouble x, NullableDouble y) { if (!x.HasValue || !y.HasValue) //one or both are null return NullableDouble.Default; return new NullableDouble(x.Value / y.Value); } public override int GetHashCode() { if (HasValue) return Value.GetHashCode(); else return 0; //GetHashCode() doesn't garantee uniqueness, and neither do we. } } } --- NEW FILE: Nullables.build --- (This appears to be a binary file; contents omitted.) --- NEW FILE: AssemblyInfo.cs --- using System.Reflection; using System.Runtime.CompilerServices; //------------------------------------------------------------------------------ // <autogenerated> // This code was generated by a tool. // Runtime Version: 1.1.4322.573 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. // </autogenerated> //------------------------------------------------------------------------------ [assembly: AssemblyTitleAttribute("Nullables for Microsoft .NET Framework 1.1")] [assembly: AssemblyDescriptionAttribute("A library of Nullable Primitive Types to serve as a bridge to .net-2.0.")] [assembly: AssemblyCompanyAttribute("nhibernate.sourceforge.net")] [assembly: AssemblyProductAttribute("Nullables")] [assembly: AssemblyCopyrightAttribute("Licensed under LGPL.")] [assembly: AssemblyVersionAttribute("0.4.0.0")] [assembly: AssemblyInformationalVersionAttribute("0.4")] [assembly: AssemblyFileVersionAttribute("0.4.0.0")] --- NEW FILE: NullableSingle.cs --- using System; namespace Nullables { [System.ComponentModel.TypeConverter(typeof(Nullables.TypeConverters.NullableSingleConverter)), Serializable()] public struct NullableSingle : INullableType { public static readonly NullableSingle Default = new NullableSingle(); Single _value; bool hasValue; #region Constructors public NullableSingle(Single value) { _value = value; hasValue = true; } #endregion #region INullable Members object INullableType.Value { get { return Value; } } public bool HasValue { get { return hasValue; } } #endregion public Single Value { get { return _value; } } #region Casts public static explicit operator Single(NullableSingle nullable) { if (!nullable.HasValue) throw new NullReferenceException(); return nullable.Value; } public static implicit operator NullableSingle(Single value) { return new NullableSingle(value); } public static implicit operator NullableSingle(DBNull value) { return NullableSingle.Default; } #endregion public override string ToString() { if (HasValue) return Value.ToString(); else return string.Empty; } public override bool Equals(object obj) { if (obj is DBNull && !HasValue) return true; else if (obj is NullableSingle) return Equals((NullableSingle)obj); else return false; //if this is reached, it is either some other type, or DBnull is compared with this and we have a Value. } public bool Equals(NullableSingle x) { return Equals(this, x); } public static bool Equals(NullableSingle x, NullableSingle y) { if (x.HasValue != y.HasValue) //one is null return false; else if (x.HasValue) //therefor y also HasValue return x.Value == y.Value; else //both are null return true; } public static bool operator ==(NullableSingle x, NullableSingle y) { return x.Equals(y); } public static bool operator ==(NullableSingle x, object y) { return x.Equals(y); } public static bool operator !=(NullableSingle x, NullableSingle y) { return !x.Equals(y); } public static bool operator !=(NullableSingle x, object y) { return !x.Equals(y); } public static NullableSingle operator +(NullableSingle x, NullableSingle y) { if (!x.HasValue || !y.HasValue) //one or both are null return NullableSingle.Default; return new NullableSingle(x.Value + y.Value); } public static NullableSingle operator -(NullableSingle x, NullableSingle y) { if (!x.HasValue || !y.HasValue) //one or both are null return NullableSingle.Default; return new NullableSingle(x.Value - y.Value); } public static NullableSingle operator *(NullableSingle x, NullableSingle y) { if (!x.HasValue || !y.HasValue) //one or both are null return NullableSingle.Default; return new NullableSingle(x.Value * y.Value); } public static NullableSingle operator /(NullableSingle x, NullableSingle y) { if (!x.HasValue || !y.HasValue) //one or both are null return NullableSingle.Default; return new NullableSingle(x.Value / y.Value); } public override int GetHashCode() { if (HasValue) return Value.GetHashCode(); else return 0; //GetHashCode() doesn't garantee uniqueness, and neither do we. } } } --- NEW FILE: NullableDecimal.cs --- using System; namespace Nullables { [System.ComponentModel.TypeConverter(typeof(Nullables.TypeConverters.NullableDecimalConverter)), Serializable()] public struct NullableDecimal : INullableType { public static readonly NullableDecimal Default = new NullableDecimal(); Decimal _value; bool hasValue; #region Constructors public NullableDecimal(Decimal value) { _value = value; hasValue = true; } #endregion #region INullable Members object INullableType.Value { get { return Value; } } public bool HasValue { get { return hasValue; } } #endregion public Decimal Value { get { return _value; } } #region Casts public static explicit operator Decimal(NullableDecimal nullable) { if (!nullable.HasValue) throw new NullReferenceException(); return nullable.Value; } public static implicit operator NullableDecimal(Decimal value) { return new NullableDecimal(value); } public static implicit operator NullableDecimal(DBNull value) { return NullableDecimal.Default; } #endregion public override string ToString() { if (HasValue) return Value.ToString(); else return string.Empty; } public override bool Equals(object obj) { if (obj is DBNull && !HasValue) return true; else if (obj is NullableDecimal) return Equals((NullableDecimal)obj); else return false; //if this is reached, it is either some other type, or DBnull is compared with this and we have a Value. } public bool Equals(NullableDecimal x) { return Equals(this, x); } public static bool Equals(NullableDecimal x, NullableDecimal y) { if (x.HasValue != y.HasValue) //one is null return false; else if (x.HasValue) //therefor y also HasValue return x.Value == y.Value; else //both are null return true; } public static bool operator ==(NullableDecimal x, NullableDecimal y) { return x.Equals(y); } public static bool operator ==(NullableDecimal x, object y) { return x.Equals(y); } public static bool operator !=(NullableDecimal x, NullableDecimal y) { return !x.Equals(y); } public static bool operator !=(NullableDecimal x, object y) { return !x.Equals(y); } public static NullableDecimal operator +(NullableDecimal x, NullableDecimal y) { if (!x.HasValue || !y.HasValue) //one or both are null return NullableDecimal.Default; return new NullableDecimal(x.Value + y.Value); } public static NullableDecimal operator -(NullableDecimal x, NullableDecimal y) { if (!x.HasValue || !y.HasValue) //one or both are null return NullableDecimal.Default; return new NullableDecimal(x.Value - y.Value); } public static NullableDecimal operator *(NullableDecimal x, NullableDecimal y) { if (!x.HasValue || !y.HasValue) //one or both are null return NullableDecimal.Default; return new NullableDecimal(x.Value * y.Value); } public static NullableDecimal operator /(NullableDecimal x, NullableDecimal y) { if (!x.HasValue || !y.HasValue) //one or both are null return NullableDecimal.Default; return new NullableDecimal(x.Value / y.Value); } public override int GetHashCode() { if (HasValue) return Value.GetHashCode(); else return 0; //GetHashCode() doesn't garantee uniqueness, and neither do we. } } } --- NEW FILE: NullableGuid.cs --- using System; using System.Runtime.Serialization; namespace Nullables { [System.ComponentModel.TypeConverter(typeof(Nullables.TypeConverters.NullableGuidConverter)), Serializable()] public struct NullableGuid : INullableType { public static readonly NullableGuid Default = new NullableGuid(); Guid _value; bool hasValue; #region Constructors public NullableGuid(Guid value) { _value = value; hasValue = true; } #endregion #region INullable Members object INullableType.Value { get { return Value; } } public bool HasValue { get { return hasValue; } } #endregion public Guid Value { get { return _value; } } #region Casts public static explicit operator Guid(NullableGuid nullable) { if (!nullable.HasValue) throw new NullReferenceException(); return nullable.Value; } public static implicit operator NullableGuid(Guid value) { return new NullableGuid(value); } public static implicit operator NullableGuid(DBNull value) { return NullableGuid.Default; } #endregion public override string ToString() { if (HasValue) return Value.ToString(); else return string.Empty; } public override bool Equals(object obj) { if (obj is DBNull && !HasValue) return true; else if (obj is NullableGuid) return Equals((NullableGuid)obj); else return false; //if this is reached, it is either some other type, or DBnull is compared with this and we have a Value. } public bool Equals(NullableGuid x) { return Equals(this, x); } public static bool Equals(NullableGuid x, NullableGuid y) { if (x.HasValue != y.HasValue) //one is null return false; else if (x.HasValue) //therefor y also HasValue return x.Value == y.Value; else //both are null return true; } public static bool operator ==(NullableGuid x, NullableGuid y) { return x.Equals(y); } public static bool operator ==(NullableGuid x, object y) { return x.Equals(y); } public static bool operator !=(NullableGuid x, NullableGuid y) { return !x.Equals(y); } public static bool operator !=(NullableGuid x, object y) { return !x.Equals(y); } public override int GetHashCode() { if (HasValue) return Value.GetHashCode(); else return 0; //GetHashCode() doesn't garantee uniqueness, and neither do we. } } } --- NEW FILE: Nullables-1.1.csproj --- <VisualStudioProject> <CSHARP ProjectType = "Local" ProductVersion = "7.10.3077" SchemaVersion = "2.0" ProjectGuid = "{9DEFA2EA-4A52-445A-8DA4-6531BD5D76B5}" > <Build> <Settings ApplicationIcon = "" AssemblyKeyContainerName = "" AssemblyName = "Nullables" AssemblyOriginatorKeyFile = "" DefaultClientScript = "JScript" DefaultHTMLPageLayout = "Grid" DefaultTargetSchema = "IE50" DelaySign = "false" OutputType = "Library" PreBuildEvent = "" PostBuildEvent = "" RootNamespace = "Nullables" RunPostBuildEvent = "OnBuildSuccess" StartupObject = "" > <Config Name = "Debug" AllowUnsafeBlocks = "false" BaseAddress = "285212672" CheckForOverflowUnderflow = "false" ConfigurationOverrideFile = "" DefineConstants = "DEBUG;TRACE" DocumentationFile = "" DebugSymbols = "true" FileAlignment = "4096" IncrementalBuild = "false" NoStdLib = "false" NoWarn = "" Optimize = "false" OutputPath = "bin\Debug\" RegisterForComInterop = "false" RemoveIntegerChecks = "false" TreatWarningsAsErrors = "false" WarningLevel = "4" /> <Config Name = "Release" AllowUnsafeBlocks = "false" BaseAddress = "285212672" CheckForOverflowUnderflow = "false" ConfigurationOverrideFile = "" DefineConstants = "TRACE" DocumentationFile = "" DebugSymbols = "false" FileAlignment = "4096" IncrementalBuild = "false" NoStdLib = "false" NoWarn = "" Optimize = "true" OutputPath = "bin\Release\" RegisterForComInterop = "false" RemoveIntegerChecks = "false" TreatWarningsAsErrors = "false" WarningLevel = "4" /> </Settings> <References> <Reference Name = "System" AssemblyName = "System" HintPath = "..\..\..\..\WINDOWS\Microsoft.NET\Framework\v1.1.4322\System.dll" /> </References> </Build> <Files> <Include> <File RelPath = "AssemblyInfo.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "INullableType.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "NullableBoolean.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "NullableByte.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "NullableDateTime.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "NullableDecimal.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "NullableDouble.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "NullableGuid.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "NullableInt16.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "NullableInt32.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "NullableInt64.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Nullables.build" BuildAction = "None" /> <File RelPath = "NullableSingle.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "TypeConverters\NullableBooleanConverter.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "TypeConverters\NullableByteConverter.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "TypeConverters\NullableDateTimeConverter.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "TypeConverters\NullableDecimalConverter.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "TypeConverters\NullableDoubleConverter.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "TypeConverters\NullableGuidConverter.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "TypeConverters\NullableInt16Converter.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "TypeConverters\NullableInt32Converter.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "TypeConverters\NullableInt64Converter.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "TypeConverters\NullableSingleConverter.cs" SubType = "Code" BuildAction = "Compile" /> </Include> </Files> </CSHARP> </VisualStudioProject> --- NEW FILE: NullableInt32.cs --- using System; using System.Runtime.Serialization; namespace Nullables { [System.ComponentModel.TypeConverter(typeof(Nullables.TypeConverters.NullableInt32Converter)), Serializable()] public struct NullableInt32 : INullableType { public static readonly NullableInt32 Default = new NullableInt32(); Int32 _value; bool hasValue; #region Constructors public NullableInt32(Int32 value) { _value = value; hasValue = true; } #endregion #region INullable Members object INullableType.Value { get { return Value; } } public bool HasValue { get { return hasValue; } } #endregion public Int32 Value { get { return _value; } } #region Casts public static explicit operator Int32(NullableInt32 nullable) { if (!nullable.HasValue) throw new NullReferenceException(); return nullable.Value; } public static implicit operator NullableInt32(Int32 value) { return new NullableInt32(value); } public static implicit operator NullableInt32(DBNull value) { return NullableInt32.Default; } #endregion public override string ToString() { if (HasValue) return Value.ToString(); else return string.Empty; } public override bool Equals(object obj) { if (obj is DBNull && !HasValue) return true; else if (obj is NullableInt32) return Equals((NullableInt32)obj); else return false; //if this is reached, it is either some other type, or DBnull is compared with this and we have a Value. } public bool Equals(NullableInt32 x) { return Equals(this, x); } public static bool Equals(NullableInt32 x, NullableInt32 y) { if (x.HasValue != y.HasValue) //one is null return false; else if (x.HasValue) //therefor y also HasValue return x.Value == y.Value; else //both are null return true; } public static bool operator ==(NullableInt32 x, NullableInt32 y) { return x.Equals(y); } public static bool operator ==(NullableInt32 x, object y) { return x.Equals(y); } public static bool operator !=(NullableInt32 x, NullableInt32 y) { return !x.Equals(y); } public static bool operator !=(NullableInt32 x, object y) { return !x.Equals(y); } public static NullableInt32 operator +(NullableInt32 x, NullableInt32 y) { if (!x.HasValue || !y.HasValue) //one or both are null return NullableInt32.Default; return new NullableInt32(x.Value + y.Value); } public static NullableInt32 operator -(NullableInt32 x, NullableInt32 y) { if (!x.HasValue || !y.HasValue) //one or both are null return NullableInt32.Default; return new NullableInt32(x.Value - y.Value); } public static NullableInt32 operator *(NullableInt32 x, NullableInt32 y) { if (!x.HasValue || !y.HasValue) //one or both are null return NullableInt32.Default; return new NullableInt32(x.Value * y.Value); } public static NullableInt32 operator /(NullableInt32 x, NullableInt32 y) { if (!x.HasValue || !y.HasValue) //one or both are null return NullableInt32.Default; return new NullableInt32(x.Value / y.Value); } public override int GetHashCode() { if (HasValue) return Value.GetHashCode(); else return 0; //GetHashCode() doesn't garantee uniqueness, and neither do we. } } } --- NEW FILE: NullableInt64.cs --- using System; namespace Nullables { [System.ComponentModel.TypeConverter(typeof(Nullables.TypeConverters.NullableInt64Converter)), Serializable()] public struct NullableInt64 : INullableType { public static readonly NullableInt64 Default = new NullableInt64(); Int64 _value; bool hasValue; #region Constructors public NullableInt64(Int64 value) { _value = value; hasValue = true; } #endregion #region INullable Members object INullableType.Value { get { return Value; } } public bool HasValue { get { return hasValue; } } #endregion public Int64 Value { get { return _value; } } #region Casts public static explicit operator Int64(NullableInt64 nullable) { if (!nullable.HasValue) throw new NullReferenceException(); return nullable.Value; } public static implicit operator NullableInt64(Int64 value) { return new NullableInt64(value); } public static implicit operator NullableInt64(DBNull value) { return NullableInt64.Default; } #endregion public override string ToString() { if (HasValue) return Value.ToString(); else return string.Empty; } public override bool Equals(object obj) { if (obj is DBNull && !HasValue) return true; else if (obj is NullableInt64) return Equals((NullableInt64)obj); else return false; //if this is reached, it is either some other type, or DBnull is compared with this and we have a Value. } public bool Equals(NullableInt64 x) { return Equals(this, x); } public static bool operator ==(NullableInt64 x, NullableInt64 y) { return x.Equals(y); } public static bool operator ==(NullableInt64 x, object y) { return x.Equals(y); } public static bool operator !=(NullableInt64 x, NullableInt64 y) { return !x.Equals(y); } public static bool operator !=(NullableInt64 x, object y) { return !x.Equals(y); } public static bool Equals(NullableInt64 x, NullableInt64 y) { if (x.HasValue != y.HasValue) //one is null return false; else if (x.HasValue) //therefor y also HasValue return x.Value == y.Value; else //both are null return true; } public static NullableInt64 operator +(NullableInt64 x, NullableInt64 y) { if (!x.HasValue || !y.HasValue) //one or both are null return NullableInt64.Default; return new NullableInt64(x.Value + y.Value); } public static NullableInt64 operator -(NullableInt64 x, NullableInt64 y) { if (!x.HasValue || !y.HasValue) //one or both are null return NullableInt64.Default; return new NullableInt64(x.Value - y.Value); } public static NullableInt64 operator *(NullableInt64 x, NullableInt64 y) { if (!x.HasValue || !y.HasValue) //one or both are null return NullableInt64.Default; return new NullableInt64(x.Value * y.Value); } public static NullableInt64 operator /(NullableInt64 x, NullableInt64 y) { if (!x.HasValue || !y.HasValue) //one or both are null return NullableInt64.Default; return new NullableInt64(x.Value / y.Value); } public override int GetHashCode() { if (HasValue) return Value.GetHashCode(); else return 0; //GetHashCode() doesn't garantee uniqueness, and neither do we. } } } --- NEW FILE: NullableBoolean.cs --- using System; namespace Nullables { [System.ComponentModel.TypeConverter(typeof(Nullables.TypeConverters.NullableBooleanConverter)), Serializable()] public struct NullableBoolean : INullableType { public static readonly NullableBoolean Default = new NullableBoolean(); Boolean _value; bool hasValue; #region Constructors public NullableBoolean(Boolean value) { _value = value; hasValue = true; } #endregion #region INullable Members object INullableType.Value { get { return Value; } } public bool HasValue { get { return hasValue; } } #endregion public Boolean Value { get { return _value; } } #region Casts public static explicit operator Boolean(NullableBoolean nullable) { if (!nullable.HasValue) throw new NullReferenceException(); return nullable.Value; } public static implicit operator NullableBoolean(Boolean value) { return new NullableBoolean(value); } public static implicit operator NullableBoolean(DBNull value) { return NullableBoolean.Default; } #endregion public override string ToString() { if (HasValue) return Value.ToString(); else return string.Empty; } public override bool Equals(object obj) { if (obj is DBNull && !HasValue) return true; else if (obj is NullableBoolean) return Equals((NullableBoolean)obj); else return false; //if this is reached, it is either some other type, or DBnull is compared with this and we have a Value. } public bool Equals(NullableBoolean x) { return Equals(this, x); } public static bool Equals(NullableBoolean x, NullableBoolean y) { if (x.HasValue != y.HasValue) //one is null return false; else if (x.HasValue) //therefor y also HasValue return x.Value == y.Value; else //both are null return true; } public static bool operator ==(NullableBoolean x, NullableBoolean y) { return x.Equals(y); } public static bool operator ==(NullableBoolean x, object y) { return x.Equals(y); } public static bool operator !=(NullableBoolean x, NullableBoolean y) { return !x.Equals(y); } public static bool operator !=(NullableBoolean x, object y) { return !x.Equals(y); } public override int GetHashCode() { if (HasValue) return Value.GetHashCode(); else return 0; //GetHashCode() doesn't garantee uniqueness, and neither do we. } //TODO: Operators for bool (or, and, xor, etc) } } |