Here below I wrote a program to take a subtotal of your
purchased amount and calulate the tax based on Tennesse
sales tax rate of 9.25%. This is just a basic ASP.NET C#
program which could be wired up as a windows form app as
well. I thought I share this with some of you that are
learning C#, ASP.NET, and the .NET Framework!
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace MikesASPNET
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class MainPage : System.Web.UI.Page
{
protected
System.Web.UI.WebControls.TextBox PAmount;
protected System.Web.UI.WebControls.Label
Label1;
protected
System.Web.UI.WebControls.Button btnSbm;
protected System.Web.UI.WebControls.Label
lblTxtT;
protected System.Web.UI.WebControls.Label
lblTitle;
protected System.Web.UI.WebControls.Label
ResultOut;
private void Page_Load(object sender,
System.EventArgs e)
{
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs
e)
{
//
// CODEGEN: This call is required
by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support -
do not modify
/// the contents of this method with the
code editor.
/// </summary>
private void InitializeComponent()
{
this.btnSbm.Click += new
System.EventHandler(this.btnSbm_Click);
this.Load += new
System.EventHandler(this.Page_Load);
}
#endregion
/// <summary>
/// This is were we wire up our code for the page to
actully do somthing!
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSbm_Click(object sender,
System.EventArgs e)
{
// We need to Create a New
Instance of Our Class Computation
// This will load the Computation
Class into memory for use
// As I under stand this when the
Class emeber within a event
// is called it is automaticly
destroyed in memory when not in use
Computation MyComputation = new
Computation();
// This was difficult to
understand these next two lines of code
// We need to get the Value from
the PAmount Textbox Property Text
// and Parse it to a floating
number this is important as the default
// value for textbox.text
property is of a string value
// Then we need to assign a the
retuned!!! Value from out Class
// Computation Method computeTax
to a varible. This was done by my design
// this way if I need to do more with it in this
context I can! There
// are other ways to do this I am
sure
float Amount;
Amount = float.Parse
(PAmount.Text);
// Are you bored and confused by
now? Well this is something I
// did by complete accident,
Notice our type for Amount is a float
// which is needed to pass this
to the Computation Class Method computeTax
// or we wil get a build error!
Remember whatever the Methods entry
// variable is declared to be,
must be the same when used to
// call the method! What I did
here I think is cool! By assigning the
// the Call to our method in a
variable, I converted the returned vaule
// from our method to double
stored in AmountT.
double AmountT = Math.Round
(MyComputation.computeTax(Amount),2);
// Now we take our varibles that
we worked with and convert them
// back to a string then and
assign them to the ResultOut.Text Lable's
// Property (Remember we need to
convert it back to a string to be
// useful!
ResultOut.Text = AmountT.ToString
();
PAmount.Text = "";
}
}
public class Computation
{
// This is the computeTax Method Retuns
AmountT as a float when called
public float computeTax(float Amount)
{
float LTax = 0.0925F, TaxT = 0,
AmountT =0;
TaxT = Amount *LTax;
AmountT = Amount + TaxT;
return AmountT;
}
}
}