分享
 
 
 

The System.Collections Namespace

王朝asp·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

The System.Collections Namespace

Use the System.Collections namespace when you need to create a data

structure that can hold a group of similar objects. The System.Collections

namespace contains all the classes and interfaces needed to define

collections of objects. Some of the classes that you might use most often

are listed in Table 4.4.

Table 4.4. System.Collections Classes Class Description

ArrayList An ArrayList is similar to an array, but the ArrayList class

allows you to add items without managing the size of the list yourself

(much like a VB Collection object).

CollectionBase This class is the base for a strongly typed collection,

which is critical for creating collection classes.

DictionaryBase This class is the base for a strongly typed collection

utilizing associated keys and values. This is similar to the Dictionary

object found in VBScript and used by many ASP developers.

SortedList This class represents a collection of keys and values that you

insert into the list. A SortedList object is always automatically sorted by

the key value. You can access the values with either the key value or an

index number.

A common use of the System.Collections namespace is to create strongly

typed collections. Consider the following scenario:

Assume you create a class called Customer. The Customer class has a number

of properties, methods, and events. You now want to create several Customer

objects within one structure so you can iterate through the collection and

print each customer to the printer. You could just create a variable of

type Array, ArrayList, or SortedList, but these standard collections can

hold any type of object. In other words, you could add a Customer object as

the first item in the collection, but you could then add an Employee object

as the second item, a String object as the third, and so on. If you try to

iterate over this collection and apply the Update method, it might work on

the Customer and Employee objects, but when your loop attempts to call the

Update method of a String object, it will certainly fail.

To get around this sticky situation, you can create your own class that

looks like a generic collection object but restricts its members to only

one data type. This new class will include the standard methods of the

collection base type, such as the Add and Remove methods, as well as an

Item property, but your class's Add method will only allow you to add

Customer objects.

In Visual Basic 6.0, you could provide this functionality, and you

generally started by creating an empty class and then creating an Add

method, a Remove method, an Item property, a Count property, and so on.

This is easier in .NET, thanks to the base classes in the

System.Collections namespace.

A System.Collections Example

Assume that you need to create a Customer object to hold values from a

Customer table in your database. The structure of the Customer object is

very simple, containing just a CompanyName property. Imagine that you want

to create a collection class named Customers (note the letter s on the end)

to hold a set of Customer objects. You first need to create the Customer

class as shown in the following code snippet:

Public Class Customer

Public CustomerName As String

End Class

Next, you create your new Customers collection class. Your class must

inherit from the System.Collections.CollectionBase class. (This base class

gives you all the functionality of a normal collection but allows you to

override any of the base methods, such as Add and Item.) Listing 4.3

provides the new class and its overridden Add and Item members:

Listing 4.3 Building Collection Classes Is Easy with the .NET Framework

Class CollectionBase

Public Class Customers

Inherits System.Collections.CollectionBase

Public Sub Add(ByVal cust As Customer)

Me.List.Add(cust)

End Sub

Public ReadOnly Property Item( _

ByVal Index As Integer) As Customer

Get

If Index > Count - 1 Or Index < 0 Then

' Return error message

Else

Return CType( _

Me.List.Item(Index), Customer)

End If

End Get

End Property

End Class

In your collection class, you'll need to override the Add method so that

instead of accepting a generic Object type, your method accepts only a

Customer object. You can then use the internal List property from the base

class to add the Customer object to the internal collection of generic

objects.

You also need to override the Item method so it returns a Customer object

instead of a generic Object data type. Notice the use of the CType

function. This function converts the data in the built-in List property

from a generic Object data type to a Customer data type.

Using inheritance, you certainly end up writing a lot less code to

implement a collection class when compared to the code you had to write in

Visual Basic 6.0. To use the collection class you created in Listing 4.3,

you could write code like that shown in Listing 4.4.

Listing 4.4 Collection Classes Are Very Easy to Use

Private Sub CustCollection()

Dim cust As Customer

Dim colcust As New Customers()

Dim strMsg As String

cust = New Customer()

cust.CustomerName = "Microsoft Corporation"

colcust.Add(cust)

cust = New Customer()

cust.CustomerName = "SAMS Publishing"

colcust.Add(cust)

strMsg = "Count = " & _

colcust.Count.ToString() & "<BR>"

For Each cust In colcust

strMsg &= cust.CustomerName & "<BR>"

Next

lblMsg.Text = strMsg

colcust.RemoveAt(0)

lblMsg.Text = strMsg & _

"After the removal = " & colcust.Count

End Sub

This code creates two new Customer objects, sets the CustomerName property

of each to a unique name, and then adds each one to the Customers

collection class. You can use the Count property from the base class to

determine the number of items in the collection, even though you did not

implement it in your Customers class. You can use the For Each iterator to

loop through each Customer object. You can also utilize the base class's

RemoveAt method to remove a specific item at a specified index in the

collection.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有