Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

WPF Control Factory

, 20 Apr 2010 CPOL
This article explains some advantages and disadvantages of factories, and shows one to use for generating WPF Controls.
WpfControlsAndSample.zip
WpfControlsAndSample
Pfz.ClassLibraries
Pfz
Caching
Collections
DataTypes
Extensions
Pfz.snk
Properties
Ranges
Threading
Pfz.WpfControls
BoundGridParts
Extensions
Pfz.WpfControls.snk
Properties
TypeConverters
WpfControlFactorySample
bin
Debug
Pfz.dll
Pfz.WpfControls.dll
WpfControlFactorySample.exe
Properties
Settings.settings
WpfControlFactorySample.suo
using System;
using System.Globalization;

namespace Pfz.DataTypes
{
	/// <summary>
	/// Class that represents a Date without Time.
	/// </summary>
	[Serializable]
	public struct Date:
		IEquatable<Date>,
		IComparable<Date>,
		IComparable
	{
		#region DefaultFormat
			private static string fDefaultFormat = "yyyy/MM/dd";
			
			/// <summary>
			/// Gets or sets the default format used to display Date
			/// strings.
			/// </summary>
			public static string DefaultFormat
			{
				get
				{
					return fDefaultFormat;
				}
				set
				{
					fDefaultFormat = value;
				}
			}
		#endregion
		#region Parse
			/// <summary>
			/// Parses the given text as a Date using the default format.
			/// </summary>
			public static Date Parse(string text)
			{
				DateTime result = DateTime.ParseExact(text, fDefaultFormat, CultureInfo.InvariantCulture);
				return (Date)result;
			}
		#endregion
	
		#region Today
			/// <summary>
			/// Gets Today as a Date object.
			/// </summary>
			public static Date Today
			{
				get
				{
					return new Date(DateTime.Today);
				}
			}
		#endregion
	
		#region Constructors
			/// <summary>
			/// Creates a new date by the specified year, month and day.
			/// </summary>
			public Date(int year, int month, int day):
				this(new DateTime(year, month, day))
			{
			}
			
			/// <summary>
			/// Creates a new date from a DateTime. The time information is lost.
			/// </summary>
			public Date(DateTime dateTime)
			{
				fValue = dateTime.Year * 10000 + dateTime.Month * 100 + dateTime.Day;
			}
			
			/// <summary>
			/// Creates a Date object from an integer value previously got from
			/// the AsInteger property of another Date.
			/// </summary>
			public Date(int asIntegerValue)
			{
				fValue = asIntegerValue;
			}
		#endregion
		
		#region Properties
			/// <summary>
			/// Gets the Year.
			/// </summary>
			public int Year
			{
				get
				{
					return fValue / 10000;
				}
			}
			
			/// <summary>
			/// Gets the Month.
			/// </summary>
			public int Month
			{
				get
				{
					return fValue / 100 % 100;
				}
			}
			
			/// <summary>
			/// Gets the Day.
			/// </summary>
			public int Day
			{
				get
				{
					return fValue % 100;
				}
			}
			
			private int fValue;
			/// <summary>
			/// Converts this Date value to an integer representation.
			/// </summary>
			public int AsInteger
			{
				get
				{
					return fValue;
				}
			}
		#endregion
		#region Methods
			#region Static comparisons
				/// <summary>
				/// Compares if this two dates are equal.
				/// </summary>
				public static bool operator == (Date date1, Date date2)
				{
					return date1.fValue == date2.fValue;
				}
				
				/// <summary>
				/// Compares if two dates are different.
				/// </summary>
				public static bool operator != (Date date1, Date date2)
				{
					return date1.fValue != date2.fValue;
				}
				
				/// <summary>
				/// Compares if the first date is less than the second date.
				/// </summary>
				public static bool operator < (Date date1, Date date2)
				{
					return date1.fValue < date2.fValue;
				}
				
				/// <summary>
				/// Compares if the first date is greater than the second.
				/// </summary>
				public static bool operator > (Date date1, Date date2)
				{
					return date1.fValue > date2.fValue;
				}
				
				/// <summary>
				/// Compares if the first date is less than or equal to the second.
				/// </summary>
				public static bool operator <= (Date date1, Date date2)
				{
					return date1.fValue <= date2.fValue;
				}
				
				/// <summary>
				/// Compares if the first date is greater than or equal to the second.
				/// </summary>
				public static bool operator >= (Date date1, Date date2)
				{
					return date1.fValue >= date2.fValue;
				}
			#endregion
			#region Implicit and explicit conversions
				/// <summary>
				/// Implicit convertion to DateTime object.
				/// </summary>
				public static implicit operator DateTime(Date date)
				{
					if (date == default(Date))
						return default(DateTime);
				
					return new DateTime(date.Year, date.Month, date.Day);
				}
				
				/// <summary>
				/// Explicit cast from DateTime to Date value.
				/// </summary>
				public static explicit operator Date(DateTime dateTime)
				{
					return new Date(dateTime);
				}
			#endregion
			#region Operator +
				/// <summary>
				/// Returns a new DateTime composed of the given date and time.
				/// </summary>
				public static DateTime operator + (Date date, Time time)
				{
					return new DateTime(date.Year, date.Month, date.Day, time.Hour, time.Minute, time.Second, time.Millisecond);
				}
			#endregion
			
			/// <summary>
			/// Gets the HashCode of the AsInteger value of this date.
			/// </summary>
			public override int GetHashCode()
			{
				return fValue.GetHashCode();
			}
			
			/// <summary>
			/// Compares if this Date equals another object.
			/// </summary>
			public override bool Equals(object obj)
			{
				if (obj is Date)
				{
					Date other = (Date)obj;
					return Equals(other);
				}
				
				return base.Equals(obj);
			}
			
			/// <summary>
			/// Returns true if this Date equals another Date.
			/// </summary>
			public bool Equals(Date other)
			{
				return fValue == other.fValue;
			}
			
			/// <summary>
			/// Converts this Date to a string representation.
			/// </summary>
			public override string ToString()
			{
				if (this == default(Date))
					return "";
			
				DateTime dateTime = this;
				return dateTime.ToString(fDefaultFormat);
			}
			
			/// <summary>
			/// Compares this date to another date.
			/// </summary>
			public int CompareTo(Date other)
			{
				return fValue - other.fValue;
			}
			
			int IComparable.CompareTo(object obj)
			{
				return CompareTo((Date)obj);
			}
		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Paulo Zemek
Engineer Microsoft Corporation
United States United States
I started to program computers when I was 11 years old, as a hobbist, programming in AMOS Basic and Blitz Basic for Amiga.
At 12 I had my first try with assembler, but it was too difficult at the time. Then, in the same year, I learned C and, after learning C, I was finally able to learn assembler (for Motorola 680x0).
Not sure, but probably between 12 and 13, I started to learn C++. I always programmed "in an object oriented way", but using function pointers instead of virtual methods.
 
At 15 I started to learn Pascal at school and to use Delphi. At 16 I started my first internship (using Delphi). At 18 I started to work professionally using C++ and since then I've developed my programming skills as a professional developer in C++ and C#, generally creating libraries that help other developers do they work easier, faster and with less errors.
 
Now I just started working as a Senior Software Engineer at Microsoft.
 
Want more info or simply want to contact me?
Take a look at: http://paulozemek.azurewebsites.net/
Or e-mail me at: paulozemek@outlook.com
 
Codeproject MVP 2012
Microsoft MVP 2013-2014

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141220.1 | Last Updated 20 Apr 2010
Article Copyright 2010 by Paulo Zemek
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid