asp.net外文翻譯_第1頁(yè)
已閱讀1頁(yè),還剩8頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、<p>  asp.net外文翻譯 </p><p> 譯文:常見的ASP.NET代碼技術(shù) ASP.NET:技巧,教程,代碼——Scott Mitchell 利用集合大多數(shù)現(xiàn)代編程語(yǔ)言提供支持某種類型的對(duì)象,可以容納一個(gè)可變數(shù)目的元素。這些對(duì)象被稱為集合,他們可以輕易地添加和刪除元素,而不必?fù)?dān)心適當(dāng)?shù)膬?nèi)存分配。如果你使用經(jīng)典ASP編程之前,你就可能已經(jīng)熟悉了腳本,字典對(duì)象,采集對(duì)象的每個(gè)元素包含一

2、個(gè)參考文本的關(guān)鍵。這種方式存儲(chǔ)對(duì)象的集合被稱為一個(gè)哈希表。有許多類型的集合,除了哈希表。每一種類型的集合是相似的目的:它作為一種手段來存儲(chǔ)不同數(shù)量的元素,提供一種簡(jiǎn)單的方法,在最小程度上添加和刪除元素。每一個(gè)不同類型的集合是唯一的方法儲(chǔ)存、檢索并借鑒它的各種因素,而.NET框架提供了很多的集合類型為開發(fā)人員使用。事實(shí)上,整個(gè)的命名空間系統(tǒng)集合是專門從事集合類型和輔助課程。這些類型的集合都可以存儲(chǔ)對(duì)象類型的元素。因?yàn)樵?NET中所有的原始

3、數(shù)據(jù)類型的字符串,整數(shù),日期/時(shí)間,陣列,都是從目標(biāo)類派生的,這些集合可以從字面上存儲(chǔ)任何東西。例如,你可以使用一個(gè)單一的收集,存儲(chǔ)一個(gè)整數(shù),一個(gè)典型的COM組件,字符串,日期/時(shí)間,和自定義編寫的.NET組件的兩個(gè)</p><p><b>  原文:</b></p><p>  Common ASP.NET Code Techniques</p>&l

4、t;p>  ASP.NET: Tips, Tutorials,and Code—Scott Mitchell</p><p>  Using Collections</p><p>  Most modern programming languages provide support for some type of object that can hold a variable n

5、umber of elements. These objects are referred to as collections, and they can have elements added and removed with ease without having to worry about proper memory allocation.If you’ve programmed with classic ASP before,

6、 you’re probably familiar with the Scripting.Dictionary object, a collection object that references each element with a textual key. A collection that stores objects in this fashio</p><p>  There are many ty

7、pes of collections in addition to the hash table. Each type of collection is similar in purpose: it serves as a means to store a varying number of elements, providing an easy way, at a minimum, to add and remove elements

8、. Each different type of collection is unique in its method of storing, retrieving, and referencing its various elements. </p><p>  The .NET Framework provides a number of collection types for the developer

9、to use. In fact, an entire namespace, System.Collections, is dedicated to collection types and helper classes.</p><p>  Each of these collection types can store elements of type Object. Because in .NET all p

10、rimitive data types—string, integers, date/times, arrays, and so on—are derived from the Object class, these collections can literally store anything! For example, you could use a single collection to store a couple of i

11、ntegers, an instance of a classic COM component, a string, a date/time, and two instances of a custom-written .NET component. Most of the examples in this section use collections to house primi</p><p>  Thro

12、ughout this section we’ll examine five collections the .NET Framework offers developers: the ArrayList, the Hashtable, the SortedList, the Queue, and the Stack. As you study each of these collections, realize that they a

13、ll have many similarities. For example, each type of collection can be iterated through element-by-element using a For Each ... Next loop in VB (or a foreach loop in C#). Each collection type has a number of similarly na

14、med functions that perform the same tasks. For example, </p><p>  Working with the ArrayList Class</p><p>  The first type of collection we’ll look at is the ArrayList. With an ArrayList, each i

15、tem is</p><p>  stored in sequential order and is indexed numerically. In our following examples, keep in mind that the developer need not worry himself with memory allocation. With the standard array, the d

16、eveloper cannot easily add and remove elements without concerning himself with the size and makeup of the array. With all the collections we’ll examine in this chapter,this is no longer a concern.</p><p>  A

17、dding Elements to an ArrayList</p><p>  We create two ArrayList class instances, aTerritories and aStates, on lines</p><p>  5 and 6, respectively. We then populate the aStates ArrayList with a

18、small subset of the 50 states of the United States using the Add method. The Add method takes one parameter, the element to add to the array, which needs to be of type Object. This Object instance is then appended to the

19、 end of the ArrayList. In this example we are simply adding elements of type String to the ArrayList aStates and aTerritories.</p><p>  The Add method is useful for adding one element at a time to the end of

20、 the array, but what if we want to add a number of elements to an ArrayList at once. The ArrayList class provides the AddRange method to do just this. AddRange expects a single parameter that supports the ICollection int

21、erface. A wide number of .NET Framework classes—such as the Array, ArrayList, DataView, DataSetView, and others—support this interface. We use the AddRange method to add each element of the aStates ArrayList t</p>

22、<p>  Because ArrayLists are ordered sequentially, there might be times when we want to add an element to a particular position. The Insert method of the ArrayList class provides this capability, allowing the devel

23、oper to add an element to a specific spot in the ArrayList collection. The Insert method takes two parameters: an integer representing the index in which you want to add the new element, and the new element, which needs

24、to be of type Object. In line 23 we add a new string to the start of the</p><p>  Removing Elements from an ArrayList</p><p>  The ArrayList class also provides a number of methods for removing

25、elements. We can</p><p>  remove a specific element from an ArrayList with the Remove method. If you attempt to remove an element that does not exist, an ArgumentException exception will be thrown.Remove all

26、ows you to take out a particular element from an ArrayList; RemoveAt, allows the developer to remove an element at a specific position in the ArrayList.</p><p>  Both Remove and RemoveAt dissect only one ele

27、ment from the ArrayList at a time. We can</p><p>  remove a chunk of elements in one fell swoop by using the RemoveRange method. This method expects two parameters: an index to start at and a count of total

28、elements to remove. Finally, to remove all the contents of an ArrayList, use the Clear method .</p><p>  Working with the Hashtable Class</p><p>  The type of collection most developers are used

29、 to working with is the hash table collection. Whereas the ArrayList indexes each element numerically, a hash table indexes each element by an alphanumeric key. The Collection data type in Visual Basic is a hash table; t

30、he Scripting.Dictionary object, used commonly in classic ASP pages, is a simple hash table. The .NET Framework provides developers with a powerful hash table class, Hashtable.</p><p>  When working with the

31、Hashtable class, keep in mind that the ordering of elements in the collection are irrespective of the order in which they are entered. The Hashtable class employs its own hashing algorithm to efficiently order the key/va

32、lue pairs in the collection. If it is essential that a collection’s elements be ordered alphabetically by the value of their keys, use the SortedList class, which is discussed in the next section, “Working with the Sorte

33、dList Class.”</p><p>  Working with the SortedList Class</p><p>  So far we’ve examined two collections provided by the .NET Framework: the Hashtable class and the ArrayList class. Each of these

34、 collections indexes elements in a different manner. The ArrayList indexes each element numerically, whereas the Hashtable indexes each element with an alphanumeric key. The ArrayList orders each element sequentially, ba

35、sed on its numerical index; the Hashtable applies a seemingly random ordering (because the order is determined by a hashing algorithm).</p><p>  What if you need a collection, though, that allows access to e

36、lements by both an alphanumeric key and a numerical index? The .NET Framework includes a class that permits both types of access, the SortedList class. This class internally maintains two arrays: a sorted array of the ke

37、ys and an array of the values.</p><p>  Adding, Removing, and Indexing Elements in a SortedList</p><p>  Because the SortedList orders its elements based on the key, there are no methods that in

38、sert elements in a particular spot. Rather, similar to the Hashtable class, there is only a single method to add elements to the collection: Add. However, because the SortedList can be indexed by both key and value, the

39、class contains both Remove and RemoveAt methods. As with all the other collection types, the SortedList also contains a Clear method that removes all elements.</p><p>  Because a SortedList encapsulates the

40、functionality of both the Hashtable and ArrayList</p><p>  classes, it’s no wonder that the class provides a number of methods to access its elements. As with a Hashtable, SortedList elements can be accessed

41、 via their keys. A SortedList that stored Integer values could have an element accessed similar to the following:</p><p>  Dim SortedListValue as Integer</p><p>  SortedListValue = slSortedListI

42、nstance(key)</p><p>  The SortedList also can access elements through an integral index, like with the ArrayList</p><p>  class. To get the value at a particular index, you can use the GetByInde

43、x method as follows:</p><p>  Dim SortedListValue as Integer</p><p>  SortedListValue = slSortedListInstance.GetByIndex(iPosition)</p><p>  iPosition represents the zero-based ordin

44、al index for the element to retrieve from</p><p>  slSortedListInstance. Additionally, elements can be accessed by index using the</p><p>  GetValueList method to return a collection of values,

45、which can then be accessed by index:</p><p>  Dim SortedListValue as Integer</p><p>  SortedListVluae = slSortedListInstance.GetValueList(iPosition)</p><p>  Working with the Queue

46、Class</p><p>  ArrayLists, Hashtables, and SortedLists all have one thing in common—they allow random</p><p>  access to their elements. That is, a developer can programmatically read, write, or

47、 remove any element in the collection, regardless of its position. However, the Queue and Stack classes (the remaining two collections we’ll examine) are unique in that they provide sequential access only. Specifically,

48、the Queue class can only access and remove elements in the order they were inserted.</p><p>  Adding, Removing, and Accessing Elements in a Queue</p><p>  Queues are often referred to as First I

49、n, First Out (FIFO) data structures because the Nth element inserted will be the Nth element removed or accessed. It helps to think of the queue data structure as a line of people. There are two parts to a queue as there

50、 are two parts to any line up: the tail of the queue, where people new to the line start waiting, and the head of the queue, where the next person in line waits to be served. In a line, the person who is standing in line

51、 first will be first </p><p>  The .NET Framework provides support for the queue data structure with the Queue class. To add an element to the tail, use the Enqueue method. To retrieve and remove an element

52、from the head of a queue, use Dequeue. As with the other collection types we’ve examined thus far, the Queue class contains a Clear method to remove all elements. To simply examine the element at the head without alterin

53、g the queue, use the Peek method. As with all the other collections, the elements of a Queue can be iter</p><p>  Working with the Stack Class</p><p>  A stack is a data structure similar to a q

54、ueue in that is supports only sequential access.However, a stack does bear one major difference from a queue: rather than storing elements with a First In, First Out (FIFO) semantic, a stack uses Last In, First Out (LIFO

55、). A crowded elevator behaves similar to a stack: the first person who enters the crowded elevator is the last person to leave, whereas the last person to board the elevator is the first out when it reaches its destinati

56、on.</p><p>  Adding, Removing, and Accessing Elements in a Stack</p><p>  The .NET Framework provides an implementation of the stack data type with the Stack class. A stack has two basic operati

57、ons: adding an element to the top of the stack, which is accomplished with the Push method, and removing an element from the top of the stack, accomplished via the Pop method. Similar to the Queue class, the Stack class

58、also contains a Peek method to permit developers to access the top of the stack without removing the element.</p><p>  Similarities Among the Collection Types</p><p>  Because each collection ha

59、s the same basic functionality—to serve as a variable-sized storage medium for Objects—it is not surprising that the collection types have much in common with one another. All have methods to add and remove elements from

60、 the collection. The Count property, which returns the total number of elements in the collection, is common among all collection types.</p><p>  Conclusion</p><p>  The .NET Framework provides

61、developers with a number of powerful collection-type classes, greatly extending the functionality of the Scripting.Dictionary object, the sole collection type available for classic ASP developers. These collections, alth

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 眾賞文庫(kù)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論