Collections
2015-06-17 14:54
477 查看
VB.NET supports collections such as List and Dictionary, which are useful constructs when building applications that need to access a set of related information.
When building Lists containing objects, the Of keyword is used in the definition.
New elements are added to the List using its Add method.
Dictionary collections work similarly to Lists with the addition of a "key" portion to each element.
The data type KeyValuePair is useful when iterating through a Dictionary collection as the example below demonstrates.
The Dictionary method ContainsKey returns true if the parameter is found as a key value in the Dictionary object.
The Collections' Count property returns an Integer containing the number of members in the collection.
The Item method returns the member of the collection for the specified key.
Dim animal As Animal
animal = Zoo.Item(0) ' return the first animal in the Zoo List
The Remove method removes a member from a collection.
Zoo.Remove(animal) ' remove the first animal in the Zoo List (see above)
The following will not work because the new animal object is not an Animal object that is directly referenced within the Zoo List (even though it is defined the same)
animal = New Animal( "lion", "roar")
Zoo. Remove( animal)
The RemoveAt method removes a member from a collection at a specified index.
Zoo. RemoveAt(0) ' remove the current first animal in the Zoo List
Collections also support methods Sort and Reverse.
MSDN: Example using the Dictionary collection in VB from the MSDN site
When building Lists containing objects, the Of keyword is used in the definition.
New elements are added to the List using its Add method.
Dictionary collections work similarly to Lists with the addition of a "key" portion to each element.
The data type KeyValuePair is useful when iterating through a Dictionary collection as the example below demonstrates.
The Dictionary method ContainsKey returns true if the parameter is found as a key value in the Dictionary object.
The Collections' Count property returns an Integer containing the number of members in the collection.
The Item method returns the member of the collection for the specified key.
Dim animal As Animal
animal = Zoo.Item(0) ' return the first animal in the Zoo List
The Remove method removes a member from a collection.
Zoo.Remove(animal) ' remove the first animal in the Zoo List (see above)
The following will not work because the new animal object is not an Animal object that is directly referenced within the Zoo List (even though it is defined the same)
animal = New Animal( "lion", "roar")
Zoo. Remove( animal)
The RemoveAt method removes a member from a collection at a specified index.
Zoo. RemoveAt(0) ' remove the current first animal in the Zoo List
Collections also support methods Sort and Reverse.
MSDN: Example using the Dictionary collection in VB from the MSDN site
相关文章推荐
- 软件开发
- Ninject Lazy Load问题
- 排序算法基本思想
- (笔试题)合法字符串
- RequireJS 文件合并压缩
- Java操作HTML格式字符串的标签和属性
- Java Keytool使用方法
- WIN8系统域用户加入本机Administrators组的方法
- 自定义注解做数据验证
- 虚拟机Nat网络模式下本地连接显示为网络电缆被拔出
- Android利用Fiddler进行网络数据抓包
- php扩展开发
- java JdbcTemplate源码
- WCF 宿主与通信模式(二)
- Selenium2学习-012-WebUI自动化实战实例-010-解决元素失效:StaleElementReferenceException: stale element reference: element is not attached to the page document
- 设计模式之Adapter模式(笔记)
- SVG基础 | SVG PATH 元素
- jQuery实现复选框批量选择与反选的方法
- 【伊利丹】Hadoop-2.5.0-CDH5.2.0/Hive与Hbase整合实验
- xcode指令集设置