Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using IComparable to compare two classes

Posted on 2008-06-25
2
Medium Priority
?
370 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:
Jason Evans earned 2000 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

670 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