Home

Mutable Variables

   

Description

When you declare and initialize a variable by binding it to a constant value, the compiler reserves memory for that variable and stores the value in the reserved memory. Here is an example:

let btnCalculateClick(e) =
    let consumption = float txtConsumption.Text

Normally, you cannot change the value of a variable after it has been declared and initialized. That is, you cannot remove the value in that reserved memory to replace it with another value. The variable is said to be immutable: its value cannot change after it has been initialized.

If you want to be able to change the value of a variable, you must indicate that it is mutable. To do this, when declaring the variable, precede it with the mutable keyword. Remember that you must always initialize the variable. Here is an example:

let btnCalculateClick(e) =
    let mutable pricePerCCF = 0.00
    let mutable monthlyCharges = 0.00

You can then use the variable any way you want. When a variable has been declared mutable, you can change its value by assigning another value to it. To do this, instead of =, use the <- operator. Here is an example:

open System
open System.Windows.Forms

let gasUtilityCompanyBillPreparation = new Form()
gasUtilityCompanyBillPreparation.Text   <- "Gas Utility Company"
gasUtilityCompanyBillPreparation.Width  <- 230
gasUtilityCompanyBillPreparation.Height <- 195

let lblConsumption = new Label()
lblConsumption.Left  <- 18
lblConsumption.Top   <- 18
lblConsumption.Width <- 100
lblConsumption.Text  <- "Comsumption:"
gasUtilityCompanyBillPreparation.Controls.Add(lblConsumption)

let txtConsumption = new TextBox()
txtConsumption.Left  <- 122
txtConsumption.Top   <- 16
txtConsumption.Width <- 75
txtConsumption.Text  <- "0.00"
gasUtilityCompanyBillPreparation.Controls.Add(txtConsumption)

let btnCalculate = new Button()
btnCalculate.Left  <- 122
btnCalculate.Top   <-  44
btnCalculate.Text  <- "Calculate"

let lblPricePerCCF = new Label()
lblPricePerCCF.Left  <- 18
lblPricePerCCF.Top   <- 78
lblPricePerCCF.Width <- 90
lblPricePerCCF.Text  <- "Price Per CCF:"
gasUtilityCompanyBillPreparation.Controls.Add(lblPricePerCCF)

let txtPricePerCCF = new TextBox()
txtPricePerCCF.Left  <- 122
txtPricePerCCF.Top   <- 74
txtPricePerCCF.Width <- 75
txtPricePerCCF.Text  <- "0.00"
gasUtilityCompanyBillPreparation.Controls.Add(txtPricePerCCF)

let lblMonthlyCharges = new Label()
lblMonthlyCharges.Left  <- 18
lblMonthlyCharges.Top   <- 104
lblMonthlyCharges.Width <- 100
lblMonthlyCharges.Text  <- "Monthly Charges:"
gasUtilityCompanyBillPreparation.Controls.Add(lblMonthlyCharges)

let txtMonthlyCharges = new TextBox()
txtMonthlyCharges.Left  <-  122
txtMonthlyCharges.Top   <- 102
txtMonthlyCharges.Width <-  75
txtMonthlyCharges.Text  <- "0.00"
gasUtilityCompanyBillPreparation.Controls.Add(txtMonthlyCharges)

let btnCalculateClick(e) =
    let mutable pricePerCCF = 0.00
    let mutable monthlyCharges = 0.00
    let consumption = float txtConsumption.Text

    pricePerCCF <- 50.00
        
    txtPricePerCCF.Text <- sprintf "%0.02f" pricePerCCF

    monthlyCharges <- consumption * pricePerCCF

    txtMonthlyCharges.Text <- sprintf "%0.02f" monthlyCharges

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

let btnClose = new Button()
btnClose.Left  <- 122
btnClose.Top   <- 130
btnClose.Width <-  75
btnClose.Text  <- "Close"
let btnCloseClick(e) = gasUtilityCompanyBillPreparation.Close()
btnClose.Click.Add(btnCloseClick)
gasUtilityCompanyBillPreparation.Controls.Add(btnClose)

Application.Run(gasUtilityCompanyBillPreparation)

Here is an example of running the program:

Mutable Variables

Mutable Variables

     
 

Home Copyright © 2009-2015 FunctionX Home