Click here to Skip to main content
15,894,180 members
Articles / Programming Languages / C#

An Event Pool

Rate me:
Please Sign up or sign in to vote.
4.68/5 (48 votes)
21 Feb 2004CPOL4 min read 153.8K   3.1K   105  
An event pool helps manage large amounts of events that otherwise clutter up your code and make maintenance difficult.
using System;
using System.Globalization;

namespace MVCDemo
{
	public delegate void RegisterChangeDelegate(object sender, EventArgs args);

	public class RpnModel : ModelBase
	{
		// 4 register stack
		protected string[] registerStack={"0.00", "0.00", "0.00", "0.00"};
		protected bool newEntryState;
		protected NumberFormatInfo formatProvider;

		public bool NewEntryState
		{
			get {return newEntryState;}
			set
			{
				newEntryState=value;
				eventPool.FireEvent("EntryStateChanged");
			}
		}

		public string X
		{
			get {return registerStack[0];}
			set
			{
				registerStack[0]=value;
				eventPool.FireEvent("X");
			}
		}

		public string Y
		{
			get {return registerStack[1];}
			set
			{
				registerStack[1]=value;
				eventPool.FireEvent("Y");
			}
		}

		public string R3
		{
			get {return registerStack[2];}
			set
			{
				registerStack[2]=value;
				eventPool.FireEvent("R3");
			}
		}

		public string R4
		{
			get {return registerStack[3];}
			set
			{
				registerStack[3]=value;
				eventPool.FireEvent("R4");
			}
		}

		public double DX
		{
			get {return Convert.ToDouble(X);}
			set {X=value.ToString("N", formatProvider);}
		}

		public double DY
		{
			get {return Convert.ToDouble(Y);}
		}

		public RpnModel()
		{
			formatProvider=new NumberFormatInfo();
			formatProvider.NumberDecimalDigits=2;
			newEntryState=true;
			eventPool.Publish("X", "Y", "R3", "R4", "EntryStateChanged");
		}

		public void RollUp()
		{
			string temp=R4;
			R4=R3;
			R3=Y;
			Y=X;
			X=temp;
		}

		public void RollDown()
		{
			string temp=X;
			X=Y;
			Y=R3;
			R3=R4;
			R4=temp;
		}

		public void Push()
		{
			R4=R3;
			R3=Y;
			Y=X;
		}

		public void PopY()
		{
			Y=R3;
			R3=R4;
			R4="0.00";
		}

		public void Exchange()
		{
			string temp=X;
			X=Y;
			Y=temp;
		}

		public void ForceUpdate()
		{
			eventPool.FireAll();
		}

		public void FormatX()
		{
			X=DX.ToString("N", formatProvider);
		}

		public void AddPoint()
		{
			AddCharToX(formatProvider.NumberDecimalSeparator[0]);
		}

		public void AddCharToX(char c)
		{
			if (newEntryState)
			{
				X=c.ToString();
			}
			else
			{
				X+=c;
			}
		}

		public void RemoveCharFromX()
		{
			if (X.Length==1)
			{
				DX=0;
				NewEntryState=true;
			}
			else
			{
				X=X.Substring(0, X.Length-1);
			}
		}

		public void ChangeSign()
		{
			if (DX != 0.00)
			{
				if (X[0] != '-')
				{
					X="-"+X;
				}
				else
				{
					X=X.Substring(1);
				}
			}
		}

		public IMemento GetState()
		{
			IMemento memento=new RpnModelMemento();
			memento.SetState(registerStack);
			return memento;
		}

		public void SetState(IMemento memento)
		{
			((string[])memento.GetState()).CopyTo(registerStack, 0);
			eventPool.FireAll();
		}
	}
}

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