contents   index   previous   next



Passing Basic data types (Integer, Boolean and Real types)

 

Visual Basic represents integer, Boolean and real types the same way as standard-conforming Fortran compilers. Table 4.1 shows the equivalences between the most common Visual Basic and Fortran types. (Some types are not available in all Fortran compilers. You should consult the documentation of your specific compiler for more information.)

 

In general it is easier to pass basic data types by reference, because this is the standard convention for both Visual Basic and Fortran. Besides, some Fortran compilers do not support arguments by value as passed by Visual Basic.

 

The following example illustrates how to call a Fortran function that computes the arc-cosine (in radians) of X. The Fortran code for the function is as follows:

 

 

 

 

 

Example 4.1

 

Program Example 41

 

function ArcCosine(X)

    implicit none

    double precision::ArcCosine

    double precision,intent(in)::X

    ArcCosine=dacos(X)

end function ArcCosine

 

 

For simplicity the code shown above does not include any of the compiler-dependent commands necessary to create a DLL including this function, or to indicate the calling convention of the function. Nor are we showing how to compile the code. These are explained in the next chapter. For the time being, let's assume that you somehow have created a DLL named "Example41.dll ", that it contains function ArcCosine as defined above, and that the DLL resides in the examples directory.

 

Function ArcCosine is very simple. The function receives a double precision argument (X) and calls an intrinsic Fortran function (dacos) that returns the arc-cosine of the argument. Note that X is declared with intent(in), because its content is not changed in the function. In spite of this, as you will see below, the Visual Basic declaration of the external function still passes argument X by reference. This illustrates the difference between the method used to pass arguments and their intent as declared in the Fortran subroutine (see section Declaring arguments in Fortran Procedures Callable from Visual Basic for more information).

 

To call ArcCosine from Visual Basic or Visual Basic for Applications, you need to declare the function as external. To see how this is done, let's start by creating a new Visual Basic project. Then add a module file with the following declaration:

 

Declare Function ArcCosine Lib "Example41.dll" (ByRef X As Double) As Double

 

In this case, we are assuming that Example41.dll and the Visual Basic executable will always be located in the same directory, or that Example41.dll will be located in the Windows system directory (e.g. Winnt\System32). If this is not the case, then you should add the path to Example41.dll in the declaration.

 

You can now create a simple form that accepts the value of X and shows the result of computing ArcCosine (Figure 4.1)

 

Below is an example of the possible code that could be executed when the Go! button is clicked:

 

Private Sub cmdGo_Click()

 

    Dim X As Double

    Dim Result As Double

 

    X = CDbl(txtX.Text)         'convert entered value into a double

    Result = ArcCosine(X)       'compute arccosine

    txtArcCosine.Text = Result   'show the result

 

End Sub

 

 

You can use a similar method to pass any of the other data types shown in Table 4.1.

 

Passing strings by reference