1. Home
  2. Computing & Technology
  3. Visual Basic

The IComparable Interface: An Intro Using a Custom Sort

Adding the calculation for the Future Husband Score

By Dan Mabbutt, About.com

Dec 29 2007

This is a good place to put our custom FHScore calculation. Whenever a new instance of FHData is created, a FHScore will be calculated along with it. And since the string descriptions that are passed in are pretty specific, we can check to make sure that they're correct at the same time. We do this with this code (part of Sub New) next.

FH = theFH ' The name of the FH
FHHasCar = theFHHasCar
FHHasMoney = theFHHasMoney
FHIsHunkOrJunk = theFHIsHunkOrJunk
'------
' the custom calculation of FHScore
'------
If FHHasMoney = "a serious lack of" Then
   FHHasCar = "might as well not have"
End If
If FHHasCar = "has" Then
   FHScore = 50 ' Half the score in high school is wheels
Else
   FHScore = 0 ' You're a zero without one
End If

Select Case FHIsHunkOrJunk
   Case "A Hunk!"
      FHScore *= 2 ' Hunkiness doubles the score
   Case "good at a distance"
      FHScore *= 1.35
   Case "just OK"
      FHScore *= 1.1
   Case "acceptable for paying for things"
      FHScore *= 0.85
   Case "someone to avoid at lunch"
      FHScore *= 0.65
   Case "someone to introduce to your cousin"
      FHScore *= 0.35
   Case "junk! If he calls, tell him you died"
      FHScore = 0 ' no junk allowed!
   Case Else
      FHIsHunkOrJunk = "not correctly hunk or junk rated"
      FHScore = 0
End Select
Select Case FHHasMoney
   Case "huge piles of"
      FHScore += 30
   Case "a nice wad of"
      FHScore += 20
   Case "just barely enough"
      FHScore += 10
   Case "not enough"
      FHScore -= 15
   Case "almost no"
      FHScore -= 25
   Case "a serious lack of"
      FHScore -= 35
   Case Else
      FHHasMoney = "an invalid money rating"
      FHScore = 0
End Select

And add an End Sub to close the New Sub.

The one required function of the Interface, CompareTo is coded on the next page.

Explore Visual Basic

More from About.com

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Using VB.NET
  5. The IComparable Interface: An Intro Using a Custom Sort

©2008 About.com, a part of The New York Times Company.

All rights reserved.