Silverfrost Forums

Welcome to our forums

How to check if a .NET-Object is null in FTN

22 Oct 2013 7:18 #13186

Hi, is there a function in FTN (or a .net function) which could be used inside FTN to prove if a .net object is 'null'? I found a function to check if a variable is NaN (System.Double.IsNaN) but I'm still missing the corresponding function for objects.

8 Nov 2013 2:19 #13294

Any comments on this topic? Robert?

9 Nov 2013 8:11 #13298

Can you provide a short program to illustrate the context with pseudo code commented out for the test.

11 Nov 2013 2:16 #13309

In C# you can do it by just checking it for null:

using System;
public static boolean IsNull(System.Object object)
{
  return (object == null);
}

Since every object inherits from System.Object, this should work for anything.

14 Nov 2013 10:59 #13314

Hi JMoody,

You’re perfectly right - this is exactly the solution we're currently using in some of our programs - it works fine. However, since FTN is a compiler which 'supports .NET' than I would expect that this basic function is available in Fortran as well - without any detour over other programming languages. The point is that I was unable to find it...

Paul, do you still need a program example from me or now the issue is clear?

14 Nov 2013 11:16 #13315

I still need a sample.

18 Nov 2013 5:00 #13332

Ok, here's the code for c# and Fortran:

/------------------------------------------------------------------------------------
/  C# part
using System;

namespace NullTest
{
    class Vector2d
    {
        public double X { get; set; }
        public double Y { get; set; }

        public Vector2d(double x, double y)
        {
            X = x;
            Y = y;
        }
    }

    class VectorPair
    {
        public Vector2d V1 { get; set; }
        public Vector2d V2 { get; set; }
    }    

    class Program
    {
        static void Main(string[] args)
        {
            Vector2d firstVector = new Vector2d(1.0, 2.0);
            double length1 = FortranClass.CalcLength(firstVector);

            Vector2d secondVector = null;  // the object secondVector is not initialised
            double length2 = FortranClass.CalcLength(secondVector);  // should not cause exception but return e.g. 0 or NaN

            VectorPair pair1 = new VectorPair();
            pair1.V1 = new Vector2d(1.0, 2.0);
            pair1.V2 = new Vector2d(2.0, 1.0);
            double distance1 = FortranClass.CalcDistance(pair1);

            VectorPair pair2 = new VectorPair();
            pair2.V1 = new Vector2d(1.0, 2.0);
            pair2.V2 = null;  // the property pair2.V2 does not contain an initialised object of type Vector2d
            double distance2 = FortranClass.CalcDistance(pair2);  // should not cause exception but return e.g. 0 or NaN

            Console.WriteLine('Length 1: {0}', length1);
            Console.WriteLine('Length 2: {0}', length2);
            Console.WriteLine('Distance 1: {0}', distance1);
            Console.WriteLine('Distance 2: {0}', distance2);
            Console.ReadKey();
        }
    }
}
/------------------------------------------------------------------------------------
/  Fortran part

Function Length(Vector_)
Implicit none
Assembly_interface (Name='CalcLength)
Object('NullTest.Vector2d')::Vector_
Real*8 Length

Length=0.0
if (.NOT.IsNull(Vector_)) then				! Check if the vector-object exists
 Lenght=(sqrt(Vector_%X**2+Vector_%Y**2)
end if

end function



Function Distance(VectorPair_)
Implicit none
Assembly_interface (Name='CalcDistance)
Object('NullTest.VectorPair')::VectorPair_
Real*8 Distance

Distance=0.0
if (.NOT.IsNull(VectorPair_%V1) .and. .NOT.IsNull(VectorPair_%V2)) then				! Check if the vector-objects exists
 Distance=sqrt((VectorPair_%V1%X-VectorPair_%V2%X)**2+(VectorPair_%V1%Y-VectorPair_%V2%Y)**2)
end if

end function

The problem is how to check if a vector is initialized. I used a pseudo function IsNull, which does not exist. Alternativelly, one could compare the value of the object with a constant (Vector_.eq.Null), however, how to define this constant?

Regards, Goran

29 Nov 2013 8:52 #13383

I don't know of a direct way to test for null in the Fortran code.

There are at least three ways to proceed:

  1. Do the test before CalcLength (etc.) is called in the C# code.

  2. Write a C# function that does the test and then goes on to call CalcLength.

  3. Write a C# function called IsNull that is callable from your Fortran code.

Please login to reply.