Click here to Skip to main content
15,896,118 members
Articles / Desktop Programming / Windows Forms

Reputationator - CP Narcissists Rejoice! Part 1 of 4

Rate me:
Please Sign up or sign in to vote.
4.93/5 (35 votes)
30 Aug 2011CPOL22 min read 59.1K   882   41  
Keep more detailed track of your Codeproject reputation points.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Xml.Linq;

namespace RepWPF
{
using System;

	//////////////////////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////////////////////
	/// <summary>
	/// Contains extension methods for the XElement class
	/// </summary>
	public static class ExtendedXElement
	{
		//--------------------------------------------------------------------------------
		public static bool ContainsElement(this XElement root, string name)
		{
			return (root.Elements(name) != null);
		}
		//--------------------------------------------------------------------------------
		public static bool ContainsAttribute(this XElement root, string name)
		{
			return (root.Attributes(name) != null);
		}
		//--------------------------------------------------------------------------------
		public static string GetValue(this XElement root, string name, string defaultValue)
		{
			return (string)root.Elements(name).FirstOrDefault() ?? defaultValue;
		}
		//--------------------------------------------------------------------------------
		public static double GetValue(this XElement root, string name, double defaultValue)
		{
			string strValue = (string)root.Elements(name).FirstOrDefault() ?? defaultValue.ToString();
			double value;
			if (!double.TryParse(strValue, out value))
			{
				throw new Exception(string.Format("Element {0}: Value retrieved was not a valid double", name));
			}
			return value;
		}
		//--------------------------------------------------------------------------------
		public static decimal GetValue(this XElement root, string name, decimal defaultValue)
		{
			string strValue = (string)root.Elements(name).FirstOrDefault() ?? defaultValue.ToString();
			decimal value;
			if (!decimal.TryParse(strValue, out value))
			{
				throw new Exception(string.Format("Element {0}: Value retrieved was not a valid decimal", name));
			}
			return value;
		}
		//--------------------------------------------------------------------------------
		public static Int32 GetValue(this XElement root, string name, Int32 defaultValue)
		{
			string strValue = (string)root.Elements(name).FirstOrDefault() ?? defaultValue.ToString();
			Int32 value;
			if (!Int32.TryParse(strValue, out value))
			{
				throw new Exception(string.Format("Element {0}: Value retrieved was not a valid 32-bit integer", name));
			}
			return value;
		}
		//--------------------------------------------------------------------------------
		public static bool GetValue(this XElement root, string name, bool defaultValue)
		{
			string strValue = (string)root.Elements(name).FirstOrDefault() ?? defaultValue.ToString();
			bool value;
			if (!bool.TryParse(strValue, out value))
			{
				throw new Exception(string.Format("Element {0}: Value retrieved was not a valid boolean", name));
			}
			return value;
		}
		//--------------------------------------------------------------------------------
		public static DateTime GetValue(this XElement root, string name, DateTime defaultValue)
		{
			string strValue = (string)root.Elements(name).FirstOrDefault() ?? defaultValue.ToString();
			DateTime value;
			if (!DateTime.TryParse(strValue, out value))
			{
				throw new Exception(string.Format("Element {0}: Value retrieved was not a valid DateTime", name));
			}
			return value;
		}

		//--------------------------------------------------------------------------------
		public static string GetAttribute(this XElement root, string name, string defaultValue)
		{
			return (string)root.Attributes(name).FirstOrDefault() ?? defaultValue;
		}
		//--------------------------------------------------------------------------------
		public static double GetAttribute(this XElement root, string name, double defaultValue)
		{
			string strValue = (string)root.Attributes(name).FirstOrDefault() ?? defaultValue.ToString();
			double value;
			if (!double.TryParse(strValue, out value))
			{
				throw new Exception(string.Format("Attribute {0}: Value retrieved was not a valid double", name));
			}
			return value;
		}
		//--------------------------------------------------------------------------------
		public static decimal GetAttribute(this XElement root, string name, decimal defaultValue)
		{
			string strValue = (string)root.Attributes(name).FirstOrDefault() ?? defaultValue.ToString();
			decimal value;
			if (!decimal.TryParse(strValue, out value))
			{
				throw new Exception(string.Format("Attribute {0}: Value retrieved was not a valid decimal", name));
			}
			return value;
		}
		//--------------------------------------------------------------------------------
		public static Int32 GetAttribute(this XElement root, string name, Int32 defaultValue)
		{
			string strValue = (string)root.Attributes(name).FirstOrDefault() ?? defaultValue.ToString();
			Int32 value;
			if (!Int32.TryParse(strValue, out value))
			{
				throw new Exception(string.Format("Attribute {0}: Value retrieved was not a valid 32-bit integer", name));
			}
			return value;
		}
		//--------------------------------------------------------------------------------
		public static bool GetAttribute(this XElement root, string name, bool defaultValue)
		{
			string strValue = (string)root.Attributes(name).FirstOrDefault() ?? defaultValue.ToString();
			bool value;
			if (!bool.TryParse(strValue, out value))
			{
				throw new Exception(string.Format("Attribute {0}: Value retrieved was not a valid boolean", name));
			}
			return value;
		}
		//--------------------------------------------------------------------------------
		public static DateTime GetAttribute(this XElement root, string name, DateTime defaultValue)
		{
			string strValue = (string)root.Attributes(name).FirstOrDefault() ?? defaultValue.ToString();
			DateTime value;
			if (!DateTime.TryParse(strValue, out value))
			{
				throw new Exception(string.Format("Attribute {0}: Value retrieved was not a valid DateTime", name));
			}
			return value;
		}

	}

 


	//////////////////////////////////////////////////////////////////////////////////////
	public class DBItemBase : INotifyPropertyChanged
	{
		#region INotifyPropertyChanged code
		public event PropertyChangedEventHandler PropertyChanged;
		protected void OnPropertyChanged(string name)
		{
			PropertyChangedEventHandler handler = PropertyChanged;
			if (handler != null)
			{
				handler(this, new PropertyChangedEventArgs(name));
			}
		}
		#endregion INotifyPropertyChanged code
	}

	//////////////////////////////////////////////////////////////////////////////////////
	public class DBSeriesDataItem : DBItemBase
	{
		protected decimal  m_value;
		protected string   m_name;
		protected DateTime m_metricDate;

		//--------------------------------------------------------------------------------
		public decimal Value
		{
			get { return m_value; }
			set { m_value = value; OnPropertyChanged("Value"); }
		}
		//--------------------------------------------------------------------------------
		public string Name
		{
			get { return m_name; }
			set { m_name = value; OnPropertyChanged("Name"); }
		}

		//--------------------------------------------------------------------------------
		public XElement XElement
		{
			get
			{
				XElement value = new XElement("SeriesData");
				return value;
			}
			set
			{
				if (value != null)
				{
					//this.Value      = Convert.ToDecimal(value.GetValue ("Value", "0"));
					string strValue = value.GetValue("Value", "");
					if (!string.IsNullOrEmpty(strValue))
					{
						decimal numValue;
						if (decimal.TryParse(strValue, out numValue))
						{
							this.Value = numValue;
						}
					}
					this.Name = value.GetValue("Name", "UNK").Trim();
				}
			}

		}

		// indicates that the data represented by this data item is fabricated because 
		// it was filled in by the code in order to complete the desired data
		protected bool m_fabricatedData = false;
		public bool IsFabricatedData
		{
			get { return m_fabricatedData; }
			set { m_fabricatedData = value; }
		}

		//--------------------------------------------------------------------------------
		public DBSeriesDataItem(XElement element)
		{
			this.XElement = element;
		}
		//--------------------------------------------------------------------------------
		public DBSeriesDataItem(string name, decimal value)
		{
			this.Name = name;
			this.Value = value;
		}
		//--------------------------------------------------------------------------------
		/// <summary>
		/// This constructor builds a data item with a name but no value
		/// </summary>
		/// <param name="name"></param>
		public DBSeriesDataItem(string name)
		{
			this.Name = name;
			this.IsFabricatedData = true;
		}
	}

	#region Sample data
	//////////////////////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////////////////////
	public class Expense : INotifyPropertyChanged
	{
		private string m_name;
		private double m_value;
		private double m_goal;

		//--------------------------------------------------------------------------------
		public string Name
		{
			get { return m_name; }
			set
			{
				m_name = value;
				OnPropertyChanged("Name");
			}
		}

		//--------------------------------------------------------------------------------
		public double Value
		{
			get { return m_value; }
			set
			{
				m_value = value;
				OnPropertyChanged("Value");
			}
		}

		//--------------------------------------------------------------------------------
		public double Goal
		{
			get { return m_goal; }
			set
			{
				m_goal = value;
				OnPropertyChanged("Goal");
			}
		}

		//--------------------------------------------------------------------------------
		public Expense(string name, double value)
		{
			this.Name = name;
			this.Value = value;
			this.Goal = 35;
		}

		public event PropertyChangedEventHandler PropertyChanged;
		protected void OnPropertyChanged(string name)
		{
			PropertyChangedEventHandler handler = PropertyChanged;
			if (handler != null)
			{
				handler(this, new PropertyChangedEventArgs(name));
			}
		}

	}

	//////////////////////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////////////////////
	public class Expenses : ObservableCollection<DBSeriesDataItem>
	{

		public Expenses()
		{
			MakeExpense("Food", 50.0M);
			MakeExpense("Rent", 173.0M);
			MakeExpense("Car", 150.50M);
			MakeExpense("Water", 23.0M);
			MakeExpense("Phone", 46.0M);
		}

		public Expenses(decimal[] values)
		{
			if (values.Length >= 1)
			{
				MakeExpense("Food", values[0]);
			}
			if (values.Length >= 2)
			{
				MakeExpense("Rent", values[1]);
			}
			if (values.Length >= 3)
			{
				MakeExpense("Car", values[2]);
			}
			if (values.Length >= 4)
			{
				MakeExpense("Water", values[3]);
			}
			if (values.Length >= 5)
			{
				MakeExpense("Phone", values[4]);
			}
		}

		private void MakeExpense(string name, decimal value)
		{
			DBSeriesDataItem exp = new DBSeriesDataItem(name, value);
			this.Add(exp);
		}

		public decimal GetMaxValue()
		{
			decimal value = 0;
			foreach (DBSeriesDataItem item in this)
			{
				value = Math.Max(value, item.Value);
			}
			return value;
		}
	}

	#endregion Sample data
}

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)


Written By
Software Developer (Senior) Paddedwall Software
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.

My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions