Click here to Skip to main content
15,891,864 members
Articles / Web Development / ASP.NET

Declarative Programming--Unifying Form And Web Development

,
Rate me:
Please Sign up or sign in to vote.
4.69/5 (14 votes)
23 Nov 20044 min read 71.6K   912   39  
Use declarative programming to create UI's common for both Web and Form applets.
// Copyright (c) 2004 Marc Clifton.  All Rights Reserved

using System;
using System.Globalization;
using System.Web.UI.WebControls;

using MyXaml;

namespace declarativeWeb
{
	public class SimpleCalc
	{
		private bool cleared;
		private string lastOp;
		private string lastValue;
		private NumberFormatInfo formatProvider;
	
		[MyXamlAutoInitialize] private TextBox display=null;

		public SimpleCalc()
		{
			cleared=true;
			lastOp=String.Empty;
			lastValue=String.Empty;

			formatProvider=new NumberFormatInfo();
			formatProvider.NumberDecimalDigits=2;
		}

		public void OnClear(object sender, EventArgs e)
		{
			display.Text="0.00";
			cleared=true;
			lastOp=String.Empty;
			lastValue=String.Empty;
		}

		public void OnClearEntry(object sender, EventArgs e)
		{
			display.Text="0.00";
			cleared=true;
		}

		public void OnDigit(object sender, EventArgs e)
		{
			Button btn=(Button)sender;
			if (cleared)
			{
				display.Text=btn.Text;
				cleared=false;
			}
			else
			{
				display.Text=display.Text+btn.Text;
			}
		}

		public string ProcessLastOp(string val)
		{
			double d=0;
			switch(lastOp)
			{
				case "+":
				{
					d=Convert.ToDouble(lastValue) + Convert.ToDouble(val);
					break;
				}
				case "-":
				{
					d=Convert.ToDouble(lastValue) - Convert.ToDouble(val);
					break;
				}
				case "*":
				{
					d=Convert.ToDouble(lastValue) * Convert.ToDouble(val);
					break;
				}
				case "/":
				{
					if (Convert.ToDouble(val) != 0.0)
					{
						d=Convert.ToDouble(lastValue) / Convert.ToDouble(val);
					}
					break;
				}
			}
			lastValue=d.ToString("N", formatProvider);
			return lastValue;
		}

		public void OnAdd(object sender, EventArgs e)
		{
			if (lastValue==String.Empty)
			{
				lastValue=display.Text;
			}
			else
			{
				lastValue=ProcessLastOp(display.Text);
				display.Text=lastValue;
			}
			lastOp="+";
			cleared=true;
		}

		public void OnSubtract(object sender, EventArgs e)
		{
			if (lastValue==String.Empty)
			{
				lastValue=display.Text;
			}
			else
			{
				lastValue=ProcessLastOp(display.Text);
				display.Text=lastValue;
			}
			lastOp="-";
			cleared=true;
		}

		public void OnMultiply(object sender, EventArgs e)
		{
			if (lastValue==String.Empty)
			{
				lastValue=display.Text;
			}
			else
			{
				lastValue=ProcessLastOp(display.Text);
				display.Text=lastValue;
			}
			lastOp="*";
			cleared=true;
		}

		public void OnDivide(object sender, EventArgs e)
		{
			if (lastValue==String.Empty)
			{
				lastValue=display.Text;
			}
			else
			{
				lastValue=ProcessLastOp(display.Text);
				display.Text=lastValue;
			}
			lastOp="/";
			cleared=true;
		}

		public void OnEqual(object sender, EventArgs e)
		{
			if (lastValue != String.Empty)
			{
				display.Text=ProcessLastOp(display.Text);
				lastValue=String.Empty;
				lastOp=String.Empty;
				cleared=true;
			}
		}
	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


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.

Written By
Web Developer
United States United States
My main goal as a developer is to improve the way software is designed, and how it interacts with the user. I like designing software best, but I also like coding and documentation. I especially like to work with user interfaces and graphics.

I have extensive knowledge of the .NET Framework, and like to delve into its internals. I specialize in working with VG.net and MyXaml. I also like to work with ASP.NET, AJAX, and DHTML.

Comments and Discussions