Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / XML

Event Logging in Cx

Rate me:
Please Sign up or sign in to vote.
4.86/5 (8 votes)
30 Sep 2009CPOL7 min read 34.9K   284   16  
Adding an event logger to Cx.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Cx.Attributes;
using Cx.EventArgs;
using Cx.Interfaces;

namespace BusinessUnitComponent
{
	[CxComponentName("Calculator Business Unit")]
	public class Calculator : ICxBusinessComponentClass
	{
		[CxEvent] public event CxStringDlgt CurrentValueChanged;

		protected enum PendingOperation
		{
			None,
			Add,
			Subtract,
			Multiply,
			Divide,
		}

		protected PendingOperation pendingOperation;
		protected string lastValue;
		protected string currentValue;

		public string CurrentValue
		{
			get { return currentValue; }
			set
			{
				if (currentValue != value)
				{
					currentValue = value;
					OnCurrentValueChanged();
				}
			}
		}

		public Calculator()
		{
			pendingOperation = PendingOperation.None;
		}

		[CxConsumer]
		public void SetCurrentValue(object sender, CxEventArgs<string> args)
		{
			CurrentValue = args.Data;
		}

		[CxConsumer]
		public void Add(object sender, EventArgs e)
		{
			Calculate();
			lastValue = currentValue;
			pendingOperation = PendingOperation.Add;
		}

		[CxConsumer]
		public void Subtract(object sender, EventArgs e)
		{
			Calculate();
			lastValue = currentValue;
			pendingOperation = PendingOperation.Subtract;
		}

		[CxConsumer]
		public void Multiply(object sender, EventArgs e)
		{
			Calculate();
			lastValue = currentValue;
			pendingOperation = PendingOperation.Multiply;
		}

		[CxConsumer]
		public void Divide(object sender, EventArgs e)
		{
			Calculate();
			lastValue = currentValue;
			pendingOperation = PendingOperation.Divide;
		}

		[CxConsumer]
		public void Equal(object sender, EventArgs e)
		{
			Calculate();
			pendingOperation = PendingOperation.None;
		}

		[CxConsumer]
		public void Clear(object sender, EventArgs e)
		{
			CurrentValue = "0";
			pendingOperation = PendingOperation.None;
		}

		protected void Calculate()
		{
			try
			{
				switch (pendingOperation)
				{
					case PendingOperation.Add:
						CurrentValue = Convert.ToString(Convert.ToDouble(lastValue) + Convert.ToDouble(currentValue));
						break;

					case PendingOperation.Subtract:
						CurrentValue = Convert.ToString(Convert.ToDouble(lastValue) - Convert.ToDouble(currentValue));
						break;

					case PendingOperation.Multiply:
						CurrentValue = Convert.ToString(Convert.ToDouble(lastValue) * Convert.ToDouble(currentValue));
						break;

					case PendingOperation.Divide:
						CurrentValue = Convert.ToString(Convert.ToDouble(lastValue) / Convert.ToDouble(currentValue));
						break;
				}
			}
			catch
			{
				CurrentValue = "Error";
			}

			pendingOperation = PendingOperation.None;
		}

		protected void OnCurrentValueChanged()
		{
			EventHelpers.Fire(CurrentValueChanged, this, new CxEventArgs<string>(currentValue));
		}
	}
}

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
Architect Interacx
United States United States
Blog: https://marcclifton.wordpress.com/
Home Page: http://www.marcclifton.com
Research: http://www.higherorderprogramming.com/
GitHub: https://github.com/cliftonm

All my life I have been passionate about architecture / software design, as this is the cornerstone to a maintainable and extensible application. As such, I have enjoyed exploring some crazy ideas and discovering that they are not so crazy after all. I also love writing about my ideas and seeing the community response. As a consultant, I've enjoyed working in a wide range of industries such as aerospace, boatyard management, remote sensing, emergency services / data management, and casino operations. I've done a variety of pro-bono work non-profit organizations related to nature conservancy, drug recovery and women's health.

Comments and Discussions