.NET Objects

In addition to the standard Fortran types described in Calling Fortran from other .NET languages, the .NET environment provides for what are called objects. An object is simply an instance of a class. .NET objects are similar in some respects to instances of Fortran derived types.

In what follows, readers who are familiar with Fortran 95 derived types and pointers may find it helpful to draw an analogy with the following fragment of code.

!Stage 1: Define a derived type.
 INTEGER x,y,r
!Stage 2: Declare a Fortran pointer to a Circle.
!Stage 3: Allocate memory and use the TYPE constructor.

FTN95 extends standard Fortran in order to enable you declare and use .NET objects. Currently it is not possible to define a class in the FTN95 code (i.e. there is no equivalent of Stage 1 for classes). The class must be defined externally in another (non-Fortran) assembly.

The FTN95 keyword OBJECT is used to declare an alias for an object. For example:

OBJECT("MySpace.TestClass") ALPHA

This is equivalent to Stage 2 and declares a variable called ALPHA, that will be used as an alias for a .NET object. The alias is as yet undefined (i.e. the variable is not yet ASSOCIATED with an object). The class (i.e. the structure of the object) is defined in another assembly (typically created using C#). The name of the class is TestClass and it is contained in the namespace MySpace. Namespaces and classes can be nested, so the string can contain several dot characters.

When you declare an alias for an object, the compiler must obtain a definition for its class from the assembly file in which the class is defined. This is automatic if the class is a system class (e.g. System.Console). In other cases names of assembly files must be given to the compiler as LIBRARY directives or by using the /LIBRARY command line option. The compiler will report an error if it cannot find a definition for the class.

Sample code is provided that illustrates how to use .NET classes. The Fortran Calculator sample illustrates calling Fortran code from C#, along with the Mandelbrot example.

See also Manipulating .NET objects.



Copyright © 1999-2024 Silverfrost Limited