To assist you with this, the .NET Framework provides the IEnumerator and the IEnumerable interfaces that are defined in the System.Collections namespace. Their generic equivalences can be found in the System.Collections.Generic namespace. After implementing these interfaces, you can use the foreach operator to visit each value of the database. To implement the System.Collections.IEnumerator interface, you must derive a class from it. Then, you must define the Reset(), the MoveNext() methods, and the Current property. Here is an example: using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections;
/// <summary>
/// Summary description for Enumerator
/// </summary>
public class Enumerator : IEnumerator
{
private string[] names;
private int cur;
public Enumerator(string[] list)
{
this.names = list;
cur = -1;
}
public Object Current
{
get { return names[cur]; }
}
public void Reset()
{
cur = -1;
}
public bool MoveNext()
{
cur++;
if (cur < names.Length)
return true;
else
return false;
}
}
To implement the System.Collections.IEnumerable interface, you must derive a class from it. When implementing the class, you must define an accessory method and the GetEnumerator() method that returns an IEnumerator object. Here is an example: using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections;
/// <summary>
/// Summary description for Enumerable
/// </summary>
public class Enumerable : IEnumerable
{
private string[] names;
public Enumerable()
{
}
public void Identify(string[] values)
{
names = values;
for (int i = 0; i < values.Length; i++)
names[i] = values[i];
}
public IEnumerator GetEnumerator()
{
return new Enumerator(names);
}
}
Once you have implemented the interfaces, you can use foreach. Here is an example: <%@ Page Language="C#" AutoEventWireup="true"
CodeFile="index.aspx.cs" Inherits="_Default" %>
<!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 runat="server">
<title>People Information</title>
</head>
<body>
<form id="frmPeople" runat="server">
<div>
<asp:ListBox ID="lbxNames" runat="server" Height="144px" Width="172px">
</asp:ListBox>
</div>
</form>
</body>
</html>
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var FullNames = new string[8];
FullNames[0] = "Gertrude Monay";
FullNames[1] = "Paul Bertrand Yamaguchi";
FullNames[2] = "Hermine Ngaleu";
FullNames[3] = "Francine Mukoko";
FullNames[4] = "Joseph Walters";
FullNames[5] = "Patricia Katts";
FullNames[6] = "Helen Cranston";
FullNames[7] = "Paul Motto";
var coll = new Enumerable();
coll.Identify(FullNames);
foreach (string s in coll)
lbxNames.Items.Add(s);
}
}
While the IEnumerator and the IEnumerable interfaces serve as valuable accessories that allow a collection class to support enumeration, to actually create a collection class, there are other interfaces you can use to implement the functionality you want for your collection. When you want to use a collection in your application, you may first check what classes are available in the .NET Framework. If you don't find a suitable class, you can create your own that implements one or more interfaces. As it happens, the .NET Framework ships with many of them and your next step is to choose which one you prefer. Some of the most commonly used interfaces are
|
|||||||||||||||||||||||||