ASP.NET Tutorial/Development/Class

Материал из .Net Framework эксперт
Версия от 11:56, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Abstract class

public abstract class BaseEmployee
{
    public abstract decimal Salary
    {
        get;
    }
    public string Company
    {
        get { return "Acme Software"; }
    }
}
public class SalesEmployee : BaseEmployee
{
    public override decimal Salary
    {
        get { return 67000.23m; }
    }
}


Assign value to class public fields (C#)

<%@ Page Language="C#" %>
<script runat="server">
   class Clock {
      public int Second;
      public int Minute;
      public int Hour;
      void SetTime(int intSec, int intMin, int intHour) {
         Second = intSec;
         Minute = intMin;
         Hour = intHour;
      }
   }
   
   void Page_Load(Object Sender, EventArgs e) {
      Clock objClock = new Clock();
      
      objClock.Second = 60;
      Response.Write(objClock.Second);
   }
</script>
<html><body>
   
</body></html>


Assign value to class public field (VB)

<%@ Page Language="VB" %>
<script runat="server">
   Class Clock
      public Second as integer
      public Minute as integer
      public Hour as integer
      sub SetTime(intSec as integer, intMin as integer, _
         intHour as integer)
         Second = intSec
         Minute = intMin
         Hour = intHour
      end sub
   End Class
   
   sub Page_Load(Sender as object, e as EventArgs)
      dim objClock as new Clock
      
      objClock.Second = 60
      
      Response.Write(objClock.Second)
   end sub
</script>
<html><body>
   
</body></html>


Call calss constructor to create new object (C#)

<script language="C#" runat="server">
   public class Clock {
      public int Second;
      public int Minute;
      public int Hour;
      public static int ClockCounter = 0;
      
      public static int AddClock(){
         ClockCounter += 1;
         return ClockCounter;
      }
      
      public void SetTime(int intSec, int intMin, int intHour) 
      {
         Second = intSec;
         Minute = intMin;
         Hour = intHour;
      }
   }
</script>
<HTML>
<BODY>
   <%
      Clock objClock = new Clock();
      objClock.SetTime(4,6,45);
      
      Clock.AddClock();
      Response.Write(Clock.ClockCounter);
   %>
   
</BODY></HTML>


Call class constructor to create new object (VB)

<script language="VB" runat="server">
   public class Clock 
      public Second as integer
      public Minute as integer
      public Hour as integer
      public ClockCounter as integer = 0
      
      public sub SetTime(intSec as integer, intMin as integer, _
         intHour as integer) 
         Second = intSec
         Minute = intMin
         Hour = intHour
      end sub
      
   end class
</script>
<HTML>
<BODY>
   <%
      dim objClock as new Clock()
      objClock.SetTime(4,6,45)
      
      Response.Write(objClock.ClockCounter)
   
   %>
   
</BODY></HTML>


Create read-only properties

File: ServerTime.cs
using System;

public class ServerTime
{
    public string CurrentTime
    {
        get
        {
            return DateTime.Now.ToString();
        }
    }
}


Creating Partial Classes

File: FirstHalf.cs
public partial class Tweedle
{
    private string _message = @"fir";
}

File: SecondHalf.cs
public partial class Tweedle
{
    public string GetMessage()
    {
        return _message;
    }
}


Declaring Constructors

A constructor is called when you create a new instance of a class. 
File: Quote.cs
using System;
using System.Collections.Generic;
public class Quote
{
    private List<string> _quotes = new List<string>();
    public string GetQuote()
    {
        Random rnd = new Random();
        return _quotes[rnd.Next(_quotes.Count)];
    }
    public Quote()
    {
        _quotes.Add("A");
        _quotes.Add("B");
        _quotes.Add("C");
    }
}


Declaring Interfaces

An interface is a list of properties and methods that a class must implement. 
public interface IProduct
{
    decimal Price
    {
        get;
    }
    void SaveProduct();
}
public class MusicProduct : IProduct
{
    public decimal Price
    {
        get { return 12.99m; }
    }
    public void SaveProduct()
    {
        // Save Music Product
    }
}
public class BookProduct : IProduct
{
    public decimal Price
    {
        get { return 23.99m; }
    }
    public void SaveProduct()
    {
        // Save Book Product
    }
}


Declaring Namespaces

A namespace enables you to group related classes. 
Namespaced.cs
namespace AspNetUnleashed.SampleCode
{
    public class Namespaced
    {
        public string SaySomething()
        {
            return "Something";
        }
    }
}


Define and use class in asp.net page (C#)

<%@ Page Language="C#" %>
<script runat="server">
   class MyValueClass {
      public DateTime dtBirthDay = new DateTime(1999,6,27);
      public string GetDayOfWeek() {
         return(dtBirthDay.DayOfWeek.ToString());
      }
   }
   void Button_Click(Object Sender, EventArgs e) {
      MyValueClass objMyValueClass = new MyValueClass();
      Response.Write(objMyValueClass.GetDayOfWeek());
   }
</script>
<html><body>
   <form runat="server">
      <asp:button id="btSubmit" Text="Submit" runat=server OnClick="Button_Click"/>
   </form>
</body></html>


Define and use class in asp.net page (VB)

<%@ Page Language="VB" %>
<script runat="server">
   class MyValueClass
      public dtBirthDay as new DateTime(1983,6,27)
      function GetDayOfWeek() as String
         return dtBirthDay.DayOfWeek.ToString()
      end function
   end class
   sub Button_Click(Sender as Object, e as EventArgs)
      dim objMyValueClass as new MyValueClass()
      Response.Write(objMyValueClass.GetDayOfWeek())
   end sub
</script>
<html>
<body>
   <form runat="server">
      <asp:button id="btSubmit" Text="Submit" runat=server OnClick="Button_Click"/>
   </form>
</body>
</html>


Inheritance and Abstract Classes

public class BaseProduct
{
    private decimal _price;
    public decimal Price
    {
        get { return _price; }
        set { _price = value; }
    }
}
public class ComputerProduct : BaseProduct
{
    private string _processor;
    public string Processor
    {
        get { return _processor; }
        set { _processor = value; }
    }
}
public class TelevisionProduct : BaseProduct
{
    private bool _isHDTV;
    public bool IsHDTV
    {
        get { return _isHDTV; }
        set { _isHDTV = value; }
    }
}


Overloading is useful when you want to associate related methods.

using System;

public class StoreProduct
{
    public void SaveProduct(string name)
    {
        SaveProduct(name, 0, String.Empty);
    }
    public void SaveProduct(string name, decimal price)
    {
        SaveProduct(name, price, String.Empty);
    }
    public void SaveProduct(string name, decimal price, string description)
    {
        // Save name, price, description to database
    }
}

File: ProductConstructor.cs
using System;

public class ProductConstructor
{
    public ProductConstructor(string name)
        : this(name, 0, String.Empty) { }
    public ProductConstructor(string name, decimal price)
        : this(name, price, String.Empty) { }
    public ProductConstructor(string name, decimal price, string description)
    {
        // Use name, price, and description
    }
}


Override a property or method of a base class

public class ProductBase
{
    private decimal _price;
    public virtual decimal Price
    {
        get { return _price; }
        set { _price = value; }
    }
}
public class OnSaleProduct : ProductBase
{
    override public decimal Price
    {
        get { return base.Price / 2; }
        set { base.Price = value; }
    }
}