vb.net接口签名语法

时间:2013-09-03 18:40:17

标签: vb.net interface

以下代码让VS抱怨两件事:

  1. IComparer界面必须实施Compare(...)

  2. Implements...在方法/多行lambda(函数内)中无效。

  3. 那有什么问题呢?签名是正确的,使函数lambda oneliner没有帮助。它的语法与documentation中的语法相同:

    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
    

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

尝试将其推入一行,或包含下划线字符:

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

VB.Net不知道那两行没有下划线字符就连接了。