locked
Class Creation Help RRS feed

  • Question

  • User1479605282 posted

    I want to take the following class and make it so that I can use it with my other objects besides "Court"

    ie:  I would call the class something like MyComparer.  Below is for my object Court.  I want to have the same class, but instead of "Court" use other objects I have such as "Contacts", "Companies", etc. 

     

    Imports Microsoft.VisualBasic
    Imports System.Collections.Generic
    Imports System.Reflection
    
    
        Public Class CourtComparer
            Implements IComparer(Of Court)
    
            Public Sub New(ByVal p_propertyName As String)
                Me.PropertyName = p_propertyName
            End Sub
            Private _propertyName As String
            Public Property PropertyName() As String
                Get
                    Return _propertyName
                End Get
                Set(ByVal value As String)
                    _propertyName = value
                End Set
            End Property
    
            Public Function Compare(ByVal x As Court, ByVal y As Court) As Integer Implements System.Collections.Generic.IComparer(Of Court).Compare
                Dim t As Type = x.GetType()
                Dim val As PropertyInfo = t.GetProperty(Me.PropertyName)
                If Not (val Is Nothing) Then
    
                    Return Comparer.DefaultInvariant.Compare(val.GetValue(x, Nothing), val.GetValue(y, Nothing))
    
                Else
    
                    Throw New Exception(Me.PropertyName + " is not a valid property to sort on.  It doesn't exist in the Class.")
                End If
            End Function
        End Class
    
     
    Friday, August 22, 2008 11:10 AM

Answers

  • User1479605282 posted

    Solved it using code below:

    Imports Microsoft.VisualBasic
    Imports System.Collections.Generic
    Imports System.Reflection
    Public Class SortComparer(Of T)
        Implements IComparer(Of T)
    
        Public Sub New(ByVal p_propertyName As String)
            Me.PropertyName = p_propertyName
        End Sub
        Private _propertyName As String
        Public Property PropertyName() As String
            Get
                Return _propertyName
            End Get
            Set(ByVal value As String)
                _propertyName = value
            End Set
        End Property
    
        Public Function Compare(ByVal x As T, ByVal y As T) As Integer Implements System.Collections.Generic.IComparer(Of T).Compare
            Dim t As Type = x.GetType()
            Dim val As PropertyInfo = t.GetProperty(Me.PropertyName)
            If Not (val Is Nothing) Then
    
                Return Comparer.DefaultInvariant.Compare(val.GetValue(x, Nothing), val.GetValue(y, Nothing))
    
            Else
    
                Throw New Exception(Me.PropertyName + " is not a valid property to sort on.  It doesn't exist in the Class.")
            End If
        End Function
    End Class
    
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 24, 2008 7:44 PM

All replies

  • User1330954201 posted

    What about :-

     Implements IComparer(Of Object)

    Saturday, August 23, 2008 5:46 AM
  • User1479605282 posted

    thanks...I figured that out shortly after my brain fart!

    Saturday, August 23, 2008 7:40 AM
  • User1479605282 posted

    actually, that didnt' work.  Get the following error:

    Unable to cast object of type 'epoScheduler.BLL.SortComparer' to type 'System.Collections.Generic.IComparer`1[epoScheduler.BLL.Court]'.

     

    My new class below:

     

    Imports Microsoft.VisualBasic
    Imports System.Collections.Generic
    Imports System.Reflection
    
    Namespace epoScheduler.BLL
        Public Class SortComparer
            Implements IComparer(Of Object)
    
            Public Sub New(ByVal p_propertyName As String)
                Me.PropertyName = p_propertyName
            End Sub
            Private _propertyName As String
            Public Property PropertyName() As String
                Get
                    Return _propertyName
                End Get
                Set(ByVal value As String)
                    _propertyName = value
                End Set
            End Property
    
            Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.Generic.IComparer(Of Object).Compare
                Dim t As Type = x.GetType()
                Dim val As PropertyInfo = t.GetProperty(Me.PropertyName)
                If Not (val Is Nothing) Then
    
                    Return Comparer.DefaultInvariant.Compare(val.GetValue(x, Nothing), val.GetValue(y, Nothing))
    
                Else
    
                    Throw New Exception(Me.PropertyName + " is not a valid property to sort on.  It doesn't exist in the Class.")
                End If
            End Function
        End Class
    End Namespace
    
     
    Saturday, August 23, 2008 2:15 PM
  • User1479605282 posted

    This didn't work...can someone help?

    Monday, August 25, 2008 7:46 AM
  • User1479605282 posted

    Solved it using code below:

    Imports Microsoft.VisualBasic
    Imports System.Collections.Generic
    Imports System.Reflection
    Public Class SortComparer(Of T)
        Implements IComparer(Of T)
    
        Public Sub New(ByVal p_propertyName As String)
            Me.PropertyName = p_propertyName
        End Sub
        Private _propertyName As String
        Public Property PropertyName() As String
            Get
                Return _propertyName
            End Get
            Set(ByVal value As String)
                _propertyName = value
            End Set
        End Property
    
        Public Function Compare(ByVal x As T, ByVal y As T) As Integer Implements System.Collections.Generic.IComparer(Of T).Compare
            Dim t As Type = x.GetType()
            Dim val As PropertyInfo = t.GetProperty(Me.PropertyName)
            If Not (val Is Nothing) Then
    
                Return Comparer.DefaultInvariant.Compare(val.GetValue(x, Nothing), val.GetValue(y, Nothing))
    
            Else
    
                Throw New Exception(Me.PropertyName + " is not a valid property to sort on.  It doesn't exist in the Class.")
            End If
        End Function
    End Class
    
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 24, 2008 7:44 PM