forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

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

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support
View previous topic :: View next topic  
Author Message
gperiskic



Joined: 18 Dec 2009
Posts: 34
Location: Stuttgart, Germany

PostPosted: Tue Oct 22, 2013 8:18 am    Post subject: How to check if a .NET-Object is null in FTN Reply with quote

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
View user's profile Send private message Visit poster's website
gperiskic



Joined: 18 Dec 2009
Posts: 34
Location: Stuttgart, Germany

PostPosted: Fri Nov 08, 2013 3:19 pm    Post subject: Reply with quote

Any comments on this topic? Robert?
Back to top
View user's profile Send private message Visit poster's website
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 7916
Location: Salford, UK

PostPosted: Sat Nov 09, 2013 9:11 am    Post subject: Reply with quote

Can you provide a short program to illustrate the context with pseudo code commented out for the test.
Back to top
View user's profile Send private message AIM Address
JMoody



Joined: 26 Aug 2008
Posts: 20
Location: Ohio

PostPosted: Mon Nov 11, 2013 3:16 pm    Post subject: Reply with quote

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
View user's profile Send private message
gperiskic



Joined: 18 Dec 2009
Posts: 34
Location: Stuttgart, Germany

PostPosted: Thu Nov 14, 2013 11:59 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 7916
Location: Salford, UK

PostPosted: Thu Nov 14, 2013 12:16 pm    Post subject: Reply with quote

I still need a sample.
Back to top
View user's profile Send private message AIM Address
gperiskic



Joined: 18 Dec 2009
Posts: 34
Location: Stuttgart, Germany

PostPosted: Mon Nov 18, 2013 6:00 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 7916
Location: Salford, UK

PostPosted: Fri Nov 29, 2013 9:52 am    Post subject: Reply with quote

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
View user's profile Send private message AIM Address
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group