小弟是个新手,刚学vb.net很短时间了,模块和类把我搞的很是头疼,下面是我刚学的一个例子,运行时老是提示:指定的 IComparer 引发异常。我很是迷茫,请问哪错了,为什么了?小弟先谢了!
Module Module1
Structure money
Implements IComparable
Private centsamount As Int16
Private Const currencysymbol As String = "$"
Public Sub New(ByVal dollars As Int16, ByVal cents As Int16)
Me.centsamount = (dollars * 100) + cents
End Sub
Public Sub New(ByVal amount As Int16)
Me.centsamount = CInt(amount * 100)
End Sub
Public Function compareto(ByVal other As Object) As Int16 Implements IComparable.CompareTo
Dim m2 As money = CType(other, money)
If Me.centsamount < m2.centsamount Then
Return -1
ElseIf Me.centsamount = m2.centsamount Then
Return 0
Else
Return 1
End If
End Function
Public Overrides Function tostring() As String
Return currencysymbol & CStr(Me.centsamount / 100)
End Function
End Structure
Sub main()
Dim salaries(4) As money
salaries(0) = New money(9)
salaries(1) = New money(4)
salaries(2) = New money(8)
salaries(3) = New money(6)
Console.WriteLine("unsorted array")
Dim salary As money
For Each salary In salaries
Console.WriteLine("{0}", salary)
Next
Array.Sort(salaries)
Console.WriteLine(vbCrLf & "sorted array")
For Each salary In salaries
Console.WriteLine("{0}", salary)
Next
End Sub
End Module