Solved

Using IComparable to compare two classes

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

760 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

23 Experts available now in Live!

Get 1:1 Help Now