ASP.Net/Language Basics/Property — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 11:53, 26 мая 2010

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>



Properties for read and write (C#)

<%@ page Language="c#" runat="server" %>
<script runat="server">
public class Car
{
  private string color;
  private int gear;
  public string Color
  {
    get 
    {
      return color;
    }
    set 
    {
      color = value;
    }
  }
  public int Gear
  {
    get 
    {
      return gear;
    }
  }
  public void ChangeGear(int Direction)
  {
    if (Direction < 0)
      gear--;
    if (Direction > 0)
      gear++;
    if ( gear > 5) 
      gear = 5;
    if ( gear < -1) 
      gear = -1;
  }
}
void Page_Load()
{
  Car MyCar = new Car();
  Response.Write("<b>New object "MyCar" created.</b>");
  Response.Write("<br/>Color: " + MyCar.Color);
  Response.Write("<br/>Gear: " + MyCar.Gear);
  MyCar.Color = "Black";
  MyCar.ChangeGear(+1);
  Response.Write("<br/><b>Properties updated.</b>");
  Response.Write("<br/>New color: " + MyCar.Color);
  Response.Write("<br/>New gear: " + MyCar.Gear);
}
</script>



Throw Exception in Property setter (VB.net)

<%@ Page Language="VB" %>
<script runat="server">
Public Class Math
    Private intValue As Integer
    Public Property InValue() As Integer
        Get
            Return intValue
        End Get
        Set(ByVal value As Integer)
            If intValue > 0 Then
                intValue = value
            Else
                Throw New Exception("InValue can not be less than 1")
            End If
        End Set 
    End Property
    Public Function MultiplyBySelf(ByVal InValue As Integer) As Integer
        Return Multiply(InValue, InValue)
    End Function
    Private Function Multiply(ByVal FirstValue As Integer, ByVal SecondValue As Integer)
        Return FirstValue * SecondValue
    End Function
End Class
    Sub Page_Load()
        Dim clsMath As Math
        clsMath = New Math
        clsMath.InValue = 10
        lblMessage.Text = clsMath.MultiplyBySelf(10)
    End Sub
    
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Show Shared Hello World</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    <asp:Label
        id="lblMessage"
        Runat="server" />
    
    </div>
    </form>
</body>
</html>