Home

Topics on Functions: do

   

Doing Something in a Function

The F# language provides the do keyword that can be used to perform an action without creating a function. The formula to use the do keyword is:

[ attributes ]
do expression

If you have some attribute(s) you want to use to control the execution of the section, start with that (those) attribute(s). If the expression is short enough, you can write do and the expression on the same line. Here is an example:

do sprintf "Welcome to the wonderful world of F# programm!"

If the expression is long, type on its own line but indent it.  Here is an example:

open System
open System.Windows.Forms

let exercise : Form = new Form()

do Application.Run exercise

Doing the Binding in a Class

F# provides a special mechanism to perform one or more actions when an object is created; that is, when a constructor is initially called to create an object. To perform one or more primary actions when an object is created, you start a section using the do keyword in the body of the class:

type class-name =
    do Action(s)
	. . .

The action can be written just after do on the same line. Here are examples:

open System
open System.Windows.Forms

type Cylinder(radius : float, height : float) =
    // Form: Right Geometric Cylinder
    let geometricCylinder = new Form()
    
    // Text Box: Base
    let txtBase = new TextBox()
    
    do txtBase.Text <- string radius

    do Application.Run geometricCylinder

You can also create the action(s) on the next line. If you do, the code for the action(s) must be indented to the right of the do indentation.

As mentioned already, the do section is used to do some things when an object is accessed. This means that, after declaring a variable for the object, the code in the do section is immediately available and/or executes. As a result,  you can use the action(s) in that section. Here is an example:

open System
open System.Windows.Forms

type Cylinder(radius : float, height : float) =
    // Form: Right Geometric Cylinder
    let geometricCylinder = new Form()
    // Label: Base
    let lblBase = new Label()
    // Text Box: Base
    let txtBase = new TextBox()
    // Label: Height
    let lblHeight = new Label()
    // Text Box: Height
    let txtHeight = new TextBox()
    do
        geometricCylinder.Width  <- 155
        geometricCylinder.Height <- 110
        geometricCylinder.Text <- "Cylinder"

        lblBase.Left   <- 22
        lblBase.Top    <- 19
        lblBase.Width  <- 40
        lblBase.Text   <- "Base:"
        geometricCylinder.Controls.Add lblBase

        txtBase.Left  <- 72
        txtBase.Top   <- 16
        txtBase.Width <- 54
        geometricCylinder.Controls.Add txtBase

        lblHeight.Left <- 22
        lblHeight.Top <- 50
        lblHeight.Width <- 45
        lblHeight.Text <- "Height:"
        geometricCylinder.Controls.Add lblHeight

        txtHeight.Left  <- 72
        txtHeight.Top   <- 43
        txtHeight.Width <- 54
        geometricCylinder.Controls.Add txtHeight
    
    do
        txtBase.Text <- string radius
        txtHeight.Text <- string height
    do Application.Run geometricCylinder

let tube = new Cylinder(44.48, 26.74)

This would produce:

Doing the Binding in a Class

In Lesson 2, we saw that, 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.

     
     
 

Previous Copyright © 2015 FunctionX Next