| You are here: | About>Computing & Technology>Visual Basic> Using VB.NET> The IComparable Interface: An Intro Using a Custom Sort |
![]() | Visual Basic |
The IComparable Interface: An Intro Using a Custom SortDec 29 2007 Using An Interface, Step by StepUsing 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 = { _ 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. |
Las Vegas on a BudgetFind a BargainHotel DealsCheap EatsFree AttractionsEntertainment for Less |
All Topics | Email Article | | | ![]() |
| Advertising Info | News & Events | Work at About | SiteMap | Reprints | Help | Our Story | Be a Guide |
| User Agreement | Ethics Policy | Patent Info. | Privacy Policy | ©2008 About, Inc., A part of The New York Times Company. All rights reserved. |


