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

The IComparable Interface: An Intro Using a Custom Sort
Using An Interface, Step by Step

By , About.com Guide

Dec 29 2007

Using an Interface is a key OOP (Object Oriented Programming) technique that some people have difficulty picking up. The way to understand it is to work through an example that actually uses an Interface. That's what this article does.

Here's the problem we're going to solve.

Ruth Leslie Katchem is a high school student who knows what she wants, and to get it, she has decided to adopt computerized methodology. There are a thousand boys in her high school and she just can't decide which one would be the optimum target. So she needs a flexible way to classify them and give her instant reports on their suitability. There are two major requirements:

First, she wants to rate each boy according to understandable classifications. She doesn't want to use something like, "Attractiveness: rate from 1 to 10". She wants to use, "Hunk or Junk" with categories like, "acceptable for paying for things" and "someone to introduce to your cousin". Ruth feels that she will be able to assign much better ratings that way.

Second, she wants to have a way to use these ratings to calculate a meaningful bottom-line "score" for each boy and a flexible way to change the calculation when necessary. For example, one classification is whether the boy owns a car or not. But if the boy doesn't have any gas money, what use is the car? The calculation would take things like that into account.

The answer to Ruth's dreams (in programming terms, that is) is a custom sort using the IComparable Interface: The HHH System! (High school Husband Hunter).

The classifications that Ruth wants to use can be organized using a simple VB.NET String array. (For simplicity, there are only three categories: "Has a Car?", "How Much Money?" and "Hunk or Junk".) Here's the array declaration with a couple of example entries. Notice that the elements of the array are actually instances of the FHData Class. That will be important to remember in a few paragraphs.

Dim HHHarray() As FHData = { _
New FHData( _
"Delbert", _
"has", _
"huge piles of", _
"junk! If he calls, tell him you died"), _

...

New FHData( _
"Fauntleroy", _
"has", _
"a serious lack of", _
"A Hunk!") _
}

If you need a refresher on arrays, I recommend one of the segments of the "Visual Basic .NET 2005 Express" tutorial: Collections of Things.

On the next page, we take up the real problem, how to write a custom sort.

Explore Visual Basic
By Category
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

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

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

All rights reserved.