PDA

View Full Version : Using interfaces on objects passed byref


Pinocchio6
04-15-2004, 10:51 AM
Hi,
I do have a mixed language environment with C# and Fortran. The C# code is as follows:

public interface IntA
{
   void Method();
}

public abstract class AObj : IntA
{
}

public interface IntB
{
   void Do(AObj aobj);
}

Now I want to implement an object in Fortran which inherits from interface IntB:

type, implements(IntB) :: Sample
   contains
   procedure, pass :: Do
end type Sample
contains
subroutine Do(this, aobj)
   type(Sample) :: this
   type(AObj) :: aobj
   ! type(AObj), target :: aobj ! error FRT1244
   type(IntA), pointer :: ia
   ia => aobj ! error FRT1386
end subroutine Do

My problem is that I dont know how to access IntB.Method() on object AObj, because if I try to use
   type(AObj), target :: aobj
so that ia => aobj is possible, I get error FRT1244 - derived type 'Sample' must implement interface member 'Do'
On the other hand, when I use
   type(AObj) :: aobj
I get FRT1386 - Pointer object in pointer assignment statement must be a variable name or a structure component which has the POINTER attribute
on ia => aobj.

Is there a workaround for this?

Regards, Charlie

tzeis
04-15-2004, 10:53 PM
There seems to be some missing information in your description. When I try to compile the C# code you posted, I get compile errors, because abstract class AObj does not provide an implementation for interface IntA. Could you post something that is compileable I can use to test the problem?

Pinocchio6
04-16-2004, 06:31 AM
Sorry about that, I've also mixed up the the interfaces in my sample, I've corrected that now.
Please find enclosed compileable sources:

ClassLibrary1 is the C# implementation of the Interface and Class definition,
ClassLibrary2 is a C# implementation of the class I'd like to implement in Fortran,
Library1 is my Fortran implementation so far.

Regards, Charlie

tzeis
04-16-2004, 07:41 PM
Thanks for posting the code, that gave me something to work with.

I would say that instead of trying the pointer assignment for ia, instantiate it instead.

For example, instead of the pointer assignment:

ia => aobj

allocate it:

allocate (ia, source = AObj())

When I make this change, the Fortran code compiles without error.

Pinocchio6
04-19-2004, 11:51 AM
Hello tzeis,

thanks for your effort, but that is not what's intended, and it won't run either. I intend to access the passed object (of type AObj) using interface IntA. Your implementation would instantiate a new AObj using interface pointer ia. That's also the reason why it won't work: it throws a runtime exception, because AObj is an abstract class and therefore cannot be instantiated. The actual object passed in the call would be inherited from class AObj of course.
To clarify the whole thing I've included a new enhanced sample, together with a little C# program (WindowsApplication1) which instantiates the objects and calls the methods, so you can try it. You may comment out the "using Library1;" and compile it with "using ClassLibrary2;" instead, to see how the sample works with a C# implementation of the class.

Regards, Charlie

tzeis
04-28-2004, 06:00 PM
It looks like the problem appears when in trying to implement an abstract class in Fortran. Doing this is not currently supported, and the work on the next rev is too far along to be able to incorporate it in the next version. The best solution that I can offer at this time is to do a "shell" implementation of the abstract class using C#, and call out from the shell to a Fortran routine which does the bulk of the work.