怎么样使集合类既有索引又有键?
例:
Public Class Person
Public Number As String '工号
Public Name As String '姓名
Public Age As Integer
Public Sub New(ByVal Num As String, ByVal Name As String, ByVal Age As Integer)
Me.Number = Num
Me.Name = Name
Me.Age = Age
End Sub
Public Sub New(ByVal Num As String)
Me.Number = Num
Me.Name = "未知"
Me.Age = 20
End Sub
End Class
Public Class PersonCollection
'Inherits System.Collections.Generic.List(Of Person)
Inherits System.Collections.Generic.Dictionary(Of String, Person)
Public Overloads Sub add(ByVal Num As String, ByVal Name As String, ByVal Age As Integer)
add(Num, New Person(Num, Name, Age))
End Sub
Public Overloads Sub add(ByVal Num As String)
add(Num, New Person(Num))
End Sub
Public Overloads Sub add(ByVal Psn As Person)
add(Psn.Number, Psn)
End Sub
End Class
这样PersonCollection只能用PersonCollection(工号)返回一个Person
如果用注释掉的继承List(Of T)只能用PersonCollection(Index)返回一个Person,而且不能保证集合内Person.Number唯一。
怎样才能使这2者共存?就象DataSet.Tables(index)和DataSet.Tables(表名)这样?