Here is some Sample Code for Classes and Methods

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Gallagher

    #1

    Here is some Sample Code for Classes and Methods

    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.W ebControls;
    using System.Web.UI.H tmlControls;

    namespace MikesASPNET

    {
    /// <summary>
    /// Summary description for WebForm1.
    /// </summary>
    public class MainPage : System.Web.UI.P age
    {
    protected
    System.Web.UI.W ebControls.Text Box PAmount;
    protected System.Web.UI.W ebControls.Labe l
    Label1;
    protected
    System.Web.UI.W ebControls.Butt on btnSbm;
    protected System.Web.UI.W ebControls.Labe l
    lblTxtT;
    protected System.Web.UI.W ebControls.Labe l
    lblTitle;
    protected System.Web.UI.W ebControls.Labe l
    ResultOut;

    private void Page_Load(objec t sender,
    System.EventArg s e)
    {

    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArg s
    e)
    {
    //
    // CODEGEN: This call is required
    by the ASP.NET Web Form Designer.
    //
    InitializeCompo nent();
    base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support -
    do not modify
    /// the contents of this method with the
    code editor.
    /// </summary>
    private void InitializeCompo nent()
    {
    this.btnSbm.Cli ck += new
    System.EventHan dler(this.btnSb m_Click);
    this.Load += new
    System.EventHan dler(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(ob ject sender,
    System.EventArg s 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(Amou nt),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.ToStrin g
    ();
    PAmount.Text = "";
    }


    }
    public class Computation
    {

    // This is the computeTax Method Retuns
    AmountT as a float when called
    public float computeTax(floa t Amount)
    {

    float LTax = 0.0925F, TaxT = 0,
    AmountT =0;
    TaxT = Amount *LTax;
    AmountT = Amount + TaxT;
    return AmountT;
    }

    }
    }

Working...