View previous topic :: View next topic |
Author |
Message |
gperiskic
Joined: 18 Dec 2009 Posts: 34 Location: Stuttgart, Germany
|
Posted: Tue Oct 22, 2013 8:18 am Post subject: How to check if a .NET-Object is null in FTN |
|
|
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. |
|
Back to top |
|
|
gperiskic
Joined: 18 Dec 2009 Posts: 34 Location: Stuttgart, Germany
|
Posted: Fri Nov 08, 2013 3:19 pm Post subject: |
|
|
Any comments on this topic? Robert? |
|
Back to top |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7925 Location: Salford, UK
|
Posted: Sat Nov 09, 2013 9:11 am Post subject: |
|
|
Can you provide a short program to illustrate the context with pseudo code commented out for the test. |
|
Back to top |
|
|
JMoody
Joined: 26 Aug 2008 Posts: 20 Location: Ohio
|
Posted: Mon Nov 11, 2013 3:16 pm Post subject: |
|
|
In C# you can do it by just checking it for null:
Code: | using System;
public static boolean IsNull(System.Object object)
{
return (object == null);
}
|
Since every object inherits from System.Object, this should work for anything. |
|
Back to top |
|
|
gperiskic
Joined: 18 Dec 2009 Posts: 34 Location: Stuttgart, Germany
|
Posted: Thu Nov 14, 2013 11:59 am Post subject: |
|
|
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? |
|
Back to top |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7925 Location: Salford, UK
|
Posted: Thu Nov 14, 2013 12:16 pm Post subject: |
|
|
I still need a sample. |
|
Back to top |
|
|
gperiskic
Joined: 18 Dec 2009 Posts: 34 Location: Stuttgart, Germany
|
Posted: Mon Nov 18, 2013 6:00 pm Post subject: |
|
|
Ok, here's the code for c# and Fortran:
Code: | /------------------------------------------------------------------------------------
/ 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 |
|
Back to top |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7925 Location: Salford, UK
|
Posted: Fri Nov 29, 2013 9:52 am Post subject: |
|
|
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. |
|
Back to top |
|
|
|