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

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

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

Define and use properties (C#)

   <source lang="csharp">

<%@ 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("Created a new Calculator object.
"); Response.Write("Current Value = " + MyCalc.CurrentValue); MyCalc.Add(23); Response.Write("
Added 23 - MyCalc.Add(23)
"); Response.Write("Current Value = " + MyCalc.CurrentValue); MyCalc.Subtract(7); Response.Write("
Subtracted 7 - MyCalc.Subtract(7)
"); Response.Write("Current Value = " + MyCalc.CurrentValue); MyCalc.Multiply(3); Response.Write("
Multiplied by 3 - MyCalc.Multiply(3)
"); Response.Write("Current Value = " + MyCalc.CurrentValue); MyCalc.Divide(4); Response.Write("
Divided by 4 - MyCalc.Divide(4)
"); Response.Write("Current Value = " + MyCalc.CurrentValue); MyCalc.Clear(); Response.Write("
Cleared - MyCalc.Clear()
"); Response.Write("Current Value = " + MyCalc.CurrentValue); }

</script>

      </source>
   
  


Properties for read and write (C#)

   <source lang="csharp">

<%@ 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("New object "MyCar" created.");
 Response.Write("
Color: " + MyCar.Color); Response.Write("
Gear: " + MyCar.Gear); MyCar.Color = "Black"; MyCar.ChangeGear(+1); Response.Write("
Properties updated."); Response.Write("
New color: " + MyCar.Color); Response.Write("
New gear: " + MyCar.Gear);

} </script>

      </source>
   
  


Throw Exception in Property setter (VB.net)

   <source lang="csharp">

<%@ 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">
   <asp:Label
       id="lblMessage"
       Runat="server" />
   
   </form>

</body> </html>

      </source>