Results 1 to 3 of 3

Thread: Problem compiling new VB.Net class

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2002
    Posts
    32

    Question Problem compiling new VB.Net class

    Hi all,

    I have a VB.Net app that uses a ListView object. Since I want to be able to sort the columns of the ListView by clicking on the column headers I decided to write/plunder(!) a class to do this. I hunted around on the net first to get an idea how to do it and found a class (in C#) which I decided to convert.

    I've done the conversion from C# to VB.Net but am getting one really stubborn error that I can't solve. The error I'm getting from the class (listed below) is this:

    E:\VS.NET\Projects\BatchRename\ListViewSortManager.vb(21): 'BatchRename.MJC.Windows.Forms.ListViewTextSort' must implement 'Overridable Overloads Function Compare(x As Object, y As Object) As Integer' for interface 'System.Collections.IComparer'.

    The thing is I *am* implementing Compare in my class - exactly how VS.Net tells me it wants it!

    Here's the code...

    Public Class ListViewTextSort
    Implements IComparer

    Private m_column As Integer
    Private m_ascending As Boolean

    'Constructor
    Public Sub New(ByVal column As Integer, ByVal ascending As Boolean)
    m_column = column
    m_ascending = ascending
    End Sub

    'Implementation of IComparer.Compare
    Overridable Overloads Function Compare(ByVal lhs As Object, ByVal rhs As Object) As Integer

    Dim lhsLvi As ListViewItem = CType(lhs, ListViewItem)
    Dim rhsLvi As ListViewItem = CType(rhs, ListViewItem)

    If lhsLvi Is Nothing Or rhsLvi Is Nothing Then
    Return 0
    End If

    Dim lhsItems As ListViewItem.ListViewSubItemCollection = lhsLvi.SubItems
    Dim rhsItems As ListViewItem.ListViewSubItemCollection = rhsLvi.SubItems

    Dim lhsText As String
    Dim rhsText As String

    If lhsItems.Count > m_column Then
    lhsText = lhsItems(m_column).Text
    Else
    lhsText = String.Empty
    End If

    If rhsItems.Count > m_column Then
    rhsText = rhsItems(m_column).Text
    Else
    rhsText = String.Empty
    End If

    Dim result As Integer = 0

    If lhsText.Length = 0 Or rhsText.Length = 0 Then
    result = lhsText.CompareTo(rhsText)
    Else
    result = OnCompare(lhsText, rhsText)
    End If

    If Not m_ascending Then
    result = -result
    End If

    Return result

    End Function


    'Overridden to do type-specific comparision.
    Protected Overridable Function OnCompare(ByVal lhs As String, ByVal rhs As String) As Integer

    Return String.Compare(lhs, rhs, False)

    End Function


    End Class


    I'm really no OO expert - can anybody spot what the problem might be here?

    TIA...

    Mike.

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You listed it as Implementing IComparer but didn't implement the methods that are required. Change this:
    Protected Overridable Function OnCompare(ByVal lhs As String, ByVal rhs As String) As Integer

    To this:
    Protected Overridable Function OnCompare(ByVal lhs As String, ByVal rhs As String) As Integer Implements IComparer.OnCompare

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2002
    Posts
    32
    Thanks for that - I added the "Implements IComparer.OnCompare" to the end of it and solved the problem!!

    Mike.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width