Fundamentals of Libraries



All languages used in the .NET Framework are meant to collaborate. This means that if a certain language 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.

The libraries in the .NET Framework (and in Microsoft Windows) are files with the .DLL extension. Those libraries contain namespaces that define 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. Here is an example:

open System

If you are using a namespace that is included in another namespace, type the full path of the namespace.

Overview of the .NET Framework

The .NET Framework is a huge library of classes. As mentioned already, each library has the extension .DLL and includes one or more namspaces. 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. One of the classes of the System namespace is called Console. The Console class allows you to perform actions on a black DOS window also named the console window. Another importand class provideed by the .NET Framework is the String.

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 borow 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:

Reference Manager

After doing this, you can call the desired Visual Basic function. Here are examples:

open System;

let mutable number = 0.00;

Console.Write("Enter a number: ");
let strNbr : string = Console.ReadLine();

if Microsoft.VisualBasic.Information.IsNumeric(strNbr) = false then
    number <- 0.00;
    number <- Microsoft.VisualBasic.Conversion.Val(strNbr);

let result = number * 2.00;

Console.WriteLine("{0} * 2 = {1}", number, result);

F# Custom Libraries



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 and create all necessary functions.

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, but 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:


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

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:


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:


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 Arithmetic;

let operator = Calculation(25.85, 17.62)

let add = operator.Addition()
let sub = operator.Subtraction()
let mul = operator.Multiplication()
let div = operator.Division()

printfn "Addition:       %0.02f" add
printfn "Subtraction:    %0.02f" sub
printfn "Multiplication: %0.04f" mul
printfn "Division:       %0.04f" div

This would produce:

Addition:       43.47
Subtraction:    8.23
Multiplication: 455.4770
Division:       1.4671
Press any key to continue . . .

Previous Copyright © 2014-2015 FunctionX Next