VB.NET集合另类使用方法详解

开发 后端
VB.NET集合不但可以用来处理Object数据类型,而且还能用来处理各种数据类型。在特定的环境中,VB.NET集合的应用可以提高编程效率。

通过对VB.NET的深入解读,可以知道,它并不仅仅是一个版本的升级,它的作用为大家带来非常多的好处。在这里我们可以通过对VB.NET集合的不同的使用方法来解读这门语言的具体应用技巧。#t#

尽管VB.NET集合一般是用来处理 Object 数据类型的,但它也可以用来处理任何数据类型。有时用集合存取数据比用数组更加有效。

如果需要更改数组的大小,必须使用 ReDim 语句 (Visual Basic)。当您这样做时,Visual Basic 会创建一个新数组并释放以前的数组以便处置。这需要一定的执行时间。因此,如果您处理的项数经常更改,或者您无法预测所需的最大项数,则可以使用集合来获得更好的性能。

集合不用创建新对象或复制现有元素,它在处理大小调整时所用的执行时间比数组少,而数组必须使用 ReDim。但是,如果不更改或很少更改大小,数组很可能更有效。一直以来,性能在很大程度上都依赖于个别的应用程序。您应该花时间把数组和集合都尝试一下。

专用VB.NET集合

下面的示例使用 .NET Framework 泛型类 System.Collections.Generic..::.List<(Of <(T>)>) 来创建 customer 结构的列表集合。

代码

  1. ' Define the structure for a 
    customer.  
  2. Public Structure customer  
  3. Public name As String  
  4. ' Insert code for other members
     of customer structure.  
  5. End Structure  
  6. ' Create a module-level collection 
    that can hold 200 elements.  
  7. Public custFile As New List
    (Of customer)(200)   
  8. ' Add a specified customer 
    to the collection.  
  9. Private Sub addNewCustomer
    (ByVal newCust As customer)  
  10. ' Insert code to perform 
    validity check on newCust.  
  11. custFile.Add(newCust)  
  12. End Sub  
  13. ' Display the list of 
    customers in the Debug window.  
  14. Private Sub printCustomers()  
  15. For Each cust As customer 
    In custFile  
  16. Debug.WriteLine(cust)  
  17. Next cust  
  18. End Sub 

注释

custFile 集合的声明指定了它只能包含 customer 类型的元素。它还提供 200 个元素的初始容量。过程 addNewCustomer 检查新元素的有效性,然后将新元素添加到集合中。过程 printCustomers 使用 For Each 循环来遍历集合并显示VB.NET集合的元素。

责任编辑:曹凯 来源: wewill.cn
相关推荐

2010-01-19 09:36:06

VB.NET Func

2010-01-21 14:06:03

VB.NET MyCl

2010-01-21 17:23:05

VB.NET Radi

2010-01-20 17:47:54

VB.NET注释

2009-10-26 13:36:58

VB.NET Spli

2010-01-18 13:12:43

VB.NET控件数组

2010-01-20 13:28:35

VB.NET计算数字

2010-01-21 10:48:18

VB.NET扩展方法

2010-01-20 10:27:07

VB.NET隐式类型局

2009-10-13 15:20:02

VB.NET使用Dra

2009-11-02 14:35:52

VB.NET打包

2011-05-20 16:34:35

VB.NET

2010-01-18 19:04:29

VB.NET组件叠加

2010-01-21 17:34:48

VB.NET Bool

2009-10-20 17:38:54

VB.NET Comp

2010-01-12 18:35:43

VB.NET Stru

2010-01-15 16:46:05

VB.NET集合存储

2010-01-19 16:55:46

VB.NET声明语句

2009-11-03 09:26:13

VB.NET方法

2009-10-12 15:44:26

VB.NET动态编码技
点赞
收藏

51CTO技术栈公众号