我通過實現IEquatable接口找到了解決方案。
Public Class cSystem Implements IEquatable(Of cSystem)
Private _systemKey As Int32
Private _systemName As String
Public Overloads Function Equals(ByVal other As cSystem) As Boolean Implements IEquatable(Of cSystem).Equals
If other Is Nothing Then Return False
If _systemKey = other.SystemKey AndAlso _systemName = other.SystemName Then
Return True
Else
Return False
End If
End Function
Public Overrides Function Equals(ByVal obj As Object) As Boolean
If obj Is Nothing Then Return MyBase.Equals(obj)
If Not TypeOf obj Is cSystemThen
Throw New InvalidCastException("The 'obj' argument is not a cSystemobject.")
Else
Return Equals(DirectCast(obj, cSystem))
End If
End Function
Public Shared Operator =(ByVal system1 As cSystem, ByVal system2 As cSystem) As Boolean
Return system1.Equals(system2)
End Operator
Public Shared Operator <>(ByVal system1 As cSystem, ByVal system2 As cSystem) As Boolean
Return Not system1.Equals(system2)
End Operator
然後我用的是包含在名單上的方法來比較兩種含cSystem類對象的泛型列表:
Dim valuesHasBeenChanged As Boolean
If objSystemCatalog.Systems.Count = objOldSystemCatalog.Systems.Count Then
For Each system As cSystem In objSystemCatalog.Systems
If objOldSystemCatalog.Systems.Contains(system) = False Then
valuesHasBeenChanged = True
End If
Next
ElseIf objSystemCatalog.Systems.Count > objOldSystemCatalog.Systems.Count Then
valuesHasBeenChanged = True
ElseIf objSystemCatalog.Systems.Count < objOldSystemCatalog.Systems.Count Then
valuesHasBeenChanged = True
End If
你的答案是正確的,但我應該怎麼做,如果我還希望比較systemKey和SYSTEMNAME? – 2010-12-21 11:14:26