Home

Classes Fundamentals

 

Class Construction

 

Introduction

A class is a technique to lay a foundation for, or to describe, an object. To start such a foundation, you must use a special member function that holds the primary characteristics of the object. That special function is called a constructor.

To create a constructor, add the parentheses to the name of the class. Here is an example:

type Vehicle() =
    member this.Color = string
    member this.Doors = int

Creating an Object to Use a Class

As mentioned already, a class provides only the description of an object. To actually create an object, you must declare a variable based on the class. This is done using the let operator, giving a name to the variable, and assigning a constructor of the class preceded by the new operator. Here is an example:

type Vehicle() =
    member this.Color = string
    member this.Doors = int

let car = new Vehicle()

You can also declare a variable for the class in a function. This would be done as follows:

open System
open System.Windows.Forms

type Creator() =
    let exercise = new Form()

    . . .

let create() =
    let app = new Creator()

As done with the other variables, to specify the data type of an object, after the name of the variable, type a colon followed by the name of the class. Here is an example:

type Vehicle() =
    member this.Color = string
    member this.Doors = int

let car : Vehicle = new Vehicle()

Accessing the Members of a Class Outside the Class

To access the member of a class outside the class, type the name of the variable, followed by a period and the name of the member. Here is an example:

type Vehicle() =
    member this.Color = string
    member this.Doors = int

let car = new Vehicle()

car.Make

If the class has more members, you can access them in the same way.

A Constructor with a Parameter

A contructor of a class is primarily a method. As suchway, it can take a parameter. Here is an example:

open System
open System.Windows.Forms

type Creator(title) =
    member this.Create() =
        let exercise = new Form()
        exercise.Text <- title

        Application.Run exercise

After doing that, when creating an object, make sure you pass an appropriate value to the constructor. Here is an example:

open System
open System.Windows.Forms

type Creator(title) =
    member this.Create() =
        let exercise = new Form()
        exercise.Text <- title

        Application.Run exercise

let app = new Creator("Application Development Environment")

app.Create()

Unlike functions and methods, a constructor always uses parentheses, both when creating it and when calling it, whether it takes parameters or not.

     
 

Class's Constructors

 

The Default Constructor of a Class

As seen in Lesson 2, when creating a class, you should add a constructor to it. This can be simply done by adding parentheses to the name of the class. Here is an example:

type Cylinder() =
    class
    end

If you add the parentheses to the name of a class but leave those parentheses empty, the class ic said to have a primary constructor. That is one way to create a default constructor. An alternative is not to add empty parentheses to the class name but add a member function named new, add empty parentheses to it, and assign the name of the class with empty parentheses to it. Here is an example:

type Cylinder =
    member me.Radius = float

    new() = Cylinder()

Details on Creating Constructors

As seen in Lesson 2, you can pass a parameter to a constructor of the class. Unlike a regular method of a class, the parameter of a contructor must be entered in the parentheses of the constructor. Here is an example:

type Circle(radius) =
    class
    end

Instead of just one parameter, you can create a constructor that has as many parameters as you want. If you want to use more than one parameter, separate them with commas in the parentheses of the name of the class. Here is an example:

type Cylinder(radius, height) =
    class
    end

If you want, or to be more explicit, you can (should) specify the data types of the parameters. Here are examples:

type Cylinder(tag : float, make : float) =
    class
    end

As an alternative, you can create a constructor using a new member function and assign to it the constructor created on the name of the class. There are rules you must follow:

  • If the name of the class has empty parentheses, this means that a default constructor is defined. You cannot create an additional default constructor. This means that you cannot add a new() member function that has empty parentheses
  • If at least one parameter is already passed to the name of the class, this means that the class doesn't have a default constructor anymore. You can then create a parameter-less new() constructor, which would now act as the default constructor. Here is an example:
    type Cylinder(radius : float, height : float) =
        new() = Cylinder()
  • You can add as many new() constructors as you want as long as each has a different number of parameters (than the others), including the one created using the name of the class.

    To add a new constructor using the new() member function, pass the desired number of parameters to both that new() member function and the class name assigned to it. In the parentheses of that class name, you must pass the number of parameters greater than or equal to the number of parameters of the new() constructor:
    • If the assigned class name has the same number of parameters as the new() constructor, the compiler will assign the parameters based on their positions. Here is an example:
      type Vehicle(tag : string, make : string, model : string) =
      
          new() = Vehicle()
          new(tag, make) = Vehicle(tag, make)
    • If the assigned class name has more parameters than the new() constructor, you must specify how the extra parameter(s) will get its (their) value(s). One way to take care of this is to assign a default value to each extra parameter (of course, there are other ways we will see). Here is an example:
type Cylinder(radius : float, height : float) =

    new() = Cylinder()
    new(radius) = Cylinder(radius, 0.00)

Using a Class

A class is meant to handle assignments and/or perform actions. Therefore, a class must be able to get values from outside and make its members available to the outside world. The role of the constructor(s) is to take care of such interactions. For example, a class can get values and use (or transform) them internally. To introduce an outside value to the members of a class, you can take it from a parameter of a constructor of the class. You can then assign that parameter to the appropriate member of the class. Here is an example:

type Circle(radius) =
    member this.Radius = rad

In the same way, you can create a contructor that takes as many members as necessary, add as members as necessary to the class, and assign the corresponding constructors parameters to the appropriate members. Here are examples:

type Cylinder(radius, height) =
    member this.Radius = radius
    member this.Height = height

Remember that you can create different constructors using the new member function with different numbers of parameters. Here are examples:

type Cylinder(radius, height) =
    member this.Radius = radius
    member this.Height = height
    new() = Cylinder()
    new(rad) = Cylinder(radius, 0.00)

Creating an Object

As mentioned already, a class provides only the description of an object. To actually create an object, you must declare a variable based on the class. This is done using the let operator, giving a name to the variable, and assigning a constructor of the class preceded by the new operator. You must call the appropriate constructor; that is, the constructor with the desired and appropriate number of parameters. Here is an example:

type ElectricBill(acntNbr, consumption, txRate, discRate) =
    new(acntNbr) = ElectricBill(acntNbr, 0.00, 0.00, 0.00)

let bill = new ElectricBill("937-497-283")

If the class has different constructors, you can declare different variables assigning the right constructor to each variable. Here are examples:

type ElectricBill(acntNbr, consumption, txRate, discRate) =
    new(acntNbr) = ElectricBill(acntNbr, 0.00, 0.00, 0.00)
    new(acntNbr, consumption) = ElectricBill(acntNbr, consumption, 0.00, 0.00)
    new(acntNbr, consumption, txRate) = ElectricBill(acntNbr,consumption,txRate,0.00)

let bill   = new ElectricBill("937-497-283")
let client = new ElectricBill("304-957-947", 8.24, 7.75)

Remember that when creating an object, you can specify its type by adding a colon and the name of the class after the name of the variable. Here are examples:

type ElectricBill(acntNbr, consumption, txRate, discRate) =
    new(acntNbr) = ElectricBill(acntNbr, 0.00, 0.00, 0.00)
    new(acntNbr, consumption) = ElectricBill(acntNbr, consumption, 0.00, 0.00)
    new(acntNbr, consumption, txRate) = ElectricBill(acntNbr,consumption,txRate,0.00)

let bill : ElectricBill = new ElectricBill("937-497-283")
let cust : ElectricBill = new ElectricBill("815-840-528", 14.86)
let client : ElectricBill = new ElectricBill("304-957-947", 8.24, 7.75)
let pay : ElectricBill = new ElectricBill("222-538-504", 12.06, 7.75, 20.00)
   
   
 

Home Copyright © 2009-2015, FunctionX Home