Define and use properties (C#) : Property « Language Basics « ASP.Net






Define and use properties (C#)

<%@ page Language="c#" runat="server" %>
<script runat="server">
  public class Calculator
  {
    private double currentValue;
    public double CurrentValue
    {
      get 
      {
        return currentValue;
      }
    }

    public void Add(double addValue)
    {
      currentValue += addValue;
    }

    public void Subtract(double subValue)
    {
      currentValue -= subValue;
    }

    public void Multiply(double multValue)
    {
      currentValue *= multValue;
    }

    public void Divide(double divValue)
    {
      currentValue /= divValue;
    }

    public void Clear()
    {
      currentValue = 0;
    }

 }
  void Page_Load() 
  {
    Calculator MyCalc = new Calculator();
    Response.Write("<b>Created a new Calculator object.</b><br/>");
    Response.Write("Current Value = " + MyCalc.CurrentValue);

    MyCalc.Add(23);
    Response.Write("<br/><b>Added 23 - MyCalc.Add(23)</b><br/>");
    Response.Write("Current Value = " + MyCalc.CurrentValue);

    MyCalc.Subtract(7);
    Response.Write("<br/><b>Subtracted 7 - MyCalc.Subtract(7)</b><br/>");
    Response.Write("Current Value = " + MyCalc.CurrentValue);

    MyCalc.Multiply(3);
    Response.Write("<br/><b>Multiplied by 3 - MyCalc.Multiply(3)</b><br/>");
    Response.Write("Current Value = " + MyCalc.CurrentValue);

    MyCalc.Divide(4);
    Response.Write("<br/><b>Divided by 4 - MyCalc.Divide(4)</b><br/>");
    Response.Write("Current Value = " + MyCalc.CurrentValue);

    MyCalc.Clear();
    Response.Write("<br/><b>Cleared - MyCalc.Clear()</b><br/>");
    Response.Write("Current Value = " + MyCalc.CurrentValue);
  }
</script>

           
       








Related examples in the same category

1.Throw Exception in Property setter (VB.net)
2.Properties for read and write (C#)