对象和结构化的数据
集合
作为集合的数组:下面是一个简单的例子: <%@Page Language="c#" %> <script runat="server" Language="c#"> void Page_Load() { string[] AnimalArray = new string[5] { "Dog", "Cat", "Elephant", "Lion", "Cat"}; MyLabel.Text = AnimalArray.GetValue(2).ToString()+"<font color=red> "+Array.IndexOf(AnimalArray,"Cat")+"</font>"; } </script> <html> <asp:label id="MyLabel" runat="server" /> </html>
这个程序输出显然是:Elephant。
下面是一个对数组中某处遍历的程序: <%@Page Language="c#" %> <script runat="server" Language="c#"> void Page_Load() { int intCounter = -1; string[] AnimalArray = new string[5] { "Dog", "Cat", "Elephant", "Lion", "Cat"}; do { intCounter = Array.IndexOf(AnimalArray, "Cat", intCounter+1); MyText.InnerHtml += "AnimalArray[" + intCounter + "]<br/>"; } while (intCounter != Array.LastIndexOf(AnimalArray, "Cat")); } </script> <html> The string "Cat" occurs in the following elements: <br/> <div id="MyText" runat="server" /> </html>
颠倒数组中元素的顺序:Array.Reverse(Array1); 将元素排序:Array.Sort(Array1);
使用数组:
<%@Page Language="c#" %> <script runat="server" Language="c#"> void Page_Load() { string[] AnimalArray = new string[5] { "Dog", "Cat", "Elephant", "Lion", "Cat" }; Array.Reverse(AnimalArray); foreach (string strAnimal in AnimalArray) { MyDropDownList.Items.Add(strAnimal); } } </script> <html> <form id="Form1" method="post" runat="server"> <asp:dropdownlist id="MyDropDownList" runat="server" /> </form> </html>
数据绑定: 集合的通用功能是:添加一对语句就可以将集合指定为数据源。 如前面的程序:
MyDropDownList.DataSource=AnimalArray; MyDropDownList.DataBind();
ArrayList
定义:ArrayLins MyArrayList=new ArrayList(); 每一个新项都会自动添加到公文的末尾! 前面的程序可以修改成这样:
<%@Page Language="c#" %> <script runat="server" Language="c#"> void Page_Load() { ArrayList AnimalArrayList = new ArrayList(); AnimalArrayList.Add("Dog"); AnimalArrayList.Add("Cat"); AnimalArrayList.Add("Elephant"); AnimalArrayList.Add("Lion"); AnimalArrayList.Add("Cat"); MyDropDownList.DataSource = AnimalArrayList; MyDropDownList.DataBind(); } </script> <html> <form id="Form1" method="post" runat="server"> <asp:dropdownlist id="MyDropDownList" runat="server" /> </form> </html> ArrayList的一些方法: 添加: MyArrayList.Add(“pig“); 插入: MyArrayList.Insert(3,“long“); 删除: MyArrayList.RemoveAt(3); OR MyArrayList.Remove(“cat“);
HashTable
创建HashTable:HashTable myHashTable=new HashTable();
添加值的两种方式: myHashTable.Add([UK],“HongKong“); OR myHashTable[UK]=“Hongkong“;
Hashtable.Add() 采用两个参数,一个用于键,一个用于值。两者都属于类型对象。为键传递的值是整数,因此必须将其装箱以便作为对象进行传递。为值传递的值是字符串,它是引用类型,因此不对字符串进行装箱。每答对一个得一分。
e.g.: <%@Page Language="c#" debug="true" %> <script runat="server" Language="c#"> void Page_Load(object source, EventArgs e) { Hashtable myHashtable = new Hashtable(); myHashtable["UK"] = "United Kingdom"; myHashtable["US"] = "United States"; myHashtable["DE"] = "Germany"; if (!(Page.IsPostBack)) { foreach (DictionaryEntry Item in myHashtable) { ListItem newListItem = new ListItem(); newListItem.Text = Item.Value.ToString(); newListItem.Value = Item.Key.ToString(); myDropDownList.Items.Add(newListItem); } } } void Click(object source, EventArgs e) { myLabel.Text = myDropDownList.SelectedItem.Value; } </script> <html> <form runat="server"> <asp:dropdownlist id="myDropDownList" runat="server" /> <asp:button id="myButton" runat="server" text="OK" Onclick="Click" /> <br /><br /> <asp:Label id="myLabel" runat="server" text="" /> </form> </html>
SortedList
类似HashTable,其中的值排序按照健值排序,而不是值! 使用: <%@Page Language="c#" debug="true" %> <script runat="server" Language="c#"> void Page_Load(object source, EventArgs e) { SortedList mySortedList = new SortedList(); mySortedList["armadillo"]="any of a family ... small bony plates"; mySortedList["amaryllis"]="an autumn-flowering ... Hippeastrum or Sprekelia]"; mySortedList["zebra"]="any of several fleet ... white or buff"; mySortedList["artichoke"]="a tall composite herb ... cooked as a vegetable"; if (!(Page.IsPostBack)) { foreach (DictionaryEntry Item in mySortedList) { ListItem newListItem = new ListItem(); newListItem.Text = Item.Key.ToString(); newListItem.Value = Item.Value.ToString(); myDropDownList.Items.Add(newListItem); } } } void Click(object source, EventArgs e) { myLabel.Text = myDropDownList.SelectedItem.Value; } </script> <html> <form runat="server"> Pick a word from the list: <asp:dropdownlist id="myDropDownList" runat="server" /> <asp:button id="myButton" runat="server" text="OK" Onclick="Click" /> <br /><br /> <b>Definition: </b> <asp:Label id="myLabel" runat="server" text="" /> </form> </html>
(380)
[C语言系列]NET 中C#的switch语句的语法 [系统软件]托拽Explore中的文件到VB.net的窗口 [系统软件]Boost库在XP+Visual C++.net中的安装 [常用软件]新配色面板:Paint.Net3.0RC1官方下载 [常用软件]用内建的“Net Meeting”聊天 [VB.NET程序]Henry的VB.NET之旅(三)—共享成员 [VB.NET程序]Henry的VB.NET之旅(二)—构造与析构 [VB.NET程序]Henry的VB.NET之旅(一)—失踪的窗体 [VB.NET程序]在托盘上显示Balloon Tooltip(VB.NET) [VB.NET程序]Henry手记-VB.NET中动态加载Treeview节点(二)
|