我下了两节课:
班级部分:
Imports System.Collections.Generic
Public Class Part
Implements IEqualityComparer(Of Part)
Public Sub New()
End Sub
Public Property _comparisonType As EqualsComparmission
Public Sub New(ComparisonType As EqualsComparmission)
Me._comparisonType = ComparisonType
End Sub
Public Property PartName() As String
Public Property PartId() As Integer
Public Function Equals1(x As Part, y As Part) As Boolean Implements System.Collections.Generic.IEqualityComparer(Of Part).Equals
If x Is Nothing AndAlso y Is Nothing Then Return True
If x Is Nothing OrElse y Is Nothing Then Return False
Select Case _comparisonType
Case EqualsComparmission.PartId
Return x.PartId = y.PartId
Case EqualsComparmission.PartName
Return String.Equals(x.PartName, y.PartName)
Case EqualsComparmission.PartId_and_PartName
Return x.PartId = y.PartId AndAlso String.Equals(x.PartName, y.PartName)
Case Else
Throw New NotSupportedException("Unknown comparison type for parts: " & _comparisonType.ToString())
End Select
End Function
Public Function GetHashCode1(obj As Part) As Integer Implements System.Collections.Generic.IEqualityComparer(Of Part).GetHashCode
Select Case _comparisonType
Case EqualsComparmission.PartId
Return obj.PartId
Case EqualsComparmission.PartName
Return If(obj.PartName Is Nothing, 0, obj.PartName.GetHashCode())
Case EqualsComparmission.PartId_and_PartName
Dim hash = 17
hash = hash * 23 + obj.PartId
hash = hash * 23 + If(obj.PartName Is Nothing, 0, obj.PartName.GetHashCode())
Return hash
Case Else
Throw New NotSupportedException("Unknown comparison type for parts: " & _comparisonType.ToString())
End Select
End Function
Public Overrides Function ToString() As String
Return "ID: " & PartId & " Name: " & PartName
End Function
End Class
Class PartCollection:
Public Class PartsCollection
Implements ICollection(Of Part)
Public Property _comparisonType As EqualsComparmission
Private myList As List(Of Part)
Public Sub New()
If myList Is Nothing Then
myList = New List(Of Part)
End If
End Sub
Public Sub New(ComparisonType As EqualsComparmission)
If myList Is Nothing Then
myList = New List(Of Part)
End If
Me._comparisonType = ComparisonType
End Sub
Public Sub Add(item As Part) Implements ICollection(Of Part).Add
myList.Add(item)
End Sub
Public Sub Clear() Implements ICollection(Of Part).Clear
myList.Clear()
End Sub
Public Sub CopyTo(array() As Part, arrayIndex As Integer) Implements ICollection(Of Part).CopyTo
End Sub
Public Function GetEnumerator() As IEnumerator(Of Part) Implements IEnumerable(Of Part).GetEnumerator
Return New PartsEnumeration(myList)
End Function
Public ReadOnly Property Count As Integer Implements ICollection(Of Part).Count
Get
Return myList.Count
End Get
End Property
Public ReadOnly Property IsReadOnly As Boolean Implements ICollection(Of Part).IsReadOnly
Get
Return False
End Get
End Property
Public Function Remove(item As Part) As Boolean Implements ICollection(Of Part).Remove
Return myList.Remove(item)
End Function
Public Function GetEnumerator1() As IEnumerator Implements IEnumerable.GetEnumerator
Return New PartsEnumeration(myList)
End Function
Public Function Contains(item As Part) As Boolean Implements ICollection(Of Part).Contains
Select Case _comparisonType
Case EqualsComparmission.PartId
Return myList.Exists(Function(x) x.PartId = item.PartId)
Case EqualsComparmission.PartName
Return myList.Exists(Function(x) x.PartName = item.PartName)
Case EqualsComparmission.PartId_and_PartName
Return myList.Exists(Function(x) x.PartId = item.PartId And x.PartName = item.PartName)
Case Else
Throw New NotSupportedException("Unknown comparison type for parts: " & _comparisonType.ToString())
End Select
End Function
End Class
我想使用Part类中的Equals1代替,因为代码现在正在使用PartsCollection中的Contains1。我想问你在这种情况下我要做什么?我当前的代码正在运行,但我想切换到我之前创建的Equals1。
这是测试代码:
Dim myParts As New PartsCollection
Dim John As New Part With { _
.PartName = "John", _
.PartId = 1 _
}
Dim Silva As New Part With { _
.PartName = "Silva", _
.PartId = 2 _
}
Dim Jimmy As New Part With { _
.PartName = "Jimmy", _
.PartId = 3 _
}
Dim James As New Part With { _
.PartName = "James", _
.PartId = 4 _
}
myParts.Add(John)
myParts.Add(Silva)
myParts.Add(Jimmy)
myParts.Add(James)
Console.WriteLine("SEARCHING FOR CONCRETE PART IF EXIST")
myParts._comparisonType = EqualsComparmission.PartId_and_PartName
Dim p As Boolean = myParts.Contains(New Part() With { _
.PartName = "James", _
.PartId = 45 _
})
答案 0 :(得分:1)
在一个地方或另一个地方拥有功能的原因通常是基于需要或最有用的地方而不是写入时。当然,您可以将代码复制到您喜欢的位置。
当前状态的一个问题是你应该是一个定义为Property的方法参数。每次使用时都需要设置comparisonType
,这样您就可以确定它设置为应该是什么。其次,由于它对比较操作至关重要,因此您应该查看正在使用的内容。
调用Equals / Contains或集合类上的任何其他内容并不排除在item类上使用该方法。要从集合类中使用它:
Public Overloads Function Contains(p As Part,
compare As EqualsComparmission) As Boolean
Dim query = Items.Where(Function(f) p.Equals(f, compare)).ToList
Return query.Count <> 0
End Function
用法:
If myParts.Contains(newP, EqualsComparmission.PartId_and_PartName) Then
比较功能存在于Part Item类中,该集合只是调用它。这也允许你这样做:
If part1.Equals(part2)
更重要的是要使用的比较模式作为参数传递,指定用于此比较的模式。使用的实际查询取决于您需要知道的内容。