📄 collection.vb
字号:
If index > Count Or index < 0 Then Throw New ArgumentOutOfRangeException("Index") End If If index = -1 Then m_Hashtable(m_HashIndexers(0)) = Value Else m_Hashtable(m_HashIndexers(index)) = Value End If End Set End Property Friend Function IndexOf(ByVal value As Object) As Integer Implements System.Collections.IList.IndexOf Dim index As Integer = -1 Dim enTry As DictionaryEntry For Each enTry In m_Hashtable If enTry.Value Is value Then index = m_HashIndexers.IndexOf(enTry.Key) Exit For End If ' also allow value comparison to work for types that do not ' override equality operator If (enTry.Value.GetType().Name = value.GetType().Name) Then If Object.Equals(enTry.Value, value) Then index = m_HashIndexers.IndexOf(enTry.Key) Exit For End If End If Next Return index End Function#If NET_VER >= 2.0 Then Public Function Contains(ByVal key As String) As Boolean Return m_Hashtable.ContainsKey(key) End Function#End If Private Function IListContains(ByVal value As Object) As Boolean Implements System.Collections.IList.Contains Return (CType(Me, IList)).IndexOf(value) <> -1 End Function#If NET_VER >= 2.0 Then Public Sub Clear()#Else Private Sub Clear()#End If m_Hashtable.Clear() m_HashIndexers.Clear() m_KeysCount = Integer.MinValue End Sub Private Sub IList_Clear() Implements System.Collections.IList.Clear Clear() End Sub Public Overloads Sub Remove(ByVal Key As String) If m_Hashtable.ContainsKey(Key) Then m_Hashtable.Remove(Key) m_HashIndexers.Remove(Key) Modified = True Else Throw New ArgumentException("Argument 'Key' is not a valid value.") End If End Sub Public Overloads Sub Remove(ByVal Index As Integer) Try ' Collections are 1-based m_Hashtable.Remove(m_HashIndexers(Index - 1)) m_HashIndexers.RemoveAt(Index - 1) Modified = True Catch e As ArgumentOutOfRangeException Throw New IndexOutOfRangeException("Collection1 index must be in the range 1 to the size of the collection.") End Try End Sub Private Overloads Sub Remove(ByVal value As Object) Implements System.Collections.IList.Remove 'FIXME: .Net behaviour is unstable Dim index As Integer = (CType(Me, IList)).IndexOf(value) If index <> -1 Then Remove(index + 1) End If End Sub Private Sub RemoveAt(ByVal index As Integer) Implements System.Collections.IList.RemoveAt If index + 1 > Count Or (index = -1 And Count = 0) Then Throw New ArgumentOutOfRangeException("Index") End If If index = -1 Then Remove(1) Else Remove(index + 1) End If End Sub Private Sub Insert(ByVal index As Integer, ByVal value As Object) Implements System.Collections.IList.Insert#If NET_VER >= 2.0 Then If index < 0 Then Throw New ArgumentOutOfRangeException End If#End If#If NET_VER < 2.0 Then If index + 2 > Count + 1 Then Throw New ArgumentOutOfRangeException End If#Else If index + 1 > Count + 2 Then Throw New ArgumentOutOfRangeException End If#End If If index + 2 >= Count Then Add(value) Else Insert(index + 2, value, GetNextKey(value)) End If End Sub Private Sub Insert(ByVal index As Integer, ByVal value As Object, ByVal Key As String) m_HashIndexers.Insert(index - 1, Key) m_Hashtable.Add(Key, value) Modified = True End Sub Private Function IList_Add(ByVal value As Object) As Integer Implements System.Collections.IList.Add Return AddByKey(value, GetNextKey(value)) End Function Private Function AddByKey(ByVal Item As Object, ByVal Key As String) As Integer m_Hashtable.Add(Key, Item) Modified = True Return m_HashIndexers.Add(Key) End Function Public Sub Add(ByVal Item As Object, _ Optional ByVal Key As String = Nothing, _ Optional ByVal Before As Object = Nothing, _ Optional ByVal After As Object = Nothing) Dim Position As Integer = Integer.MinValue ' check for valid args If (Not Before Is Nothing) And (Not After Is Nothing) Then Throw New ArgumentException("'Before' and 'After' arguments cannot be combined.") End If If Key <> Nothing And m_HashIndexers.IndexOf(Key) <> -1 Then Throw New ArgumentException End If If Not Before Is Nothing Then ' Looks like its an implementation bug in .NET ' Not very satisfied with the fix, but did it ' just to bring the similar behaviour on mono ' as well. If TypeOf Before Is Integer Then Position = Convert.ToInt32(Before) If Position <> (m_HashIndexers.Count + 1) Then Position = GetIndexPosition(Before) End If Else Position = GetIndexPosition(Before) End If End If If Not After Is Nothing Then Position = GetIndexPosition(After) + 1 End If If Key Is Nothing Then Key = GetNextKey(Item) End If If Position > (m_HashIndexers.Count + 1) Or Position = Integer.MinValue Then AddByKey(Item, Key) Else Insert(Position, Item, Key) End If End Sub Private Function GetNextKey(ByVal value As Object) As String m_KeysCount = m_KeysCount + 1 Dim key As String If value Is Nothing Then key = "Nothing" Else key = value.ToString() End If Return (key + m_KeysCount.ToString()) End Function Private Function GetIndexPosition(ByVal Item As Object) As Integer Dim Position As Integer = Integer.MinValue If TypeOf Item Is String Then Position = m_HashIndexers.IndexOf(Item) + 1 ElseIf TypeOf Item Is Integer Then Position = Convert.ToInt32(Item) Else Throw New InvalidCastException End If If Position < 0 Then Throw New ArgumentOutOfRangeException("Specified argument was out of the range of valid values.") End If 'Position must be from 1 to value of collections Count If Position > m_HashIndexers.Count Then Throw New ArgumentOutOfRangeException("Specified argument was out of the range of valid values.") End If Return Position End Function Private Sub CopyTo(ByVal array As System.Array, ByVal index As Integer) Implements System.Collections.ICollection.CopyTo If array Is Nothing Then Throw New ArgumentNullException End If If index < 0 Then Throw New ArgumentOutOfRangeException End If If array.Rank > 1 Or index >= array.Length Or Count > (array.Length - index) Then Throw New ArgumentException End If 'Dim NewArray As System.Array = array.CreateInstance(Type.GetType("System.Object"), m_HashIndexers.Count - index) ' Collections are 1-based For i As Integer = 0 To m_HashIndexers.Count - 1 array.SetValue(m_Hashtable(m_HashIndexers(i)), i + index) Next End Sub Private Function IEnumerable_GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator Return New ColEnumerator(Me) End Function Public Function GetEnumerator() As System.Collections.IEnumerator Return IEnumerable_GetEnumerator() End Function#If NET_VER >= 2.0 Then Private Sub GetObjectData(ByVal info As System.Runtime.Serialization.SerializationInfo, ByVal context As System.Runtime.Serialization.StreamingContext) Implements System.Runtime.Serialization.ISerializable.GetObjectData Throw New NotImplementedException End Sub Private Sub OnDeserialization(ByVal sender As Object) Implements System.Runtime.Serialization.IDeserializationCallback.OnDeserialization Throw New NotImplementedException End Sub#End If End ClassEnd Namespace
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -