Home

F# Topics: Reference Cells

   

Description

The values you declare in your programs are stored in the computer memory when your application starts. Every one of those values has or uses an address as its location. The compiler can refer to that address to locate the variable and use its value. You too can get a reference to that address and use it as you see fit. For example, you can change the value that the variable located at a particular address.

To get a reference to a variable, use the ref keyword. To use it, when declaring a variable, add this keyword between = and the value that will be initially stored in the variable. Here is an example:

let weeklySalary = ref 0.00

To get to which the value the variable refers, you must dereference the variable. This is done by applying the ! operator to the name of the variable. Here is an example:

let weeklySalary = ref 0.00
    
let strWeeklySalary = sprintf "%0.02f" !weeklySalary

When necessary, you can change the value of the variable by assigning another value to it. This is done using the := operator. Here is an example:

open System
open System.Windows.Forms

let payroll = new Form()
payroll.Text   <- "Payroll Preparation"
payroll.Width  <- 275
payroll.Height <- 140

let lblHourlySalary = new Label()
lblHourlySalary.Left  <- 18
lblHourlySalary.Top   <- 18
lblHourlySalary.Width <- 82
lblHourlySalary.Text  <- "Hourly Salary:"
payroll.Controls.Add(lblHourlySalary)

let txtHourlySalary = new TextBox()
txtHourlySalary.Left    <- 102
txtHourlySalary.Top     <- 16
txtHourlySalary.Width <- 64
txtHourlySalary.Text    <- "0.00"
payroll.Controls.Add(txtHourlySalary)

let lblWeeklyTime = new Label()
lblWeeklyTime.Left  <- 18
lblWeeklyTime.Top   <- 46
lblWeeklyTime.Width <- 82
lblWeeklyTime.Text  <- "Weekly Time:"
payroll.Controls.Add(lblWeeklyTime)

let txtWeeklyTime = new TextBox()
txtWeeklyTime.Left  <- 102
txtWeeklyTime.Top   <-  44
txtWeeklyTime.Width <-  64
txtWeeklyTime.Text  <- "0.00"
payroll.Controls.Add(txtWeeklyTime)

let btnCalculate = new Button()
btnCalculate.Left <- 176
btnCalculate.Top  <- 43
btnCalculate.Text <- "Calculate"

let lblWeeklySalary = new Label()
lblWeeklySalary.Left  <- 18
lblWeeklySalary.Top   <- 76
lblWeeklySalary.Width <- 82
lblWeeklySalary.Text  <- "Weekly Salary:"
payroll.Controls.Add(lblWeeklySalary)

let txtWeeklySalary = new TextBox()
txtWeeklySalary.Left  <- 102
txtWeeklySalary.Top   <-  74
txtWeeklySalary.Width <-  64
txtWeeklySalary.Text  <- "0.00"
payroll.Controls.Add(txtWeeklySalary)

let btnClose = new Button()
btnClose.Left <- 176
btnClose.Top  <-  73
btnClose.Text <- "Close"

let btnCalculateClick(e) =
    let weeklySalary = ref 0.00
    let hourlySalary = float txtHourlySalary.Text
    let weeklyTime   = float txtWeeklyTime.Text
    
    weeklySalary := hourlySalary * weeklyTime
    
    let strWeeklySalary = sprintf "%0.02f" !weeklySalary
    txtWeeklySalary.Text <- strWeeklySalary

btnCalculate.Click.Add(btnCalculateClick)
payroll.Controls.Add(btnCalculate)

let btnCloseClick(e) = payroll.Close()
btnClose.Click.Add(btnCloseClick)
payroll.Controls.Add(btnClose)

Application.Run(payroll)

Here is an example of running the program:

Reference Cells

Reference Cells

The ref feature works like that of a mutable variable and allows you to declare a variable whose value you plan to change while the program is running.

     
 

Home Copyright © 2009-2015, FunctionX Home