Home

F# Collections: Setting the Values of an Array

   

Description

To allow you to specify or change the value of an element of an array, the Array module provides a function named set. Its signature is:

Array.set : 'T [] -> int -> 'T -> unit

This method takes three arguments. The first is the array with elements. The second argument is the index of the element whose value you want to specify. The last argument is the value to set. Here is an example that sets the values of the second and the fourth members of the array:

open System
open System.Drawing
open System.Windows.Forms 

type Customer = {
    AccountNumber : string
    FirstName     : string
    LastName      : string }

and GasBill(billNbr, client : Customer, meterReading) =
    let bNbr = ref billNbr
    let cust = ref client
    let (rStart, rEnd) = meterReading

    member this.InvoiceNumber with get() = !bNbr and set(nbr) = bNbr := nbr
    member this.Customer with get() = !cust and set(nbr) = cust := nbr
    member this.MeterReadingStart with get() = rStart
    member this.MeterReadingEnd with get() = rEnd
    member this.Usage with get() = rEnd - rStart
    member this.TotalCCF with get() = this.Usage * 1.012
    member this.TotalTherms with get() = this.TotalCCF * 1.034

    member this.ServiceCharge with get() = 5.85
    member this.DistributionCharge with get() =
                                    let first100Therms = ref 0.00
                                    let above100Therms = ref 0.00

                                    if this.TotalTherms <= 100.00 then
                                        first100Therms := this.TotalTherms * 0.351869
                                        above100Therms := 0.00
                                    else
                                        first100Therms := 100.00 * 0.3518693
                                        above100Therms := (this.TotalTherms - 100.00) * 0.292748
                                    !first100Therms + !above100Therms
    member this.TotalDelivery with get() = this.ServiceCharge + this.DistributionCharge
                                    
    member this.MunicipalTax with get() = this.DistributionCharge * 0.86 / 100.00
    member this.StateTax with get() = this.DistributionCharge * 0.258 / 100.00
    member this.TotalInvoice with get() = this.TotalDelivery + this.MunicipalTax + this.StateTax
    new() = GasBill(0, { AccountNumber = ""; FirstName = ""; LastName = "" }, (0.00, 0.00))

let customers : Customer array = Array.zeroCreate 5

customers.[0] <- { AccountNumber = "807495"; FirstName = "Julius"; LastName = "Gluck" }
customers.[1] <- { AccountNumber = "928037"; FirstName = "Stephanie"; LastName = "Dugan" }
customers.[2] <- { AccountNumber = "279714"; FirstName = "William"; LastName = "Bamberg" }
customers.[3] <- { AccountNumber = "680513"; FirstName = "Kelly"; LastName = "Stewart" }
customers.[4] <- { AccountNumber = "479242"; FirstName = "Mark"; LastName = "Goddard" }

let bills = [| new GasBill(); new GasBill(); new GasBill(); new GasBill(); new GasBill(); new GasBill(); new GasBill() |];

Array.set bills 0 (new GasBill(100001, customers.[1], ( 6527.00,  6739.00)))
Array.set bills 1 (new GasBill(100002, customers.[3], (12582.00, 12846.00)))
Array.set bills 2 (new GasBill(100003, customers.[4], ( 3019.00,  3102.00)))
Array.set bills 3 (new GasBill(100004, customers.[2], (27471.00, 27829.00)))
Array.set bills 4 (new GasBill(100005, customers.[0], (48731.00, 48825.00)))
Array.set bills 5 (new GasBill(100006, customers.[1], ( 6739.00,  6997.00)))
Array.set bills 6 (new GasBill(100007, customers.[4], ( 3102.00,  3218.00)))

let gasUtilityCompany : Form = new Form(MaximizeBox = false, Text = "Gas Utility Company - Customer Invoice", ClientSize = new System.Drawing.Size(425, 410), StartPosition = FormStartPosition.CenterScreen)

gasUtilityCompany.Controls.Add(new Label(Location = new Point(20, 24), AutoSize = true, Text = "Invoice #:"))
let txtInvoiceNumber : TextBox = new TextBox(Location = new Point(98, 21), Width = 80, Text = "0")
gasUtilityCompany.Controls.Add txtInvoiceNumber

let lblCustomerInformation = new Label(Location = new Point(29, 55), AutoSize = true)
lblCustomerInformation.Text <- "Customer Information __________________________________________"
gasUtilityCompany.Controls.Add lblCustomerInformation

gasUtilityCompany.Controls.Add(new Label(Location = new Point(20, 85), AutoSize = true, Text = "Account #:"))
let txtAccountNumber : TextBox = new TextBox(Location = new Point(98, 82), Width = 78)
gasUtilityCompany.Controls.Add txtAccountNumber

let txtCustomerName : TextBox = new TextBox(Location = new Point(184, 82), Width = 196)
gasUtilityCompany.Controls.Add txtCustomerName

let lblMeterReading = new Label(Location = new Point(20, 114), AutoSize = true)
lblMeterReading.Text <- "Meter Reading _____________"
gasUtilityCompany.Controls.Add lblMeterReading

let lblInvoiceDetails = new Label(Location = new Point(205, 114), AutoSize = true)
lblInvoiceDetails.Text <- "Invoice Details _____________"
gasUtilityCompany.Controls.Add lblInvoiceDetails

gasUtilityCompany.Controls.Add(new Label(Location = new Point(22, 147), AutoSize = true, Text = "Start:"))
let txtMeterReadingStart : TextBox = new TextBox(Location = new Point(100, 144), Width = 78, Text = "0.00", TextAlign = HorizontalAlignment.Right)
gasUtilityCompany.Controls.Add txtMeterReadingStart

gasUtilityCompany.Controls.Add(new Label(Location = new Point(205, 146), AutoSize = true, Text = "Service Delivery"))

gasUtilityCompany.Controls.Add(new Label(Location = new Point(22, 172), AutoSize = true, Text = "End:"))
let txtMeterReadingEnd : TextBox = new TextBox(Location = new Point(100, 170), Width = 78, Text = "0.00", TextAlign = HorizontalAlignment.Right)
gasUtilityCompany.Controls.Add txtMeterReadingEnd

gasUtilityCompany.Controls.Add(new Label(Location = new Point(205, 172), AutoSize = true, Text = "Service Charge:"))
let txtServiceCharge : TextBox = new TextBox(Location = new Point(325, 169), Width = 65, Text = "0.00", TextAlign = HorizontalAlignment.Right)
gasUtilityCompany.Controls.Add txtServiceCharge

gasUtilityCompany.Controls.Add(new Label(Location = new Point(22, 199), AutoSize = true, Text = "Difference:"))
let txtDifference : TextBox = new TextBox(Location = new Point(100, 196), Width = 78, Text = "0.00", TextAlign = HorizontalAlignment.Right)
gasUtilityCompany.Controls.Add txtDifference

gasUtilityCompany.Controls.Add(new Label(Location = new Point(205, 199), AutoSize = true, Text = "Distribution Charge:"))
let txtDistributionCharge : TextBox = new TextBox(Location = new Point(325, 196), Width = 65, Text = "0.00", TextAlign = HorizontalAlignment.Right)
gasUtilityCompany.Controls.Add txtDistributionCharge

gasUtilityCompany.Controls.Add(new Label(Location = new Point(22, 230), AutoSize = true, Text = "Conversion to CCF: * 1.0120"))

gasUtilityCompany.Controls.Add(new Label(Location = new Point(205, 227), AutoSize = true, Text = "Total Delivery:"))
let txtTotalDelivery : TextBox = new TextBox(Location = new Point(325, 224), Width = 65, Text = "0.00", TextAlign = HorizontalAlignment.Right)
gasUtilityCompany.Controls.Add txtTotalDelivery

gasUtilityCompany.Controls.Add(new Label(Location = new Point(22, 258), AutoSize = true, Text = "CCF Total:"))
let txtTotalCCF : TextBox = new TextBox(Location = new Point(100, 255), Width = 78, Text = "0.00", TextAlign = HorizontalAlignment.Right)
gasUtilityCompany.Controls.Add txtTotalCCF

let lblTaxes = new Label(Location = new Point(209, 262), AutoSize = true)
lblTaxes.Text <- "Taxes _______________________"
gasUtilityCompany.Controls.Add lblTaxes

gasUtilityCompany.Controls.Add(new Label(Location = new Point(22, 290), AutoSize = true, Text = "Conversion to Therms: * 1.034"))

gasUtilityCompany.Controls.Add(new Label(Location = new Point(205, 292), AutoSize = true, Text = "Municipal Tax (8.6%):"))
let txtMunicipalTax : TextBox = new TextBox(Location = new Point(325, 289), Width = 65, Text = "0.00", TextAlign = HorizontalAlignment.Right)
gasUtilityCompany.Controls.Add txtMunicipalTax

gasUtilityCompany.Controls.Add(new Label(Location = new Point(22, 318), AutoSize = true, Text = "Therms Total:"))
let txtTotalTherms : TextBox = new TextBox(Location = new Point(100, 315), Width = 78, Text = "0.00", TextAlign = HorizontalAlignment.Right)
gasUtilityCompany.Controls.Add txtTotalTherms

gasUtilityCompany.Controls.Add(new Label(Location = new Point(205, 318), AutoSize = true, Text = "State Tax (2.58%):"))
let txtStateTax : TextBox = new TextBox(Location = new Point(325, 315), Width = 65, Text = "0.00", TextAlign = HorizontalAlignment.Right)
gasUtilityCompany.Controls.Add txtStateTax

let lblLine = new Label(Location = new Point(22, 338), AutoSize = true)
lblLine.Text <- "____________________________________________________________"
gasUtilityCompany.Controls.Add lblLine

gasUtilityCompany.Controls.Add(new Label(Location = new Point(205, 370), AutoSize = true, Text = "Total Invoice:"))
let txtTotalInvoice : TextBox = new TextBox(Location = new Point(325, 367), Width = 65, Text = "0.00", TextAlign = HorizontalAlignment.Right)
gasUtilityCompany.Controls.Add txtTotalInvoice

let btnFindInvoice : Button = new Button(Location = new Point(184, 19), Width = 100, Text = "Find Invoice")
let btnFindInvoiceClick e =
    let billFound     = ref false
    let customerFound = ref false
    let cust = ref { AccountNumber = "000000"; FirstName = ""; LastName = "" }
    let bill = ref (new GasBill(0, { AccountNumber = "000000"; FirstName = ""; LastName = "" }, (0.00, 0.00)))

    for i in 0 .. bills.Length - 1 do
        if bills.[i].InvoiceNumber = int txtInvoiceNumber.Text then
            (:=) bill bills.[i]
            (:=) billFound true

    if (!) billFound = true then
        for i in 0 .. customers.Length - 1 do
            if customers.[i].AccountNumber = bill.Value.Customer.AccountNumber then
                (:=) cust customers.[i]
                (:=) customerFound true

    if (!) customerFound = true then
        txtAccountNumber.Text <- cust.Value.AccountNumber
        txtCustomerName.Text <- cust.Value.LastName + ", " + cust.Value.FirstName
    if !billFound = true then
        txtMeterReadingStart.Text <- sprintf "%0.0f" bill.Value.MeterReadingStart
        txtMeterReadingEnd.Text <- sprintf "%0.0f" bill.Value.MeterReadingEnd
        txtDifference.Text <- sprintf "%0.0f" bill.Value.Usage
        txtTotalCCF.Text <- sprintf "%0.0f" bill.Value.TotalCCF
        txtTotalTherms.Text <- sprintf "%0.0f" bill.Value.TotalTherms
        txtServiceCharge.Text <- sprintf "%0.02f" bill.Value.ServiceCharge
        txtDistributionCharge.Text <- sprintf "%0.02f" bill.Value.DistributionCharge 
        txtTotalDelivery.Text <- sprintf "%0.02f" bill.Value.TotalDelivery
        txtMunicipalTax.Text <- sprintf "%0.02f" bill.Value.MunicipalTax
        txtStateTax.Text <- sprintf "%0.02f" bill.Value.StateTax
        txtTotalInvoice.Text <- sprintf "%0.02f" bill.Value.TotalInvoice

btnFindInvoice.Click.Add btnFindInvoiceClick
gasUtilityCompany.Controls.Add btnFindInvoice

let btnClose = new Button(Location = new Point(103, 364), Text = "Close")
let btnCloseClick e = gasUtilityCompany.Close()
btnClose.Click.Add btnCloseClick
gasUtilityCompany.Controls.Add btnClose

[<EntryPoint>]
let main argv =
    Application.Run gasUtilityCompany

Here is an example of using the application:

Setting the Values of an Array

Setting the Values of an Array

Setting the Values of an Array

     
     
 

Home Copyright © 2012-2015, FunctionX Home