Home

Staticity

 

Fundamentals

 

Introduction

After creating a class, in order to take advantage of its characteristics, you must declare a variable from it. In the same way, you can declare different variables of the same class. Declaring the variable is also referred to as creating an object or creating an instance (or a sample) of the class; or creating instances of the same class.

The characteristics (properties and methods) you access depend on the object (the variable) you had declared. This means that those characteristics are different from one object to another. An object is referred to as static if it povides one general or global object used throughout the program. The characteristics of such an object areferred to as static. This concept can be applied to all types of members of a class.

In F#, static variables or static functions can only be created in classes.

A Static Variable

A static variable is one that is declared as a regular field of a class but is present as soon as the class is made available in a program, which is even if no instance of the class is created and even before any method of the class is called.

To declare a member variable of a class as static, type the static keyword on the left side of let. Here is an example:

open System
open System.Windows.Forms

type Exercise() =
    static let title = string
    
    member this.Create() =
        let exercise = new Form()
        exercise.Text <- "Graphical User Interface"

        Application.Run(exercise)

let exo = new Exercise()
exo.Create()

In the same way, you can declare as many static member variables as you want in a class. Remember that let members are always private, so there is no reason to apply an access level to a static member.

Remember that, as soon as a class is accessed, which happens when you declare a variable for the class, the static member(s) is(are) available. As a result, not only do you not have to declare a variable of the class to access its member variable(s), but also once an object is created, its member(s) is(are) readily available. Still, in a class, you can access a static member variable as you would a non-static member. If you plan to change the value of the variable, you should first declare it using the mutable keyword, in which case you must initialize it. Here is an example:

open System
open System.Windows.Forms

type Exercise() =
    static let mutable title : string = ""
    
    member this.Create() =
        let exercise = new Form()
        exercise.Text <- "Graphical User Interface"

        Application.Run(exercise)

let exo = new Exercise()
exo.Create()

You can also declare the static field as a reference cell. Here is an example:

open System
open System.Windows.Forms

type Exercise() =
    static let title = ref ""
    
    member this.Create() =
        let exercise = new Form()
        exercise.Text <- "Graphical User Interface"

        Application.Run(exercise)

let exo = new Exercise()
exo.Create()

Any method of the class can access a static member. Here is an example:

open System
open System.Windows.Forms

type Exercise() =
    static let title : string = "Application Development"
    
    member this.Create() =
        let exercise = new Form()
        exercise.Text <- title

        Application.Run(exercise)

let exo = new Exercise()
exo.Create()

In the same way, you can declare a combination of static and non-static variables as you see fit. Here is an example:

open System
open System.Windows.Forms

type Exercise(width) =
    static let title : string = "Application Development"
    let w = width
    
    member this.Create() =
        let exercise = new Form(Width = w, Height = 205)
        exercise.Text <- title

        Application.Run(exercise)

let exo = new Exercise(325)
exo.Create()
     
 

Static Methods

 

Statically Doing Something

We already know that the do keyword can be used to perform an operation in a class. The action(s) of the do section is(are) performed when you declare a variable for the class. If you want the action(s) to take place whether an object is formally created or not, precede the do keyword with static. The member variables accessed in the do section have to be static. Here is an example:

open System
open System.Windows.Forms

type Exercise() =
    static let title : string = "Application Development"
    static let mutable hgt = 0

    static do
        hgt <- 255
    
    member this.Create() =
        let exercise = new Form(Height = hgt)

        exercise.Text <- title
        Application.Run exercise

let exo = new Exercise()
exo.Create()

Of course, you can create different do and static do sections in your class. In this case, the static do section would be accessed first.

A Static Method

Like a field, a method of a class can be defined as static. Such a method can access any member of the class but it depends on how the member was declared.

To create a static method, type the static keyword to the left of the member keyword. The method cannot use the this keyword. Here is an example:

type Exercise() =
    static member Initialize() = . . .

As mentioned already, the way a static method accesses a field depends on how the member variable was created. Static members are directly accessible in the body of a static method, with just the name of the static member. Here is an example:

type Exercise() =
    static let mutable hgt = 0
    
    static member Initialize() =
        hgt <- 255

Outside the class, to access a static member, don't declare a variable for the class. Simply use the name of the class and access the static method using the period operator. Here is an example:

open System
open System.Windows.Forms

type Exercise() =
    static let title : string = "Application Development"
    static let mutable hgt = 0

    static member Initialize() =
        hgt <- 255
    
    member this.Create() =
        let exo = new Form(Height = hgt)

        exo.Text <- title
        
        Application.Run exo

Exercise.Initialize()

let exo = new Exercise()
exo.Create()

Static Classes

Like a variable or a method, a class can be considered static. A static class is a class whose members are accessed without creating an instance of the class. Also, all members of a static class are created as static.

A Review of Built-In Static Classes

 

Introduction to Built-In Static Classes

In the strict sense of the C# language, a static class is a class that is created with the following rules:

  • The class is created with the static keyword. This can be done as follows:
    static class Exercise
    {
    }
  • If the class contains a contructor (not all classes have a constructor), the contructor is created as static
  • All of the properties and methods of the class are created as static

F# doesn't strictly support that concept of static class (almost none of the other .NET languages (C++/CLI and Visual Basic) does). In our lessons, if we refer to a static class, it means that the class follows the rules of a static class as the rules are defined in C# and are applied in the .NET Framework.

Built-In Static Methods

As you may know already, a static method is a member function created as static in a class. In the .NET Framework, many of the classes contain static methods. Most of the methods are created in addition to non-static methods so that a user can use one of the desired and appropriate versions of the method depending on the task.

The Object class is equipped with two versions of the Equals() method. One of the versions uses the following signature:

static member Equals : 
        objA:Object * 
        objB:Object -> bool

This version takes two objects (variables from a class) and compares them. If they are equal, the method returns true. If the objects are different, the method returns false.

The Operating System

To make your application available to users, you must install it on a computer. While someone is using your application, you may want to get some information about the computer. To help you with this, the .NET Framework provides a sealed class named OperatingSystem.

The OperatingSystem class is defined in the System namespace of the mscorlib.dll library. To use this class, first declare a variable.

The Type Built-In Class

 

Introduction

When studying conditional statements, we saw how to check the value held by a variable. In some cases, when you have a variable, you may need to identify the class name of that variable. To assist you with this, the .NET Framework provides an abstract class named Type. The Type class is created in the System namespace. Because Type is an abstract class, you cannot declare a variable of it.

The Type of an Object

You will usually need the services of the Type class to know the data type of a variable or of an argument to a method. To make this possible, the Object class is equipped with a method named GetType. Its signature is:

member GetType : unit -> Type

This method takes no argument and returns the name of the class or the data type of the variable that called it. If the variable that calls this function is of a primitive data type, this method returns its .NET Framework equivalent.

The Base Type of a Class

The Type.GetType() method gives you the name of the class of a variable. If the class is derived from another class and you want to know the name of its parent class, the Type class can help you, using a property named BaseType.

In the same way, you can use recursion, which would consist of accessing the BaseType of the BaseType of the BaseType ... of a variable.

The Assembly to Which a Type Belongs

Sometimes you need more information from a class or an application. For example, if a class is defined in an assembly, you may want to know what that assembly is. To assist you with getting this information, the Type class is equipped with a property named Assembly. When accessing the Type.Assembly property, make sure the class of the variable you are using has a formal assembly. Otherwise you would receive an error.

The Full Name of a Type

To get the complete name of the class of a variable without the assembly, use the Type.FullName property.

The Namespace of a Type

Besides the assembly in which the class of a variable exists, you may want to know the namespace in which that class is defined. To get this information, the Type class is equipped with the Namespace property.

Finding Out Whether a Type is a Class or an Abstract

Besides the name of the class of a variable, another valuable piece of information to find out about a class is its classification. That is, sometimes when using a type, you may want to know whether it is a simple regular class, an abstract class, an interface, an enumeration, or another type (structure, etc). To assist you with checking this, the Type class provides various Boolean properties such as IsAbstract or IsClass.

The Environment Class

 

Introduction

The .NET Framework provides a static class named Environment. This class quickly and easily gives some information about the application that is currently running and some objects you can use in your application.

The Environment class is defined in the System namespace. This means that you don't have to do anything to access it, since it is automatically available whenever you create an application.

Getting the Machine Name

To let you know the name of the computer on which your application is running, the Environment class provides a property named MachineName.

Getting the Operation System's Version

To assist you with knowing the version of the operating system on which the application is running, the Environment class provides the OSVersion property.

Getting the Current User's name

At any time, if you want to know the user name of the person or the non-human who is currently using your application, you can get the value of the UserName property of the Environment class.

Getting the System Directory

The Microsoft Windows operation system is usually installed on the C: drive with most of its drivers in a folder named System32 that is located either under Windows or WinNT. This full path of the driver's main folder is referred to as the system directory.

To assist you with finding out the path to the system directory, the Environment class is equipped with a property named SystemDirectory.

Creating a New Line

We know that you can use the \n escape sequence to send the caret to the next line. To support this operation, the Environment class is equipped with a property named NewLine. This property causes the same behavior as its escape sequence counterpart.

Getting the Current Directory

Whether you save the project or not, the Environment class allows you to know where the current project is located. To assist you with this, it is equipped with a property named CurrentDirectory.

Exiting an Application

You can specify a certain number by which your application must exit. This information can be used by other applications, however they want to use it. To assist you with getting the exit number of an application, the Environment class is equipped with a property named ExitCode.

To let you specify the exit number of your application, the Environment class is equipped with a method named Exit. Its signature is:

static member Exit : 
        exitCode:int -> unit

The default value to pass to this method is 0.

   
   
 

Home Copyright © 2014-2015, FunctionX Home