Solved

Using IComparable to compare two classes

Posted on 2008-06-25
2
358 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

752 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