Home

Introduction

 

Fundamentals

 

Introduction

 

 

     
 
 

 

   
   
 

Previous Copyright © 2015 FunctionX Next

Home

F# Project Management: Libraries

     

Fundamentals of Libraries

 

Introduction

A library is one or a collection of functions, one or a collection of classes, or combinations of functions and classes, that can be directly used in an application to ease up the developer job who would not have to create a sought behavior from scratch. The primary library used in Microsoft Windows is called Win32. It can be difficult or restrictive to use because you must know the C and/or C++ languages.

To make it easier to create application in Microsoft Windows, at one time the company created (or rather developed) a language named Visual Basic (from BASIC) and developed a huge library of functions for it. To make it possible to use other languages to create applications, Microsoft created another library called the .NET Framework. Like Win32, the .NET Framework is a collection of internal sub-libraries so that, at one time, you can pick the library you want to use, based on your needs.

The libraries in the .NET Framework (and in Microsoft Windows) are files with the .DLL extension. Those libraries contain namespaces that contain functions and classes.

In order to use an external library, you must import or load it. Since a library may contain many namespaces, you should know what particular namespace contains the functionality you want to use. Once you know that namespace, to indicate it in your code file, you use the open keyword. The formula to follow is:

open namespace-name;

If the namespace is the top, simply type its name. The semi-colon is optional. If you are using a namespace that is included in another namespace, type the full path of the namespace.

The (or most) languages used in the .NET Framework are meant to collaborate. This means that if a certain language, such as Visual Basic, includes some functions (and/or classes) in one of its own libraries, other .NET languages should be able to use those functions (and/or classes) and benefit from them.

External Libraries

Besides Win32 and the .NET Framework, there are other libraries on the market and some languages have their own libraries. Probably the most expanded of the .NET languages is Visual Basic. It includes an impressive library of functions. In addition to, or instead of, F# built-in functions, you can borrow functions from Visual Basic and use them as you see fit.

To use the functions of the Visual Basic language in an F# application, you must reference its library. Most (if not all) of the functions of Visual Basic are created in the Microsoft.VisualBasic.dll assembly but they might be in different namespaces.  To start, you can add its reference in your application. To do that, on the main menu of Microsoft Visual Studio, click project -> Add Reference, or in the Solution Explorer, right-click the name of the project and click Add -> References, or right-click References -> Add References:

Reference Manager

After doing this, you can call the desired Visual Basic function. The functions in the Visual Basic library are organized in classes. Since the functions exist as members of classes, they are treated as method. You must know the name of the class that contains the method you want to use.

To call a Visual Basic function, type Microsoft.VisualBasic. followed by the name of the class, followed by the name of the method, and use it appropriately. For example, there is a method used to display a message box, and it is called MsgBox. That method exists in a class named Interaction. The method returns a value (named MsgBoxResult) that you can use or ignore. Here is an example of calling the method:

open System
open System.Windows.Forms
open Microsoft.VisualBasic

let exercise = new Form(Text = "Exercise")

let btnDisplay = new Button(Left = 24, Top = 24, Text = "Close")
let btnDisplayClick e =
    Microsoft.VisualBasic.Interaction.MsgBox("Welcome to the wonderful world of F# programming",
                                             MsgBoxStyle.OkOnly ||| MsgBoxStyle.Information,
                                             "F# Application Programming") |> ignore

exercise.Controls.Add btnDisplay
btnDisplay.Click.Add btnDisplayClick

do Application.Run exercise

F# Custom Libraries

   

Introduction

As we have seen and we will see in other lessons, the F# language provides various libraries with many functions. If those functions are not enough, you can create your own library with the desired functions and/or classes.

To create a custom library, display the New Project dialog box of Microsoft Visual Studio. In the left list , select Visual F#. In the middle list, click Library. Give a name to the new project:

Reference Manager

Once you are ready, click OK. A new file with a default name as Library1.fs is created for you. You can keep that name or you can change it. If you want to change it, in the Solution Explorer, right-click the name of the file and click Rename. Give the desired name with the .fs extension and press Enter.

If you had started from another type of project or you selected something other than Library in the New Project dialog box but you want to create a library, open the properties of the project (Project -> Project-Name Properties)... In the Output Type combo box, select Class Library:

Properties

In the file, create the functionality you want. For example, you can create classes or functions. Here is an example:

namespace Arithmetic

type Calculation(x : double, y : double) =
    member this.Addition()       : double = x + y
    member this.Subtraction()    : double = x - y
    member this.Multiplication() : double = x * y
    member this.Division()       : double = if y <> 0.00 then x / y else 0.00

To actually create the library, on the main menu, click Build -> Build Solution. This would create a file that has the name of the F# file you created and the .dll extension:

Properties

To use the library, start a project. To add a reference to your library, on the main menu, click Project -> Add Reference... or in the Solution Explorer, right-click References and click Add Reference... In the Reference Manager, click the Browse button. Locate the folder of the project that has the DLL file:

Properties

Click the DLL file and click Add. On the Reference Manager dialog box, click OK. In your file, add a reference to the namespace that contains the code of the library. In your code, access the class from the library and us it as you see fit. Here is an example:

open System
open Arithmetic
open System.Windows.Forms

let exercise = new Form(Text = "Arithmentic", Height = 255, Width = 215)

let lblOperand1 = new Label(Left = 22, Top = 21, Width = 65, Text = "Operand 1:")
exercise.Controls.Add lblOperand1

let txtOperand1 = new TextBox(Left = 108, Top = 16, Text = "0.00", Width = 75)
exercise.Controls.Add txtOperand1

let lblOperand2 = new Label(Left = 22, Top = 45, Width = 65, Text = "Operand 2:")
exercise.Controls.Add lblOperand2

let txtOperand2 = new TextBox(Left = 108, Top = 42, Text = "0.00", Width = 75)
exercise.Controls.Add txtOperand2

let btnOperate = new Button(Left = 108, Top = 70, Text = "Operate", Width = 75)
exercise.Controls.Add btnOperate

let lblAddition = new Label(Left = 22, Top = 105, Width = 65, Text = "Addition:")
exercise.Controls.Add lblAddition

let txtAddition = new TextBox(Left = 108, Top = 103, Text = "0.00", Width = 75)
exercise.Controls.Add txtAddition

let lblSubtraction = new Label(Left = 22, Top = 134, Width = 75, Text = "Subtraction:")
exercise.Controls.Add lblSubtraction

let txtSubtraction = new TextBox(Left = 108, Top = 131, Text = "0.00", Width = 75)
exercise.Controls.Add txtSubtraction

let lblMultiplication = new Label(Left = 22, Top = 160, Width = 75, Text = "Multiplication:")
exercise.Controls.Add lblMultiplication

let txtMultiplication = new TextBox(Left = 108, Top = 159, Text = "0.00", Width = 75)
exercise.Controls.Add txtMultiplication

let lblDivision = new Label(Left = 22, Top = 190, Width = 65, Text = "Division:")
exercise.Controls.Add lblDivision

let txtDivision = new TextBox(Left = 108, Top = 187, Text = "0.00", Width = 75)
exercise.Controls.Add txtDivision

let btnOperateClick e =
    let operand1 = float txtOperand1.Text
    let operand2 = float txtOperand2.Text

    let operation      = new Calculation(operand1, operand2)
    let addition       = operation.Addition()
    let subtraction    = operation.Subtraction()
    let multiplication = operation.Multiplication()
    let division       = operation.Division()

    txtAddition.Text       <- sprintf "%0.02f" addition
    txtSubtraction.Text    <- sprintf "%0.02f" subtraction
    txtMultiplication.Text <- sprintf "%0.07f" multiplication
    txtDivision.Text       <- sprintf "%0.07f" division
btnOperate.Click.Add btnOperateClick
do Application.Run exercise

Here are examples of running the program:

Creating an F# Custom Library Creating an F# Custom Library

Overview of the .NET Framework

The .NET Framework is a huge library of classes. As mentioned already, each library, also called an assembly, has the extension .DLL and includes one or more namespaces. The top level namespace is called System. Whenever you want to use it, include it on top of your file. Here are examples:

open System

The System namespace is very huge with its hundreds of classes.


Home Copyright © 2014-2015 FunctionX Home