Example #1
0
 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);
     }
 }
Example #2
0
        public int CompareTo(object obj)
        {
            if (obj is NullableDateTime) //chack and unbox
            {
                NullableDateTime value = (NullableDateTime)obj;

                if (value.HasValue == this.HasValue) //both null or not null
                {
                    if (this.HasValue)               //this has a value, so they both do
                    {
                        return(Value.CompareTo(value.Value));
                    }
                    else
                    {
                        return(0); //both null, so they are equal;
                    }
                }
                else //one is null
                {
                    if (HasValue) //he have a value, so we are greater.
                    {
                        return(1);
                    }
                    else
                    {
                        return(-1);
                    }
                }
            }
            else if (obj is DateTime)
            {
                DateTime value = (DateTime)obj;

                if (HasValue) //not null, so compare the real values.
                {
                    return(Value.CompareTo(value));
                }
                else
                {
                    return(-1); //this is null, so less that the real value;
                }
            }

            throw new ArgumentException("NullableDateTime can only compare to another NullableDateTime or a System.DateTime");
        }
Example #3
0
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value,
                                         Type destinationType)
        {
            if (destinationType == typeof(InstanceDescriptor) && value is NullableDateTime)
            {
                NullableDateTime nullable = (NullableDateTime)value;

                Type[] constructorArgTypes = new Type[1] {
                    typeof(DateTime)
                };
                ConstructorInfo constructor = typeof(NullableDateTime).GetConstructor(constructorArgTypes);

                if (constructor != null)
                {
                    object[] constructorArgValues = new object[1] {
                        nullable.Value
                    };
                    return(new InstanceDescriptor(constructor, constructorArgValues));
                }
            }
            else if (destinationType == typeof(DateTime))
            {
                NullableDateTime ndt = (NullableDateTime)value;

                if (ndt.HasValue)
                {
                    return(ndt.Value);
                }
                else
                {
                    return(DBNull.Value);
                }
            }

            return(base.ConvertTo(context, culture, value, destinationType));
        }
Example #4
0
 public bool Equals(NullableDateTime x)
 {
     return(Equals(this, x));
 }