1

下面的这段代码让 VS 抱怨两件事:

  1. IComparer接口必须实现Compare(...)

  2. 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
4

2 回答 2

2

在 VB 中没有语句终止符(就像;在 C# 中一样),所以每一行都是一个语句。这就是为什么你不能在 C# 等某些地方放置New Line。这就是为什么您的代码无法编译的原因。

将您的方法声明更改为一行:

Public Function Compare(x As Object, y As Object) As Integer  Implements Collections.IComparer.Compare

或者_在第一行的末尾添加以告诉编译器它不是语句的结尾,它应该在编译之前将它与下一行合并:

Public Function Compare(x As Object, y As Object) As Integer _
  Implements Collections.IComparer.Compare
于 2013-09-03T18:43:52.530 回答
1

尝试将其放在一行中,或包含下划线字符:

Public Function Compare(x As Object, y As Object) As Integer _
    Implements Collections.IComparer.Compare

VB.Net 不知道这两条线在没有下划线字符的情况下是连接的。

于 2013-09-03T18:43:40.017 回答