Solved

Using IComparable to compare two classes

Posted on 2008-06-25
2
355 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

Suggested Solutions

Title # Comments Views Activity
VB.Net - TypeInitializer Error 25 28
SqlServer no dupes 25 35
Call Controller Action Method from ASPX 2 15
Convert datetime to time string 10 21
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

810 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