PDA

View Full Version : More .NET String Questions


Don Johnson
11-14-2003, 11:22 PM
Ok, I'm trying to call a C Sharp DLL from a fortran.NET Console application. All method calls works fine, except when I use the "out" attribute with strings or arrays.

It compiles fine, and the CS dll is callable from other CS apps, but I get this message when running the Fortran ap:

"Method not found: Void ClassLibrary7.Class1.getstr(Int16, System.String)."

The code:
//ClassLibrary7.cs
using System;
namespace ClassLibrary7
{
public class Class1
{
public Class1()
{
}
public static void getstr(short num, out string ostr)
{
ostr = num.ToString();
}
}
}


!***fortran.net***
! ConsoleApp
program MyProg
use System
use ClassLibrary7
implicit none
integer*2 :: xxx
character(len=5) :: buff
type (Class1), pointer :: cslib
type (String), pointer :: str1
xxx = 5_2
call cslib.getstr(xxx, str1)
buff = astring(str1)
end program MyProg


***
Now, the problem seems to be with the "out" statement...as if I take that out, I don't get that error, but the value is NOT passed to my fortran str1 pointer, and hence using astring fails.

Looking at the ildasm of both, I see that the arguments for getstr with the "out" attribute left in, shows "getstr: void(int16,string&)"
while, w/o the "out", it's just "getstr: void(int16,string)".
Meanwhile, the call from MyProg shows it looking for (int16,string), hence the error, as if it doesn't recognize the string out properly.

Single numeric values seem to work fine, with the out attribute, but arrays and strings have this problem. Any suggestions? I have tried using functions, but then I get a whole different error saying I have more actual arguments than dummy arguments (even with zero actual arguments).
Thanks in advance. :)

tzeis
11-17-2003, 07:49 PM
I see that if the "out" method parameter is used, C# requires that the calling program declare that the argument is an "out" argument. This is not really supported in Fortran, and I suspect that it works in some cases by accident.
While I am unable to make your program work by passing arguments, I am able to make a function version work. Here is the C# side of my function:


//ClassLibrary7.cs
using System;
namespace ClassLibrary7
{
public class Class1
{
public Class1()
{
}
public String getstr(short num)
{
return num.ToString();
}
}
}


and here is the Fortran side.


!***fortran.net***
! ConsoleApp
program MyProg
use System
use ClassLibrary7
implicit none
integer*2 :: xxx
character(len=5) :: buff
type (Class1) :: cslib
type (String), pointer :: str1
xxx = 5_2
str1 => cslib.getstr(xxx)
buff = astring(str1)
write(*,*) buff
end program MyProg


As an aside, here is what the original C# subroutine would look like if it were done with Fortran:


module ClassLibrary7
use System
type Class1
contains
procedure, nopass :: getstr
end type
contains
subroutine getstr(i,str)
integer(2) :: i
type(String), pointer :: str
str => i%ToString()
end subroutine
end module


I notice that you are able to refer to the static method written in C# using the variable name rather than the class name. In Fortran, you can only use the class name to refer to the method.

Hope this helps.

Don Johnson
11-17-2003, 08:50 PM
That does help.

I see my problem with using static, and a calling using "cslib." verses "Class1." That explaims my weird problem with functions.
Now I can work around it using functions instead of outs.

I didn't know cs outs weren't supported. It does work for simple data types.

Thanks again!
Don