Home

Actions Delegates

 

Fundamentals

 

Introduction

Instead of making you create delegates from scratch, and to provide universal support for all .NET languages, the .NET Framework provides various pre-built or built-in delegates that you can use directly in your programs. One of the built-in delegates is named Action. It is provided in various versions. The Action delegate is defined in the System namespace of the mscorlib.dll library. You should include that namespace in the document of your program.

Creating a Simple Action

The primary type of an Action delegate is for functions that take no parameter and return nothing. Its signature is:

type Action = 
    delegate of unit -> unit

Notice that the signature exactly resembles the one we saw in our introduction to delegates. This time, the name of the delegate is replaced with Action. Also notice that the delegate of expression is used, exactly as it was in our introduction to delegates. This means that an Action delegate is of type FSharpFunc. This also means that such a delegate receives all the members of that class, including the Invoke() method.

To create an action for the delegate, declare a variable. Since you would not have created a delegate, use Action to initialize the variable and add its parentheses. The rest is done as we saw already. This means that you should first  implement a method and pass it to the parentheses of Action.

Consider the simple example we saw in our introduction:

open System
open System.Windows.Forms

type TitleCreation = delegate of unit -> unit

let exercise = new Form(Width = 300, Height = 80, Text = "Exercise")

let lblMessage = new Label(Left = 21, Top = 18, Width = 280)
exercise.Controls.Add lblMessage

let getTitle() : unit =
    lblMessage.Text <- "Introduction to F# Programming, Second Edition"

let title = TitleCreation(getTitle)

title.Invoke()

do Application.Run exercise

This would produce:

Creating a Simple Action

To use the Action deleegate, replace the name of the delegate with Action. Here is an example:

open System
open System.Windows.Forms

type TitleCreation = delegate of unit -> unit

let exercise = new Form(Width = 300, Height = 80, Text = "Exercise")

let lblMessage = new Label(Left = 21, Top = 18, Width = 280)
exercise.Controls.Add lblMessage

let getTitle() : unit =
    lblMessage.Text <- "Introduction to F# Programming, Second Edition"

let title = Action(getTitle)

title.Invoke()

do Application.Run exercise

You can inclue the new operator before Action. Here is an example:

open System
open System.Windows.Forms

let exercise = new Form(Width = 300, Height = 80, Text = "Exercise")

let lblMessage = new Label(Left = 21, Top = 18, Width = 280)
exercise.Controls.Add lblMessage

let getTitle() : unit =
    lblMessage.Text <- "Introduction to F# Programming, Second Edition"

let title = new Action(getTitle)

title.Invoke()

do Application.Run exercise

If you want to specify the data type of the variable, type : Action after the name of the variable. Here is an example:

open System
open System.Windows.Forms

let exercise = new Form(Width = 300, Height = 80, Text = "Exercise")

let lblMessage = new Label(Left = 21, Top = 18, Width = 280)
exercise.Controls.Add lblMessage

let getTitle() : unit =
    lblMessage.Text <- "Introduction to F# Programming, Second Edition"

let title : Action = new Action(getTitle)

title.Invoke()

do Application.Run exercise
     
 

An Action Delegate With Parameters

To support parameters passed to a delegate, the Action delegate is provided in many versions, each for a certain number of parameters. The primary version in this category has the following signature:

type Action = 
    delegate of  
        obj:'T -> unit

This signature is for a function that takes one parameter and returns nothing. As you can see, the parameter is a generic type, meaning it can be any type such as a primitive type. When calling Action, you must specify the type of the parameter. To do this, after Action, add the <> operator. Inside, enter the type of the parameter. Here is an example:

Action<string>()

Everything else is done as seen so far. As mentioned already, you can use the Action delegate that takes one parameter and the parameter can be a primitive type. Here is an example:

open System
open System.Windows.Forms

let exercise = new Form(Width = 265, Height = 80, Text = "Exercise")

let lblMessage = new Label(Left = 21, Top = 18, Width = 260)
exercise.Controls.Add lblMessage

let describe str =
    lblMessage.Text <- "Course Name: " + str

let desc = new Action<string>(describe)

desc.Invoke "Enterprise Database Design"

do Application.Run exercise

This would produce:

An Action Delegate With Parameters

The parameter can be of a class type. Here is an example:

open System
open System.Windows.Forms 

let exercise = new Form(Width = 200, Height = 160, Text = "Exercise")

let lblRadius = new Label(Left = 23, Top = 18, Width = 180)
exercise.Controls.Add lblRadius

let lblDiameter = new Label(Left = 23, Top = 43, Width = 180)
exercise.Controls.Add lblDiameter

let lblCircumference = new Label(Left = 23, Top = 69, Width = 180)
exercise.Controls.Add lblCircumference

let lblArea = new Label(Left = 23, Top = 96, Width = 180)
exercise.Controls.Add lblArea

type Circle() =
    let mutable radius = 0.00
    member me.Radius with get() = radius and set(value) = radius <- value
    member this.Diameter with get() = radius * 2.00
    member this.Circumference with get() = this.Diameter * 3.14156
    member this.Area with get() = radius * radius * 3.14156
    
let describe (circle : Circle) =
    lblRadius.Text <- "Radius: " + string circle.Radius
    lblDiameter.Text <- "Diameter: " + string circle.Diameter
    lblCircumference.Text <- "Circumference: " + string circle.Circumference
    lblArea.Text <- "Area: " + string circle.Area
    
let round = Circle()
round.Radius <- 48.06

let result = Action<Circle>(describe)

result.Invoke round

do Application.Run exercise

This would produce:

An Action Delegate With Parameters

The method of a class can be passed to an Action delegate. Here is an example:

open System
open System.Windows.Forms 

let exercise = new Form(Width = 200, Height = 160, Text = "Exercise")

let lblEmployeeNumber  = new Label(Left = 23, Top = 18, Width = 180)
exercise.Controls.Add lblEmployeeNumber 

let lblEmployeeName  = new Label(Left = 23, Top = 43, Width = 180)
exercise.Controls.Add lblEmployeeName 

let lblEmploymentStatus = new Label(Left = 23, Top = 69, Width = 180)
exercise.Controls.Add lblEmploymentStatus

let lblHourlySalary  = new Label(Left = 23, Top = 96, Width = 180)
exercise.Controls.Add lblHourlySalary

type Employee(emplNbr, fname, lname, status, salary) =
    member val EmployeeNumber = emplNbr with get, set
    member val FirstName = fname with get, set
    member val LastName = lname with get, set
    member val Status = status with get, set
    member val HourlySalary = salary with get, set
    
    member this.Present() : unit =
        lblEmployeeNumber.Text <- "Employee #: " + this.EmployeeNumber
        lblEmployeeName.Text <- "Full Name: " + this.FirstName + " " + this.LastName
        lblHourlySalary.Text <- "Status: " + this.Status
        lblEmploymentStatus.Text <- "Hourly Salary: " + (string this.HourlySalary)

let empl = Employee("7092-3094", "Rose", "Crittenden", "Full-Time", 24.05)

let resume = new Action(empl.Present)

resume.Invoke()

do Application.Run exercise

This would produce:

An Action Delegate With Parameters

To support the parameters passed to a function, there are various versions of the Action delegate. To specify the parameters of an Action delegate, in its <> operator, enter the parameter types separated by a comma. Here is an example:

Action<int, string>()

In the parentheses, pass the name of  the function that implements the delegate.

 

   
   
 

Home Copyright © 2015 FunctionX Home