-
如何:创建集合初始值设定项所使用的集合 (Visual Basic)
使用集合初始值设定项创建集合时,Visual Basic 编译器会搜索该集合类型的 Add 方法,所搜索的 Add 方法的参数要与用于该集合的集合初始值设定项中的值类型相匹配。 此 Add 方法用于以集合初始值设定项中的值填充集合。
示例
下面的示例演示 OrderCollection 集合,该集合包含一个公共 Add 方法,集合初始值设定项可使用该方法添加 Order 类型的对象。 Add 方法允许使用集合初始值设定项的缩写语法。
VB
Public Class Customer
Public Property Id As Integer
Public Property Name As String
Public Property Orders As OrderCollection
Public Sub New(ByVal id As Integer, ByVal name As String, ByVal orders As OrderCollection)
Me.Id = id
Me.Name = name
Me.Orders = orders
End Sub
End Class
Public Class Order
Public Property Id As Integer
Public Property CustomerId As Integer
Public Property OrderDate As DateTime
Public Sub New(ByVal id As Integer,
ByVal customerId As Integer,
ByVal orderDate As DateTime)
Me.Id = id
Me.CustomerId = customerId
Me.OrderDate = orderDate
End Sub
End Class
VB
Public Class OrderCollection
Implements IEnumerable(Of Order)
Dim items As New List(Of Order)
Public Property Item(ByVal index As Integer) As Order
Get
Return CType(Me(index), Order)
End Get
Set(ByVal value As Order)
items(index) = value
End Set
End Property
Public Sub Add(ByVal id As Integer, ByVal customerID As Integer, ByVal orderDate As DateTime)
items.Add(New Order(id, customerID, orderDate))
End Sub
Public Function GetEnumerator() As IEnumerator(Of Order) Implements IEnumerable(Of Order).GetEnumerator
Return items.GetEnumerator()
End Function
Public Function GetEnumerator1() As IEnumerator Implements IEnumerable.GetEnumerator
Return Me.GetEnumerator()
End Function
End Class
VB
Imports System.Runtime.CompilerServices
Module Module1
<Extension()>
Sub Add(ByVal genericList As List(Of Customer),
ByVal id As Integer,
ByVal name As String,
ByVal orders As OrderCollection)
genericList.Add(New Customer(id, name, orders))
End Sub
End Module
VB
Dim customerList = New List(Of Customer) From
{
{1, "John Rodman", New OrderCollection From {{9, 1, #6/12/2008#},
{8, 1, #6/11/2008#},
{5, 1, #5/1/2008#}}},
{2, "Ariane Berthier", New OrderCollection From {{2, 2, #1/18/2008#},
{4, 2, #3/8/2008#},
{6, 2, #3/18/2008#},
{7, 2, #5/14/2008#},
{5, 2, #4/4/2008#}}},
{3, "Brian Perry", New OrderCollection From {{1, 3, #1/15/2008#},
{3, 3, #3/8/2008#}}}
}
出处:https://docs.microsoft.com/zh-cn/previous-versions/visualstudio/visual-studio-2010/dd293646(v=vs.100)
最新更新
求1000阶乘的结果末尾有多少个0
详解MyBatis延迟加载是如何实现的
IDEA 控制台中文乱码4种解决方案
SpringBoot中版本兼容性处理的实现示例
Spring的IOC解决程序耦合的实现
详解Spring多数据源如何切换
Java报错:UnsupportedOperationException in Col
使用Spring Batch实现批处理任务的详细教程
java中怎么将多个音频文件拼接合成一个
SpringBoot整合ES多个精确值查询 terms功能实
数据库审计与智能监控:从日志分析到异
SQL Server 中的数据类型隐式转换问题
SQL Server中T-SQL 数据类型转换详解
sqlserver 数据类型转换小实验
SQL Server数据类型转换方法
SQL Server 2017无法连接到服务器的问题解决
SQLServer地址搜索性能优化
Sql Server查询性能优化之不可小觑的书签查
SQL Server数据库的高性能优化经验总结
SQL SERVER性能优化综述(很好的总结,不要错
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比