ASP.NET Tutorial/Collections/IEnumerable

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

IEnumerable, IEnumerator

<%@ Page Language="C#" %>
<script runat="server">
void Page_Load()
{
  CustomCollection customCollection = new CustomCollection(10);
  
  DemoOutput.Text = "";
  foreach (CustomItem customItem in customCollection)
  {
    DemoOutput.Text += customItem.Index + "<br />";
  }
}
public class CustomCollection : IEnumerable, IEnumerator
{
  private CustomItem[] customItems;
  private int current = -1;
  public CustomCollection(int Count)
  {
    customItems = new CustomItem[Count];
    for (int index = 0; index < Count; index++)
    {
      customItems[index] = new CustomItem(index);
    }
  }
  #region Implementation of IEnumerable
  public IEnumerator GetEnumerator()
  {
    return (IEnumerator) this;
  }
  #endregion
  #region Implementation of IEnumerator
  public void Reset()
  {
    current = -1;
  }
  public bool MoveNext()
  {
    if (current < customItems.Length - 1)
    {
      current++;
      return true;
    }
    else
    {
      return false;
    }
  }
  public object Current
  {
    get
    {
      return customItems[current];
    }
  }
  #endregion
}
public class CustomItem
{
  private int index;
  
  public int Index
  {
    get
    {
      return index;
    }
  }
  
  public CustomItem(int Index)
  {
    index = Index;
  }
}
</script>
<html>
  <head>
    <title>Creating a Custom Collection</title>
  </head>
  <body>
    <form id="MainForm" runat="server">
      Output of Looping through a Custom Collection
      <br />
      <asp:literal id="DemoOutput" runat="server" />
    </form>
  </body>
</html>


IEnumerable, IEnumerator (VB)

<%@ Page Language="VB" %>
<script runat="server">
Sub Page_Load()
  Dim _customCollection = New CustomCollection(10)  
  
  DemoOutput.Text = ""
  Dim _customItem As CustomItem
  For Each _customItem In _customCollection
    DemoOutput.Text += _customItem.Index & "<br />"
  Next
End Sub
Public Class CustomCollection
  Implements IEnumerable, IEnumerator
  Private customItems() As CustomItem
  Private currentIndex As Integer = -1
  Public Sub New(ByVal Count As Integer)
    Dim index As Integer
    ReDim customItems(Count - 1)
    For index = 0 To Count - 1
      customItems(index) = New CustomItem(index)
    Next
  End Sub
#Region "Implementation of IEnumerable"
  Public Function GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
    Return CType(Me, IEnumerator)
  End Function
#End Region
#Region "Implementation of IEnumerator"
  Public Sub Reset() Implements IEnumerator.Reset
    currentIndex = -1
  End Sub
  Public Function MoveNext() As Boolean Implements IEnumerator.MoveNext
    If currentIndex < customItems.Length - 1 Then
      currentIndex = currentIndex + 1
      Return True
    Else
      Return False
    End If
  End Function
  Public ReadOnly Property Current() As Object Implements IEnumerator.Current
    Get
      Return customItems(currentIndex)
    End Get
  End Property
#End Region
End Class
Public Class CustomItem
  Private _index As Integer
  Public ReadOnly Property Index() As Integer
    Get
      Return _index
    End Get
  End Property
  Public Sub New(ByVal Index As Integer)
    _index = Index
  End Sub
End Class
</script>
<html>
  <head>
    <title>Creating a Custom Collection</title>
  </head>
  <body>
    <form id="MainForm" runat="server">
      Output of Looping through a Custom Collection
      <br />
      <asp:literal id="DemoOutput" runat="server" />
    </form>
  </body>
</html>


Using CustomCollection

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="UsingCustomCollection" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Using a Customer Collection</title>
</head>
<body>
    <form id="form1" runat="server">
   <div id="container">
      <h1>Using a Customer Collection</h1>
      This example illustrates the use of a custom collection
      <asp:DropDownList ID="drpSample" runat="server" 
         DataTextField="Name"
         DataValueField="Id"
/>
      
   </div>
    </form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class UsingCustomCollection : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       Customer c1 = new Customer("1", "A", "AA", "123-4567");
       Customer c2 = new Customer("2", "B", "BB", "123-1267");
       Customer c3 = new Customer("3", "C", "CC", "123-7823");
       Customer c4 = new Customer("4", "D", "DD", "123-6383");
       CustomerCollection cc = new CustomerCollection();
       cc.Add(c1);
       cc.Add(c2);
       cc.Add(c3);
       cc.Add(c4);
       drpSample.DataSource = cc;
       drpSample.DataBind();
    }
}
public class CustomerCollection: IEnumerable
{
   private ArrayList _customers;
  public CustomerCollection()
  {
      _customers = new ArrayList();
  }
   public void Add(Customer c)
   {
      if (c != null)
         _customers.Add(c);
   }

   public Customer this[int index]
   {
      get {
         if (index >= 0 && index < _customers.Count)
            return (Customer)_customers[index];
         else
            return null;
      }
   }
   public Customer FindById(string id)
   {
      foreach (Customer c in _customers)
      {
         if (c.Id == id)
            return c;
      }
      return null;
   }
   public IEnumerator GetEnumerator()
   {
      return _customers.GetEnumerator();
   }
}
public abstract class AbstractEntity
{
   private string _id;
  public AbstractEntity(string id)
  {
      _id = id;
  }

   public string Id
   {
      get { return _id; }
      set { _id = value; }
   }

   public abstract bool IsValid
   {
      get;
   }
}
public class Customer: AbstractEntity 
{
   // data members
   private string _firstName;
   private string _lastName;
   private string _phone;
   public Customer(string id, string firstName, string lastName, string phone): base(id)
   {
      _firstName = firstName;
      _lastName = lastName;
      _phone = phone;
   }

   public string FirstName
   {
      get { return _firstName; }
      set { _firstName = value; }
   }
   public string LastName
   {
      get { return _lastName; }
      set { _lastName = value; }
   }
   public string Phone
   {
      get { return _phone; }
      set { _phone = value; }
   }
   public string Name
   {
      get { return LastName + ", " + FirstName; }
   }
   public override bool IsValid
   {
      get
      {
         if (Id.Length > 0 && LastName.Length > 0)
            return true;
         else
            return false;
      }
   }
   public override string ToString()
   {
      return Id + "," + Name + "," + Phone; 
   }
}