ASP.NET Tutorial/Collections/Array
Содержание
- 1 Array binding
- 2 Array with custom objects
- 3 Define and use array in asp.net page (C#)
- 4 Define and use array in asp.net page (VB)
- 5 Looking for an object in an array by reference (C#)
- 6 Looking for an object in an array by reference (VB)
- 7 Searching for an equivalent object with Array.BinarySearch (C#)
- 8 Searching for an equivalent object with Array.BinarySearch (VB)
- 9 Searching for a object in an array by reference (C#)
- 10 Searching for a object in an array by reference (VB)
- 11 Sorting arrays of Person (C#)
- 12 Sorting arrays of Person (VB)
Array binding
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="SimpleArrayDataBinding" %>
<!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>Simple Data Binding to Array</title>
</head>
<body>
<form id="form1" runat="server">
<div id="container">
<h1>Simple Data Binding to Array</h1>
This example illustrates simple data binding. The drop-down list is data bound to an array.
<asp:DropDownList ID="drpSample" runat="server" />
</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 SimpleArrayDataBinding : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string[] names;
names = new string[3] { "Austen", "Dante", "Goethe" };
drpSample.DataSource = names;
drpSample.DataBind();
}
}
Array with custom objects
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="UsingArrays" %>
<!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>Using Arrays</title>
</head>
<body>
<form id="form1" runat="server">
<div id="container">
<h1>Using Objects in an Array</h1>
This example illustrates how an array can contain multiple objects.
<asp:Label ID="labMsg" runat="server" />
</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 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
{
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;
}
}
public partial class UsingArrays : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Customer[] myCustomers = new Customer[] {
new Customer("1", "A", "AA", "123-4567"),
new Customer("2", "B", "BB", "456-1267"),
new Customer("3", "C", "CC", "564-7823"),
new Customer("4", "D", "DD", "253-6383")
};
Customer c1 = myCustomers[2];
labMsg.Text = c1.FirstName + "<br/>";
labMsg.Text += myCustomers.GetUpperBound(0) + "," + myCustomers.Length ;
labMsg.Text += "<h2>Iteration Output</h2>";
IEnumerator ie = myCustomers.GetEnumerator();
while (ie.MoveNext())
{
Customer c = (Customer)ie.Current;
labMsg.Text += c.FirstName + "<br/>";
}
}
}
Define and use array in asp.net page (C#)
<%@ Page Language="C#" %>
<script runat="server">
string[] arrColors = new string[5];
void Page_Load(Object Sender, EventArgs e) {
arrColors[0] = "green";
arrColors[1] = "red";
arrColors[2] = "yellow";
arrColors[3] = "blue";
arrColors[4] = "violet";
Response.Write("The first element is: ");
Response.Write(arrColors[0] + "");
Response.Write("The third element is: ");
Response.Write(arrColors[2] + "");
Response.Write("The 5-3 element is: ");
Response.Write(arrColors[5-3] + "");
}
</script>
<html><body>
</body></html>
Define and use array in asp.net page (VB)
<%@ Page Language="VB" %>
<script runat="server">
dim arrColors(5) as String
sub Page_Load(Sender as object, e as eventargs)
arrColors(0) = "green"
arrColors(1) = "red"
arrColors(2) = "yellow"
arrColors(3) = "blue"
arrColors(4) = "violet"
Response.Write("The first element is: ")
Response.Write(arrColors(0) & "")
Response.Write("The third element is: ")
Response.Write(arrColors(2) & "")
Response.Write("The 5-3 element is: ")
Response.Write(arrColors(5-3) & "")
end sub
</script>
<html><body>
</body></html>
Looking for an object in an array by reference (C#)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Person scott = new Person("A", "B");
Person bill = new Person("C", "D");
Person srini = new Person("E", "F");
Person[] people = { bill, scott, srini };
int indexOfC = Array.IndexOf(people, bill);
Response.Write("C is at " + indexOfC + "<BR/>");
int indexOfA = Array.IndexOf(people, scott);
Response.Write("A is at " + indexOfA + "<BR/>");
}
}
public class Person
{
string FirstName;
string LastName;
public Person(string first, string last)
{
FirstName = first;
LastName = last;
}
public string FullName
{
get
{
return FirstName + " " + LastName;
}
}
}
Looking for an object in an array by reference (VB)
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" 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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
File: Default.aspx.vb
Imports Microsoft.VisualBasic
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
Dim scott As New Person("A", "B")
Dim bill As New Person("C", "D")
Dim srini As New Person("E", "F")
Dim people() As Person = {bill, scott, srini}
Dim indexOfC As Integer = Array.IndexOf(people, bill)
Response.Write("C is at " & indexOfC & "<BR/>")
Dim indexOfA As Integer = Array.IndexOf(people, scott)
Response.Write("A is at " & indexOfA & "<BR/>")
End Sub
End Class
Public Class Person
Dim FirstName As String
Dim LastName As String
Public Sub New(ByVal First As String, ByVal Last As String)
FirstName = First
LastName = Last
End Sub
Public ReadOnly Property FullName() As String
Get
Return FirstName & " " & LastName
End Get
End Property
End Class
Searching for an equivalent object with Array.BinarySearch (C#)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Person scott = new Person("A", "B");
Person bill = new Person("C", "D");
Person srini = new Person("E", "F");
Person[] people = { bill, scott, srini };
int indexOfC = Array.IndexOf(people, bill);
Response.Write("C is at " + indexOfC + "<BR/>");
int indexOfA = Array.IndexOf(people, scott);
Response.Write("A is at " + indexOfA + "<BR/>");
Person scott2 = new Person("A", "B");
int indexOfA2 = Array.IndexOf(people, scott2);
Response.Write("A #2 is at " + indexOfA2 + "<BR/>");
int indexOfEquivalentA = Array.BinarySearch(people, scott2);
Response.Write("An Equivalent A is at " + indexOfEquivalentA + "<BR/>");
}
}
public class Person : IComparable
{
string FirstName;
string LastName;
public Person(string first, string last)
{
FirstName = first;
LastName = last;
}
public string FullName
{
get
{
return FirstName + " " + LastName;
}
}
int IComparable.rupareTo(object obj)
{
Person p2 = obj as Person;
if (p2 == null) throw new ArgumentException("Object is not a Person!");
int lastNameResult = this.LastName.rupareTo(p2.LastName);
if (lastNameResult == 0)
{
int firstNameResult = this.FirstName.rupareTo(p2.FirstName);
return firstNameResult;
}
else
{
return lastNameResult;
}
}
}
Searching for an equivalent object with Array.BinarySearch (VB)
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" 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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
File: Default.aspx.vb
Imports Microsoft.VisualBasic
Public Class Person
Implements IComparable
Dim FirstName As String
Dim LastName As String
Public Sub New(ByVal First As String, ByVal Last As String)
FirstName = First
LastName = Last
End Sub
Public ReadOnly Property FullName() As String
Get
Return FirstName & " " & LastName
End Get
End Property
Public Function CompareTo(ByVal obj As Object) _
As Integer Implements IComparable.rupareTo
If Not TypeOf (obj) Is Person Then
Throw New ArgumentException("Object is not a Person!")
End If
Dim p2 As Person = CType(obj, Person)
Dim lastNameResult As Integer = Me.LastName.rupareTo(p2.LastName)
If lastNameResult = 0 Then
Dim firstNameResult As Integer = Me.FirstName.rupareTo(p2.FirstName)
Return firstNameResult
Else
Return lastNameResult
End If
End Function
End Class
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
Dim scott As New Person("A", "B")
Dim bill As New Person("C", "D")
Dim srini As New Person("E", "F")
Dim people() As Person = {bill, scott, srini}
Dim indexOfC As Integer = Array.IndexOf(people, bill)
Response.Write("C is at " & indexOfC & "<BR/>")
Dim indexOfA As Integer = Array.IndexOf(people, scott)
Response.Write("A is at " & indexOfA & "<BR/>")
Dim scott2 As New Person("A", "B")
Dim indexOfA2 As Integer = Array.IndexOf(people, scott2)
Response.Write("A #2 is at " & indexOfA2 & "<BR/>")
Dim indexOfEquivalentA As Integer = Array.BinarySearch(people, scott2)
Response.Write("An Equivalent A is at " & indexOfEquivalentA & "<BR/>")
End Sub
End Class
Searching for a object in an array by reference (C#)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Person scott = new Person("A", "B");
Person bill = new Person("C", "D");
Person srini = new Person("E", "F");
Person[] people = { bill, scott, srini };
int indexOfC = Array.IndexOf(people, bill);
Response.Write("C is at " + indexOfC + "<BR/>");
int indexOfA = Array.IndexOf(people, scott);
Response.Write("A is at " + indexOfA + "<BR/>");
Person scott2 = new Person("A", "B");
int indexOfA2 = Array.IndexOf(people, scott2);
Response.Write("A #2 is at " + indexOfA2 + "<BR/>");
}
}
public class Person
{
string FirstName;
string LastName;
public Person(string first, string last)
{
FirstName = first;
LastName = last;
}
public string FullName
{
get
{
return FirstName + " " + LastName;
}
}
}
Searching for a object in an array by reference (VB)
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" 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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
File: Default.aspx.vb
Imports Microsoft.VisualBasic
Public Class Person
Dim FirstName As String
Dim LastName As String
Public Sub New(ByVal First As String, ByVal Last As String)
FirstName = First
LastName = Last
End Sub
Public ReadOnly Property FullName() As String
Get
Return FirstName & " " & LastName
End Get
End Property
End Class
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
Dim scott As New Person("A", "B")
Dim bill As New Person("C", "D")
Dim srini As New Person("E", "F")
Dim people() As Person = {bill, scott, srini}
Dim indexOfC As Integer = Array.IndexOf(people, bill)
Response.Write("C is at " & indexOfC & "<BR/>")
Dim indexOfA As Integer = Array.IndexOf(people, scott)
Response.Write("A is at " & indexOfA & "<BR/>")
Dim scott2 As New Person("A", "B")
Dim indexOfA2 As Integer = Array.IndexOf(people, scott2)
Response.Write("A #2 is at " & indexOfA2 & "<BR/>")
End Sub
End Class
Sorting arrays of Person (C#)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Person scott = new Person("A", "B");
Person bill = new Person("C", "D");
Person srini = new Person("E", "F");
Person[] people = { scott, bill, srini };
Response.Write("Unsorted. We used foreach.<BR/>");
foreach (Person p in people)
{
Response.Write(p.FullName + "<BR/>");
}
Response.Write("Sort...<BR/>");
Array.Sort(people);
Response.Write("Sorted. We used foreach.<BR/>");
foreach (Person p in people)
{
Response.Write(p.FullName + "<BR/>");
}
}
}
public class Person : IComparable
{
string FirstName;
string LastName;
public Person(string first, string last)
{
FirstName = first;
LastName = last;
}
public string FullName
{
get
{
return FirstName + " " + LastName;
}
}
int IComparable.rupareTo(object obj)
{
Person p2 = obj as Person;
if (p2 == null) throw new ArgumentException("Object is not a Person!");
int lastNameResult = this.LastName.rupareTo(p2.LastName);
if (lastNameResult == 0)
{
int firstNameResult = this.FirstName.rupareTo(p2.FirstName);
return firstNameResult;
}
else
{
return lastNameResult;
}
}
}
Sorting arrays of Person (VB)
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" 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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
File: Default.aspx.vb
Imports Microsoft.VisualBasic
Public Class Person
Implements IComparable
Dim FirstName As String
Dim LastName As String
Public Sub New(ByVal First As String, ByVal Last As String)
FirstName = First
LastName = Last
End Sub
Public ReadOnly Property FullName() As String
Get
Return FirstName & " " & LastName
End Get
End Property
Public Function CompareTo(ByVal obj As Object) _
As Integer Implements IComparable.rupareTo
If Not TypeOf (obj) Is Person Then
Throw New ArgumentException("Object is not a Person!")
End If
Dim p2 As Person = CType(obj, Person)
Dim lastNameResult As Integer = Me.LastName.rupareTo(p2.LastName)
If lastNameResult = 0 Then
Dim firstNameResult As Integer = Me.FirstName.rupareTo(p2.FirstName)
Return firstNameResult
Else
Return lastNameResult
End If
End Function
End Class
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
Dim scott As New Person("A", "B")
Dim bill As New Person("C", "D")
Dim srini As New Person("E", "F")
Dim people() As Person = {scott, bill, srini}
Response.Write("Unsorted. We used foreach.<BR/>")
For Each p As Person In people
Response.Write(p.FullName & "<BR/>")
Next
Response.Write("Sort...<BR/>")
Array.Sort(people)
Response.Write("Sorted. We used foreach.<BR/>")
For Each p As Person In people
Response.Write(p.FullName & "<BR/>")
Next
End Sub
End Class