Fundamentals of Libraries


A library is a place that contains books that people read to acquire knowledge. When it comes to computers, a library is an application that contains functions and/or classes that people can use to complete their own application. Of course, somebody has to create such a library before it can be used. You can create and use your own library (or libraries) and/or you can use libraries that other people have already created.

Creating a Library

You can create your own library for a certain need to solve a particular problem.

To start the creation of a 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 prepare the library to be useable, on the main menu of Microsoft Visual Studio, click Build -> Build Solution. This would create a file that has the name of the F# file you created and the .dll extension:


Using a Library

Once you have a library, whether you created it or someone else di, you can use it in your application. To use the library, start a project. To add a reference to your library, on the main menu of Microsoft Visual Studio, 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 . . .

Introduction to Built-In Libraries


There are many libraries that have already been created and are ready to be used. Normally, when you create an F# application, especially in Microsoft Visual Studio, that studio automatically "loads" many libraries without your knowing. This means that, as you are working in your F# projects, the functions and classes of many libraries are accessed behind-the-scenes to make sure that everything is working smoothly in your project. In some other cases, you must expressly "load" a library you want to use.

Introduction to the .NET Framework

The most fundamental library you use in your applications is called the .NET Framework, or simply .NET. It is a huge collection of sub-libraies, or libraies, also called assemblies. Any of those libraries or assemblies contain classes that are ready to be used in your applications. As mentioned above, some of those assemblies are immediately available when you start an F# project.

Most of the .NET assemblies are files with the .DLL extension. Those assemblies contain namespaces that define functions and classes.

Loading, Importing, Opening a Library

In order to use a library in your application, 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.

Introduction to the System Library

The .NET Framework is a huge library of classes. The top level library and namespace is called System. Whenever you want to use it, include it on top of your file. Here is an example:

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.

Introduction to Other Libraries

The most fundamental library in the family of Microsoft Windows operating systems is named Win32, but you will hardly use it. Besides Win32 and the .NET Framework, there are other libraries on the market and some languages have their own libraries.

Introduction to the Visual Basic Library

Probably the most expanded of the .NET languages is Visual Basic. It includes its own 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);

Previous Copyright © 2014-2024, FunctionX Monday 14 February 2022 Next