Solved

Using IComparable to compare two classes

Posted on 2008-06-25
2
354 Views
Last Modified: 2013-11-26
Another (I think reasonably easy) vb.net class/interface question.

I have a class called "Credentials" which holds a username/password combo in a hashed form. I want to be able to compare it to another credentials object created by the user in order to determine if the user entered the right username/password combination. Attached in the snippet is the barebones of the class.

I'd like to compare them like this:

' Stored somewhere
Dim cred1 as new Credentials("john", "mypassword")

' Provided by user
Dim cred2 as new Credentials("john", "userenteredthewrongthing")

If cred1.CompareTo(cred2) Then
      msgbox("Correct!")
Else
      msgbox("Access denied!")
End If

Now I know I could just go MAKE the CompareTo() method, have it take a credential object and presto - but wouldn't it be more correct to have the class implement the IComparable interface? So I did that. Again, see my code snippet.

But CompareTo() takes an object, not a Credentials class. How do I implement the comparison?
Public Class Credentials

    Implements IComparable
 

    Public usernamehash as String

    Public passwordhash as String
 

    Sub New()

        Throw New NotImplementedException()

    End Sub

    Sub New(ByVal user as String, ByVal pass as String)

        Throw New NotImplementedException()

    End Sub
 

    Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo

    End Function

End Class

Open in new window

0
Comment
Question by:Frosty555
2 Comments
 
LVL 10

Accepted Solution

by:
MrClyfar earned 500 total points
ID: 21868078
Hi Frosty555.

Here's a simple class that I just tested that user IComparable:

Public Class Class1 : Implements IComparable

    Public UserNamne As String
    Public Password As String

    Public Sub New(ByVal userName As String, ByVal password As String)
        Me.UserNamne = userName
        Me.Password = password
    End Sub

    Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo

        Dim passedClass As Class1 = DirectCast(obj, Class1)

        If passedClass.UserNamne = Me.UserNamne AndAlso passedClass.Password = Me.Password Then
            Return 1
        Else
            Return 0
        End If

    End Function
End Class

The idea is that you can use DirectCast (or CType) to change obj so that you can then perform checks against whatever properties you like. So in your case, you could use:

        Dim passedClass As Credentials = DirectCast(obj, Credentials)

Hope this helps.
Jas.
0
 
LVL 31

Author Comment

by:Frosty555
ID: 21883512
Ahh that's what you're supposed to do. I didn't know about DirectCast. Thank you!

I think actually IComparable might not quite be what I'm supposed to use since it is supposed to determine if the value is less than or greater than for the purposes of sorting... but nevertheless I see how you do this now.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now