下面的这段代码让 VS 抱怨两件事:
IComparer
接口必须实现Compare(...)
Implements...
在方法/多行 lambda(函数内部)中无效。
那么它有什么问题呢?签名是正确的,使函数成为 lambda oneliner 没有帮助。无论如何,它与文档中的语法相同:
Public Class ColorSorter : Implements Collections.IComparer
Public Function Compare(x As Object, y As Object) As Integer
Implements Collections.IComparer.Compare
Dim cx As Drawing.Color, cy As Drawing.Color
Dim hx As Single, hy As Single, sx As Single, sy As Single, bx As Single, by As Single
cx = DirectCast(x, Drawing.SolidBrush).Color
cy = DirectCast(y, Drawing.SolidBrush).Color
sx = cx.GetSaturation()
sy = cy.GetSaturation()
hx = cx.GetHue()
hy = cy.GetHue()
bx = cx.GetBrightness()
by = cy.GetBrightness()
If hx < hy Then : Return -1
ElseIf hx > hy Then : Return 1
Else
If sx < sy Then : Return -1
ElseIf sx > sy Then : Return 1
Else
If bx < by Then : Return -1
ElseIf bx > by Then : Return 1
Else : Return 0
End If
End If
End If
End Function
End Class