Home

Introduction

 

Fundamentals

 

Introduction

 

 

     
 
 

 

   
   
 

Previous Copyright © 2015 FunctionX Next

Home

Built-In Interfaces: IComparable

     

Introduction

Comparing two objects consists of finding out which one comes before. The comparison is simple if you are dealing with values of primitive types. For example, it is easy to know that 2 is lower than 5, but it is not obvious to compare two objects created from a composite type, such as two students, two cars, or two food items.

To assist you with comparing two objects, the .NET Framework provides various comparable interfaces. One of these interfaces is named IComparable. The IComparable interface is a member of the System namespace. Obviously you must define what would be compared and how the comparison would be carried. For example, to compare two Student objects of the above class, you can ask the compiler to base the comparison on the student number. Here is an example:

open System
open System.Windows.Forms

type StoreItem(number, make, category, subcategory, name, size, price, discrate) =
    let mutable nbr = number
    let mutable mk = make
    let mutable cat = category
    let mutable sub = subcategory
    let mutable nm = name
    let mutable sz = size
    let mutable prc = price
    let mutable rate = discrate

    member this.ItemNumber     with get() = nbr  and set(value) = nbr  <- value
    member this.Manufacturer   with get() = mk   and set(value) = mk   <- value
    member this.Category       with get() = cat  and set(value) = cat  <- value
    member this.SubCategory    with get() = sub  and set(value) = sub  <- value
    member this.ItemName       with get() = nm   and set(value) = nm   <- value
    member this.ItemSize       with get() = sz   and set(value) = sz   <- value
    member this.UnitPrice      with get() = prc  and set(value) = prc  <- value
    member this.DiscountRate   with get() = rate and set(value) = rate <- value
    member this.DiscountAmount with get() = this.UnitPrice * this.DiscountRate / 100.00
    member this.MarkedPrice    with get() = this.UnitPrice - this.DiscountAmount
    new() = StoreItem("", "", "", "", "", "", 0.00, 0.00)
    new(number, make, category, subcategory, name, size, price) = StoreItem(number, make, category, subcategory, name, size, price, 0.00)

    override this.Equals(obj) =
            match obj with
            | :? StoreItem as si -> this.ItemNumber = si.ItemNumber
            | _ -> false

    override this.ToString() =
        "Item #: " + this.ItemNumber + ", " + this.Manufacturer + ", " + this.Category + " " + this.SubCategory + ", Named " + this.ItemName + "(" + this.ItemSize + "), Price: " + string this.UnitPrice

    interface IComparable with
        member this.CompareTo(obj : Object) : int =
            match obj with
            | :? StoreItem as si -> compare this.ItemName si.ItemName
            | _ -> 0

let si1 = new StoreItem("279403", "Reef", "Men", "Sandals", "Men's Phantom Ultimate Flip Flop", "10", 45.50, float 0)
let si2 = new StoreItem("927497", "Guess", "Women", "Dresses", "Zip Front Fit and Flare Dressr", "4", 155.75, float 25)
let si3 = new StoreItem("293041", "Anne Klein", "Women", "Skirts", "A-Line Skirt with Lining", "4", 82.50, float 50)
let si4 = new StoreItem("537946", "Guess", "Women", "Dresses", "Zip Front Fit and Flare Dressr", "4", 155.75, float 25)

let comparison1 = sprintf "%i" ((si1 :> IComparable).CompareTo(si2))
let comparison2 = sprintf "%i" ((si1 :> IComparable).CompareTo(si3))
let comparison3 = sprintf "%i" ((si2 :> IComparable).CompareTo(si4))

// Form: Department Store
let departmentStore = new Form()
departmentStore.Width  <- 520
departmentStore.Height <- 225
departmentStore.Text   <- "Department Store"

// Label: Item 1
let lblItem1 = new Label()
lblItem1.Left   <- 12
lblItem1.Top    <- 18
lblItem1.Width  <- 500
lblItem1.Text   <- si1.ToString()
departmentStore.Controls.Add lblItem1

// Label: Item 3
let lblItem2 = new Label()
lblItem2.Left   <- 12
lblItem2.Top    <- 42
lblItem2.Width  <- 500
lblItem2.Text   <- si2.ToString()
departmentStore.Controls.Add lblItem2

// Label: Item 3
let lblItem3 = new Label()
lblItem3.Left   <- 12
lblItem3.Top    <- 64
lblItem3.Width  <- 500
lblItem3.Text   <- si3.ToString()
departmentStore.Controls.Add lblItem3

// Label: Item 4
let lblItem4 = new Label()
lblItem4.Left   <- 12
lblItem4.Top    <- 88
lblItem4.Width  <- 500
lblItem4.Text   <- si4.ToString()
departmentStore.Controls.Add lblItem4

// Label: Comparison 1
let lblComparison1 = new Label()
lblComparison1.Left   <- 12
lblComparison1.Top    <- 118
lblComparison1.Width  <- 500
lblComparison1.Text   <- "Item 927497 compared to 279403 produces: " + comparison1.ToString()
departmentStore.Controls.Add lblComparison1

// Label: Comparison 2
let lblComparison2 = new Label()
lblComparison2.Left  <-  12
lblComparison2.Top   <- 142
lblComparison2.Width <- 500
lblComparison2.Text  <- "Item 927497 compared to 537946 produces: " + comparison2.ToString()
departmentStore.Controls.Add lblComparison2

// Label: Comparison 3
let lblComparison3 = new Label()
lblComparison3.Left   <- 12
lblComparison3.Top    <- 168
lblComparison3.Width  <- 500
lblComparison3.Text   <- "Item 279403 compared to 293041 produces: " + comparison3.ToString()
departmentStore.Controls.Add lblComparison3

do Application.Run departmentStore

This would produce:

Comparing Two Objects

Most of the .NET Framework's classes that need to perform comparison already implement the IComparable interface or one of its equivalents.


Home Copyright © 2014-2015 FunctionX Home