IDisposable Interface in F#

Introduction

This article explains events and the IDisposable interface. Objects are used in an application to consume memory and other resources. Some objects use more resources than others, for example objects are used to establish connection from one system to another. When these objects finish thier work we need to ensure that the resources are freed.

Event

Events are generally user actions, like button clicks, key presses, mouse hovers, clicks and so on or system generated notifications. One of the restrictions of C# is that events can only exist as members within classes. With the F# model, new event values can be created as part of any expression. In Windows Forms applications events can be used for many controls; these events allow us to instantly act upon the key presses.

IDisposable Interface

The IDisposable Interface is defined in the System namespace. The Idisposable interface releases unmanaged resourses, objects that are no longer needed as soon as it occurs and automatically frees up the memory. The IDisposable interface has only a single method called "Dispose". The Dispose method is called when the object needs to be freed. You can call the Dispose method manually when the object's work has been completed or automatically during Garbage Collection.

I will now show you how to combine two buttons with the event on the buttons in a Windows Forms application. Use the following procedure to create the sample.

Step 1:

Open Visual Studio then select "Create New Project" --> "F# Console Application".

CreateApplication

Step 2:

Now go the Solution Explorer to the right side of the application. Right-click on "References" and select "Add references".

SelectReferences

 



AddReferences

Step 3:

After selecting "Add References", in the framework template you need to select "System.Drawing" and "System.Windows.Forms" with holding the Ctrl key and click on "Ok".

ImportNamespaces

Write the following code in the F# application:

Step 4:

open System

open System.Drawing

open System.Windows.Forms

let merge (one:IEvent<'Del,'T>) (two:IEvent<'Del,'T>) (lst:'a list) = 

    let event = new Event<'a>()

    let rec getdata i =

        let rec next condition op = 

            if condition then

                event.Trigger(lst.[op i 1])

                firstbtnevent.Dispose() 

                secondbtnevent.Dispose()

                getdata (op i 1)

        and firstbtnevent:IDisposable = one.Subscribe (fun _ -> next (i < lst.Length-1) (+))

        and secondbtnevent:IDisposable = two.Subscribe (fun _ -> next (i > 0) (-))

        ()

    getdata 0

    event.Publish 

let Eventform = new Form(Text="Button Events")

Eventform.BackColor<-Color.DimGray

let firstbtn = new Button(Text="Click Here to Forward",Top=40, Width=140,Left=40)

firstbtn.BackColor<-Color.Ivory

let secondbtn = new Button(Text="Click Here to Backward",Top=80, Width=140,Left=40)

secondbtn.BackColor<-Color.Ivory

firstbtn |> Eventform.Controls.Add

secondbtn |> Eventform.Controls.Add

//secondbtn.Top <- 10

['P';'A';'N';'K';'A';'J'] |> merge firstbtn.Click secondbtn.Click 

|> Event.add (fun display -> MessageBox.Show (display.ToString()) |> ignore)

Eventform.ShowDialog() |> ignore

 

Note : The click event is raised when the Button Control is clicked. This event is commonly used when no command name is associated with the Button control (such as a Submit button).

Step 1 :

Debug the application by pressing F5 to execute the Windows application. After debugging the application the output will be as in the following figure:

AfterDebug

Step 2 :

Now click on the first button "Click here to forward"; this button event moves the character ahead one by one as in the figure given below.

ClicktoFrwrd

Displays next character.

FrwrdChar

Displays next character.

FrwrdK

Step 3 :

Now click on the second button "Click here to backward"; this button event moves the character back one by one as in the figure given below.

ClicktoBck

Displays previous character.

BackA

Displays previous character.

BackP

Summary

In this article you saw how to combine two Events in one click event. The first button event moves the character in a forward manner and the second button event moves the characters in a backward manner.


Similar Articles